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