From 639af48fa58baf2c9f612ed9208f0254aad807f5 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Thu, 5 Oct 2023 12:15:55 -0700 Subject: [PATCH] Make InvalidGenerationSourceError an Exception (dart-lang/source_gen#687) See https://github.com/dart-lang/build/issues/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. --- pkgs/source_gen/source_gen/CHANGELOG.md | 3 +++ pkgs/source_gen/source_gen/lib/source_gen.dart | 3 ++- pkgs/source_gen/source_gen/lib/src/generator.dart | 11 ++++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pkgs/source_gen/source_gen/CHANGELOG.md b/pkgs/source_gen/source_gen/CHANGELOG.md index 596c40dcb..e6f21c162 100644 --- a/pkgs/source_gen/source_gen/CHANGELOG.md +++ b/pkgs/source_gen/source_gen/CHANGELOG.md @@ -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 diff --git a/pkgs/source_gen/source_gen/lib/source_gen.dart b/pkgs/source_gen/source_gen/lib/source_gen.dart index b695732e2..af399f77c 100644 --- a/pkgs/source_gen/source_gen/lib/source_gen.dart +++ b/pkgs/source_gen/source_gen/lib/source_gen.dart @@ -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; diff --git a/pkgs/source_gen/source_gen/lib/src/generator.dart b/pkgs/source_gen/source_gen/lib/src/generator.dart index db5ad62ff..b29121c45 100644 --- a/pkgs/source_gen/source_gen/lib/src/generator.dart +++ b/pkgs/source_gen/source_gen/lib/src/generator.dart @@ -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; @@ -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,