Skip to content

Commit

Permalink
Make InvalidGenerationSourceError an Exception (#687)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natebosch authored Oct 5, 2023
1 parent 3c522e2 commit c8d8873
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

## 1.4.0

Expand Down
3 changes: 2 additions & 1 deletion source_gen/lib/source_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export 'src/builder.dart'
show LibraryBuilder, PartBuilder, SharedPartBuilder, defaultFileHeader;
export 'src/constants/reader.dart' show ConstantReader;
export 'src/constants/revive.dart' show Revivable;
export 'src/generator.dart' show Generator, InvalidGenerationSourceError;
export 'src/generator.dart'
show Generator, InvalidGenerationSource, InvalidGenerationSourceError;
export 'src/generator_for_annotation.dart' show GeneratorForAnnotation;
export 'src/library.dart' show AnnotatedElement, LibraryReader;
export 'src/span_for_element.dart' show spanForElement;
Expand Down
11 changes: 8 additions & 3 deletions source_gen/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ abstract class Generator {
String toString() => runtimeType.toString();
}

/// May be thrown by generators during [Generator.generate].
class InvalidGenerationSourceError extends Error {
typedef InvalidGenerationSourceError = InvalidGenerationSource;

/// A description of a problem in the source input to code generation.
///
/// May be thrown by generators during [Generator.generate] to communicate a
/// problem to the codegen user.
class InvalidGenerationSource implements Exception {
/// What failure occurred.
final String message;

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

InvalidGenerationSourceError(
InvalidGenerationSource(
this.message, {
this.todo = '',
this.element,
Expand Down

0 comments on commit c8d8873

Please sign in to comment.