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