Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(967)

Unified Diff: pkg/barback/lib/src/errors.dart

Issue 22824023: Start sketching out a buildAll() method. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/barback/lib/src/errors.dart
diff --git a/pkg/barback/lib/src/errors.dart b/pkg/barback/lib/src/errors.dart
index 138984ce58200de5d450f9a1b108efcb68f60b64..761baf297e16f2c1e6b9a6f51f5db75e65e4c1dd 100644
--- a/pkg/barback/lib/src/errors.dart
+++ b/pkg/barback/lib/src/errors.dart
@@ -20,10 +20,44 @@ class AssetNotFoundException implements Exception {
String toString() => "Could not find asset $id.";
}
+/// Recursively replaces any occurrences of [AggregateException] in [errors]
nweiz 2013/08/20 22:29:03 Remove "Recursively"
Bob Nystrom 2013/08/21 18:10:23 Done.
+/// with the list of errors it contains.
+Iterable<BarbackException> flattenAggregateExceptions(
+ Iterable<BarbackException> errors) {
+ return errors.expand((error) {
+ if (error is! AggregateException) return [error];
+ return error.errors;
+ });
+}
+
/// The interface for exceptions from the barback graph or its transformers.
///
/// These exceptions are never produced by programming errors in barback.
-abstract class BarbackException implements Exception {}
+abstract class BarbackException implements Exception {
+ /// Takes a collection of [BarbackExceptions] and returns a single exception
+ /// that contains them all.
+ ///
+ /// If [errors] is empty, returns `null`. If it only has one error, that
+ /// error is returned. Otherwise, an [AggregateException] is returned.
+ static BarbackException aggregate(Iterable<BarbackException> errors) {
+ if (errors.isEmpty) return null;
+ if (errors.length == 1) return errors.single;
+ return new AggregateException(errors);
+ }
+}
+
+/// An error that wraps a collection of other [BarbackException]s.
+///
+/// It implicitly flattens any [AggregateException]s that occur in the list of
+/// exceptions it wraps.
+class AggregateException implements BarbackException {
+ final Set<BarbackException> errors;
+
+ AggregateException(Iterable<BarbackException> errors)
+ : errors = flattenAggregateExceptions(errors).toSet();
+
+ String toString() => "Multiple errors occurred:\n\n- ${errors.join('\n- ')}";
nweiz 2013/08/20 22:29:03 We should be prepared for an individual error to b
Bob Nystrom 2013/08/21 18:10:23 Done.
+}
/// Error thrown when two or more transformers both output an asset with [id].
class AssetCollisionException implements BarbackException {
@@ -66,34 +100,49 @@ class InvalidOutputException implements BarbackException {
"same package (${transform.primaryId.package}).";
}
+/// Base class for an error that wraps another.
+abstract class WrappedException implements BarbackException {
nweiz 2013/08/20 22:29:03 This class should be private.
Bob Nystrom 2013/08/21 18:10:23 Done.
+ /// The wrapped exception.
+ final error;
+
+ WrappedException(this.error);
+
+ String get _message;
+
+ String toString() {
+ var result = "$_message: $error";
+
+ var stack = getAttachedStackTrace(error);
+ if (stack != null) {
+ result = "$result\n${new Trace.from(stack).terse}";
+ }
+
+ return result;
+ }
+}
+
/// Error wrapping an exception thrown by a transform.
-class TransformerException implements BarbackException {
+class TransformerException extends WrappedException {
/// The transform that threw the exception.
final TransformInfo transform;
- /// The wrapped exception.
- final error;
-
- TransformerException(this.transform, this.error);
+ TransformerException(this.transform, error)
+ : super(error);
- String toString() => "Transform $transform threw error: $error\n" +
- new Trace.from(getAttachedStackTrace(error)).terse.toString();
+ String get _message => "Transform $transform threw error";
}
/// Error thrown when a source asset [id] fails to load.
///
/// This can be thrown either because the source asset was expected to exist and
/// did not or because reading it failed somehow.
-class AssetLoadException implements BarbackException {
+class AssetLoadException extends WrappedException {
final AssetId id;
- /// The wrapped exception.
- final error;
-
- AssetLoadException(this.id, this.error);
+ AssetLoadException(this.id, error)
+ : super(error);
- String toString() => "Failed to load source asset $id: $error\n"
- "${new Trace.from(getAttachedStackTrace(error)).terse}";
+ String get _message => "Failed to load source asset $id";
}
/// Information about a single transform in the barback graph.

Powered by Google App Engine
This is Rietveld 408576698