dart-sass/lib/src/exception.dart

57 lines
1.8 KiB
Dart
Raw Normal View History

2016-09-03 16:42:29 +02:00
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'package:source_span/source_span.dart';
import 'package:stack_trace/stack_trace.dart';
2016-09-03 16:42:29 +02:00
2016-10-10 05:57:10 +02:00
/// An exception thrown by Sass.
2016-09-03 16:42:29 +02:00
class SassException extends SourceSpanException {
FileSpan get span => super.span as FileSpan;
SassException(String message, FileSpan span) : super(message, span);
}
2016-10-10 05:57:10 +02:00
/// An exception thrown by Sass while evaluating a stylesheet.
class SassRuntimeException extends SassException {
2016-10-10 05:57:10 +02:00
/// The Sass stack trace at the point this exception was thrown.
final Trace trace;
SassRuntimeException(String message, FileSpan span, this.trace)
: super(message, span);
String toString({color}) {
var buffer = new StringBuffer(super.toString(color: color));
for (var frame in trace.toString().split("\n")) {
if (frame.isEmpty) continue;
buffer.writeln();
buffer.write(" $frame");
}
return buffer.toString();
}
}
2016-10-10 05:57:10 +02:00
/// An exception thrown when Sass parsing has failed.
2016-09-03 16:42:29 +02:00
class SassFormatException extends SourceSpanFormatException
implements SassException {
FileSpan get span => super.span as FileSpan;
String get source => span.file.getText(0);
SassFormatException(String message, FileSpan span) : super(message, span);
}
2016-09-03 17:20:00 +02:00
/// An exception thrown by SassScript.
2016-10-10 05:57:10 +02:00
///
/// This doesn't extends [SassException] because it doesn't (yet) have a
/// [FileSpan] associated with it. It's caught by Sass's internals and converted
/// to a [SassRuntimeException] with a source span and a stack trace.
class SassScriptException {
2016-10-10 05:57:10 +02:00
/// The error message.
2016-09-03 17:20:00 +02:00
final String message;
SassScriptException(this.message);
2016-09-03 17:20:00 +02:00
String toString() => "$message\n\nBUG: This should include a source span!";
}