Yogesh Chauhan's Blog

At-rules: @error and @warn in SCSS

in SCSS on July 12, 2021

@error rule

@error rule comes in handy while using @mixin and functions and taking arguments, when you want to make sure that the arguments passed have the exact types and formats you asked for.

If that’s not the case then use @error rule to notify users about it before you move forward with the rest of the code.

The implementation is as easy as the idea itself! All you need to do is write whatever you want to print as an error after @error. Most of the time, you’d want to use a string and the passed argument to show the complete error.

SassScript will stop execution/compilation of the file after it prints the error.

@error rule example


@mixin reflexive-position($property, $value) {
  @if $property !=left and $property !=right {
    @error "Property #{$property} must be either left or right.";
  }

  ...
  ...
}

.sidebar {
  @include reflexive-position(top, 12px);
}

It will print an error like this:


Error: "Property top must be either left or right."
  ╷
3 │     @error "Property #{$property} must be either left or right.";
  │     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@warn rule

@warn rule is similar to @error rule and it comes in handy while using @mixin and functions and taking arguments. But unlike @error, you just want to discourage users from passing certain args or values.

@warn rule is really useful when showing warning to users when they pass deprecated arguments/values.

Writing @warn rule is similar to writing an @error rule.

Unlike the @error rule, the @warn rule doesn’t stop SassScript entirely.

@warn rule example


$known-prefixes: webkit,
moz,
ms,
o;

@mixin prefix($property, $value, $prefixes) {
  @each $prefix in $prefixes {
    @if not index($known-prefixes, $prefix) {
      @warn "Unknown prefix #{$prefix}.";
    }
  }
...
}

.class-name {
  @include prefix(transform, rotate(15deg), abc o);
}

The script will execute but will show a warning like this:


Warning: Unknown prefix abc.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
PHP Login System using PDO Part 1: Create User Registration PagePHPCREATE DATABASE in PostgreSQLPostgresHow does Next.js load pages faster?NextJSHow to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?PHPClearwater Seafoods – B2C in ChinaMiscellaneousWindow setTimeout() Method in JavaScriptJavaScript