2013-11-21 65 views
11

我正在創建一個字體模塊,其中包含我所有的Web字體和一些Sass混合類型來編寫@font-face聲明。主要的混合類似於includeFont(name, weight, style)在Sass中投擲自定義錯誤/警告

我會在一些Sass變量中保留一個記錄,其中字體的重量和樣式實際上可用,並且通過對此進行巧妙的處理,我認爲我可以編寫mixin,以便我可以檢測到我是否嘗試並請求不存在的字體。

但是當我檢測到這種情況時,我該如何拋出一個錯誤?

+0

你嘗試在看文檔? http://sass-lang.com/documentation/file.SASS_REFERENCE.html#_5 – cimmanon

+1

我做到了,但我錯過了這一點 - 謝謝 – wheresrhys

回答

5

截至Sass 3.4.0,有一個@error指令,你可以用它來產生一個致命的錯誤:如果您嘗試在shell編譯這個

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

,你會看到以下內容:

$ 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; }