@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.
@error @warn arguments functions mixin sass