Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library barback.errors; | 5 library barback.errors; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 10 |
| 11 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
| 12 import 'transformer.dart'; | 12 import 'transformer.dart'; |
| 13 | 13 |
| 14 /// Error thrown when an asset with [id] cannot be found. | 14 /// Error thrown when an asset with [id] cannot be found. |
| 15 class AssetNotFoundException implements Exception { | 15 class AssetNotFoundException implements Exception { |
| 16 final AssetId id; | 16 final AssetId id; |
| 17 | 17 |
| 18 AssetNotFoundException(this.id); | 18 AssetNotFoundException(this.id); |
| 19 | 19 |
| 20 String toString() => "Could not find asset $id."; | 20 String toString() => "Could not find asset $id."; |
| 21 } | 21 } |
| 22 | 22 |
| 23 /// 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.
| |
| 24 /// with the list of errors it contains. | |
| 25 Iterable<BarbackException> flattenAggregateExceptions( | |
| 26 Iterable<BarbackException> errors) { | |
| 27 return errors.expand((error) { | |
| 28 if (error is! AggregateException) return [error]; | |
| 29 return error.errors; | |
| 30 }); | |
| 31 } | |
| 32 | |
| 23 /// The interface for exceptions from the barback graph or its transformers. | 33 /// The interface for exceptions from the barback graph or its transformers. |
| 24 /// | 34 /// |
| 25 /// These exceptions are never produced by programming errors in barback. | 35 /// These exceptions are never produced by programming errors in barback. |
| 26 abstract class BarbackException implements Exception {} | 36 abstract class BarbackException implements Exception { |
| 37 /// Takes a collection of [BarbackExceptions] and returns a single exception | |
| 38 /// that contains them all. | |
| 39 /// | |
| 40 /// If [errors] is empty, returns `null`. If it only has one error, that | |
| 41 /// error is returned. Otherwise, an [AggregateException] is returned. | |
| 42 static BarbackException aggregate(Iterable<BarbackException> errors) { | |
| 43 if (errors.isEmpty) return null; | |
| 44 if (errors.length == 1) return errors.single; | |
| 45 return new AggregateException(errors); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 /// An error that wraps a collection of other [BarbackException]s. | |
| 50 /// | |
| 51 /// It implicitly flattens any [AggregateException]s that occur in the list of | |
| 52 /// exceptions it wraps. | |
| 53 class AggregateException implements BarbackException { | |
| 54 final Set<BarbackException> errors; | |
| 55 | |
| 56 AggregateException(Iterable<BarbackException> errors) | |
| 57 : errors = flattenAggregateExceptions(errors).toSet(); | |
| 58 | |
| 59 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.
| |
| 60 } | |
| 27 | 61 |
| 28 /// Error thrown when two or more transformers both output an asset with [id]. | 62 /// Error thrown when two or more transformers both output an asset with [id]. |
| 29 class AssetCollisionException implements BarbackException { | 63 class AssetCollisionException implements BarbackException { |
| 30 /// All the transforms that output an asset with [id]. | 64 /// All the transforms that output an asset with [id]. |
| 31 /// | 65 /// |
| 32 /// If this only contains a single transform, that indicates that a | 66 /// If this only contains a single transform, that indicates that a |
| 33 /// transformer produced an output that collides with a source asset or an | 67 /// transformer produced an output that collides with a source asset or an |
| 34 /// asset from a previous phase. | 68 /// asset from a previous phase. |
| 35 final Set<TransformInfo> transforms; | 69 final Set<TransformInfo> transforms; |
| 36 final AssetId id; | 70 final AssetId id; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 59 /// The transform that output the asset. | 93 /// The transform that output the asset. |
| 60 final TransformInfo transform; | 94 final TransformInfo transform; |
| 61 final AssetId id; | 95 final AssetId id; |
| 62 | 96 |
| 63 InvalidOutputException(this.transform, this.id); | 97 InvalidOutputException(this.transform, this.id); |
| 64 | 98 |
| 65 String toString() => "Transform $transform emitted $id, which wasn't in the " | 99 String toString() => "Transform $transform emitted $id, which wasn't in the " |
| 66 "same package (${transform.primaryId.package})."; | 100 "same package (${transform.primaryId.package})."; |
| 67 } | 101 } |
| 68 | 102 |
| 103 /// Base class for an error that wraps another. | |
| 104 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.
| |
| 105 /// The wrapped exception. | |
| 106 final error; | |
| 107 | |
| 108 WrappedException(this.error); | |
| 109 | |
| 110 String get _message; | |
| 111 | |
| 112 String toString() { | |
| 113 var result = "$_message: $error"; | |
| 114 | |
| 115 var stack = getAttachedStackTrace(error); | |
| 116 if (stack != null) { | |
| 117 result = "$result\n${new Trace.from(stack).terse}"; | |
| 118 } | |
| 119 | |
| 120 return result; | |
| 121 } | |
| 122 } | |
| 123 | |
| 69 /// Error wrapping an exception thrown by a transform. | 124 /// Error wrapping an exception thrown by a transform. |
| 70 class TransformerException implements BarbackException { | 125 class TransformerException extends WrappedException { |
| 71 /// The transform that threw the exception. | 126 /// The transform that threw the exception. |
| 72 final TransformInfo transform; | 127 final TransformInfo transform; |
| 73 | 128 |
| 74 /// The wrapped exception. | 129 TransformerException(this.transform, error) |
| 75 final error; | 130 : super(error); |
| 76 | 131 |
| 77 TransformerException(this.transform, this.error); | 132 String get _message => "Transform $transform threw error"; |
| 78 | |
| 79 String toString() => "Transform $transform threw error: $error\n" + | |
| 80 new Trace.from(getAttachedStackTrace(error)).terse.toString(); | |
| 81 } | 133 } |
| 82 | 134 |
| 83 /// Error thrown when a source asset [id] fails to load. | 135 /// Error thrown when a source asset [id] fails to load. |
| 84 /// | 136 /// |
| 85 /// This can be thrown either because the source asset was expected to exist and | 137 /// This can be thrown either because the source asset was expected to exist and |
| 86 /// did not or because reading it failed somehow. | 138 /// did not or because reading it failed somehow. |
| 87 class AssetLoadException implements BarbackException { | 139 class AssetLoadException extends WrappedException { |
| 88 final AssetId id; | 140 final AssetId id; |
| 89 | 141 |
| 90 /// The wrapped exception. | 142 AssetLoadException(this.id, error) |
| 91 final error; | 143 : super(error); |
| 92 | 144 |
| 93 AssetLoadException(this.id, this.error); | 145 String get _message => "Failed to load source asset $id"; |
| 94 | |
| 95 String toString() => "Failed to load source asset $id: $error\n" | |
| 96 "${new Trace.from(getAttachedStackTrace(error)).terse}"; | |
| 97 } | 146 } |
| 98 | 147 |
| 99 /// Information about a single transform in the barback graph. | 148 /// Information about a single transform in the barback graph. |
| 100 /// | 149 /// |
| 101 /// Identifies a single transformation in the barback graph. | 150 /// Identifies a single transformation in the barback graph. |
| 102 /// | 151 /// |
| 103 /// A transformation is uniquely identified by the ID of its primary input, and | 152 /// A transformation is uniquely identified by the ID of its primary input, and |
| 104 /// the transformer that is applied to it. | 153 /// the transformer that is applied to it. |
| 105 class TransformInfo { | 154 class TransformInfo { |
| 106 /// The transformer that's run for this transform. | 155 /// The transformer that's run for this transform. |
| 107 final Transformer transformer; | 156 final Transformer transformer; |
| 108 | 157 |
| 109 /// The id of this transform's primary asset. | 158 /// The id of this transform's primary asset. |
| 110 final AssetId primaryId; | 159 final AssetId primaryId; |
| 111 | 160 |
| 112 TransformInfo(this.transformer, this.primaryId); | 161 TransformInfo(this.transformer, this.primaryId); |
| 113 | 162 |
| 114 bool operator==(other) => | 163 bool operator==(other) => |
| 115 other is TransformInfo && | 164 other is TransformInfo && |
| 116 other.transformer == transformer && | 165 other.transformer == transformer && |
| 117 other.primaryId == primaryId; | 166 other.primaryId == primaryId; |
| 118 | 167 |
| 119 int get hashCode => transformer.hashCode ^ primaryId.hashCode; | 168 int get hashCode => transformer.hashCode ^ primaryId.hashCode; |
| 120 | 169 |
| 121 String toString() => "$transformer on $primaryId"; | 170 String toString() => "$transformer on $primaryId"; |
| 122 } | 171 } |
| OLD | NEW |