在Sass中抛出自定义错误/警告

时间:2013-11-21 10:33:04

标签: sass

我正在创建一个字体模块,其中包含我的所有webfonts和一些Sass mixins来写出@font-face声明。主要的mixin类似于includeFont(name, weight, style)

我会在一些Sass变量中保留一个记录,其中哪些字体的重量和样式实际上是可用的,并且通过巧妙地对此我想我可以写mixin以便我可以检测到我是否可以尝试并请求一个不存在的字体。

但是当我发现这种情况时,我怎么能抛出错误?

1 个答案:

答案 0 :(得分:6)

Sass 3.4.0开始,您可以使用@error指令导致致命错误:

$stuff: fubar;
@if ($stuff == fubar) {
    @error "stuff is fubar";
}

如果您尝试在shell中编译它,您将看到以下内容:

$ sass test.scss
Error: stuff is fubar
        on line 3 of test.scss
  Use --trace for backtrace.

还有相关的@warn@debug指令在语言中使用的时间更长,以防对您更有用。例如:

@debug "stuff is happening";
@warn "stuff is happening that probably shouldn't be happening";

/*
    Note that these directives do not terminate execution, and this block
    will therefore still be output.
*/
* {
    color: pink;
}

编译时:

$ sass test2.scss
test2.scss:1 DEBUG: stuff is happening
WARNING: stuff is happening that probably shouldn't be happening
         on line 2 of test2.scss

/*
    Note that these directives do not terminate execution, and this block
    will therefore still be output.
*/
* {
  color: pink; }