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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
What’s a Log File and What are Log File Monitors?MiscellaneousLearn how to give a temporary name to a column or to a table using SQL AliasesSQL/MySQLThe difference between isFinite() method and isFinite() function in JavaScriptJavaScriptCSS Overflow Property with ExamplesCSSWP_Query Class in WordPressWordPressHow to get user’s Browser and Operating Systems information using PHP?PHP