Skip to content

Commit c8d8873

Browse files
authored
Make InvalidGenerationSourceError an Exception (#687)
See dart-lang/build#3585 Rename to `InvalidGenerationSource` and change to a subtype of `Exception`. This class is not thrown to indicate a programming error in the code that is running, it is thrown to indicate an error in the code under analysis. It was originally implemented without a super type and the name ending in "Error" was not specifically discussed, but it was intended to convey an "error in build input", not an error in the builder implementation (where it is thrown). Later a lint required it to be a subtype of either `Error` or `Exception`, and the `Error` supertype was chosen without discussion because of the name, even though it's not thrown as an `Error` in the sense where that distinction matters. Add a type alias for the exception with the old name. This can be deprecated whenever we have the bandwidth to handle the cleanup, but it will not be deprecated immediately. Change from `extends Error` to `implements Exception`. This is technically breaking, but it is unlikely to make a significant impact in real world usage scenarios. It may impact how the error surfaces to end users in some places.
1 parent 3c522e2 commit c8d8873

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

source_gen/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Add `throwOnUnresolved` configuration to the `GeneratorForAnnotation`
44
constructor.
5+
- Rename `InvalidGenerationSourceError` to `InvalidGenerationSource`. Change
6+
from a subtype of `Error` to a subtype of `Exception`. This may be breaking if
7+
a builder relies on a `on Exception catch` to ignore this error.
58

69
## 1.4.0
710

source_gen/lib/source_gen.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export 'src/builder.dart'
66
show LibraryBuilder, PartBuilder, SharedPartBuilder, defaultFileHeader;
77
export 'src/constants/reader.dart' show ConstantReader;
88
export 'src/constants/revive.dart' show Revivable;
9-
export 'src/generator.dart' show Generator, InvalidGenerationSourceError;
9+
export 'src/generator.dart'
10+
show Generator, InvalidGenerationSource, InvalidGenerationSourceError;
1011
export 'src/generator_for_annotation.dart' show GeneratorForAnnotation;
1112
export 'src/library.dart' show AnnotatedElement, LibraryReader;
1213
export 'src/span_for_element.dart' show spanForElement;

source_gen/lib/src/generator.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ abstract class Generator {
3030
String toString() => runtimeType.toString();
3131
}
3232

33-
/// May be thrown by generators during [Generator.generate].
34-
class InvalidGenerationSourceError extends Error {
33+
typedef InvalidGenerationSourceError = InvalidGenerationSource;
34+
35+
/// A description of a problem in the source input to code generation.
36+
///
37+
/// May be thrown by generators during [Generator.generate] to communicate a
38+
/// problem to the codegen user.
39+
class InvalidGenerationSource implements Exception {
3540
/// What failure occurred.
3641
final String message;
3742

@@ -52,7 +57,7 @@ class InvalidGenerationSourceError extends Error {
5257
/// code, or if the location was passed with [element].
5358
final AstNode? node;
5459

55-
InvalidGenerationSourceError(
60+
InvalidGenerationSource(
5661
this.message, {
5762
this.todo = '',
5863
this.element,

0 commit comments

Comments
 (0)