Skip to content

Instantly share code, notes, and snippets.

@xzyfer
Created May 25, 2014 05:14
Show Gist options
  • Save xzyfer/ab32c066e694f1f6e8a3 to your computer and use it in GitHub Desktop.
Save xzyfer/ab32c066e694f1f6e8a3 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.7)
// Compass (v1.0.0.alpha.18)
// ----
// Handle Errors
//@include handle-error(test param, {accepted types or strings as spaced list}, {optional custom message as string});
@mixin handle-error($test, $testAgainst, $custom-message:null) {
$string-match: false;
$type-match: false;
$message: null;
@each $item in $testAgainst {
@if ($item == $test) { $string-match:true;}
@if (type-of($test) == $item) { $type-match:true;}
}
@if ($custom-message != null) {
$message: $custom-message;
}
@else {
$message: "The type of the passed variable is incorrent. expected:{#{$testAgainst}}, was:{#{$test} as #{type-of($test)}}";
}
@if ($string-match == false and $type-match == false) {
@warn $message;
// Included for test cases
status:$message;
}
// Included for test cases
@else {
status:"ok!";
}
}
// Example in Use
// ---------------------------------------------------------------------------
@mixin set-background($color) {
background:$color;
@include handle-error($color, color "transparent" );
}
// Test Cases
// ---------------------------------------------------------------------------
test-string-match-true {
$param: "random-string";
@include handle-error($param, number "random-string" );
}
test-string-match-false {
$param: "random-string";
@include handle-error($param, number "random-string-check" );
}
test-color-match-true {
$param: #ffffff;
@include handle-error($param, color);
}
test-color-match-true {
$param: green;
@include handle-error($param, color);
}
test-color-match-false {
$param: newMadeupColor;
@include handle-error($param, color);
}
test-custom-error-true {
$param: blue ;
@include handle-error($param, number, "This is a custom error!");
}
test-string-match-true {
status: "ok!";
}
test-string-match-false {
status: 'The type of the passed variable is incorrent. expected:{number "random-string-check"}, was:{random-string as string}';
}
test-color-match-true {
status: "ok!";
}
test-color-match-true {
status: "ok!";
}
test-color-match-false {
status: "The type of the passed variable is incorrent. expected:{color}, was:{newMadeupColor as string}";
}
test-custom-error-true {
status: "This is a custom error!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment