Skip to content

Commit d3201fb

Browse files
authored
Less verbose error for analysis error (#142)
The AnalysisException thrown during initial iterating over elements can be very noisy. Stack traces are included in the `toString()` and it's detail that most users won't care about. - Add a safeIterate utility method which allows catching the exceptions specifically thrown by the elements iterable and not by the Builder - Simplify to a message about failing to resolve the input. Include the extra details in a log.fine - Require sdk 1.21.1 for generic method syntax
1 parent 466bf4f commit d3201fb

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 0.5.4+4
1+
## 0.5.5
22

33
* Support package:build 0.8.x
4+
* Less verbose errors when analyzer fails to resolve the input.
45

56
## 0.5.4+3
67

lib/src/builder.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import 'dart:async';
55

66
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/exception/exception.dart';
78
import 'package:build/build.dart';
89
import 'package:dart_style/src/dart_formatter.dart';
910

@@ -98,7 +99,11 @@ class GeneratorBuilder extends Builder {
9899

99100
Stream<GeneratedOutput> _generate(LibraryElement unit,
100101
List<Generator> generators, BuildStep buildStep) async* {
101-
for (var element in getElementsFromLibraryElement(unit)) {
102+
var elements = safeIterate(getElementsFromLibraryElement(unit), (e, [_]) {
103+
log.fine('Resolve error details:\n$e');
104+
log.severe('Failed to resolve ${buildStep.inputId}.');
105+
});
106+
for (var element in elements) {
102107
yield* _processUnitMember(element, generators, buildStep);
103108
}
104109
}

lib/src/utils.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ Iterable<Element> getElementsFromLibraryElement(LibraryElement unit) sync* {
4949
}
5050
}
5151

52+
/// Returns an Iterable that will not throw any exceptions while iterating.
53+
///
54+
/// If an exception occurs while iterating [iterable] the return value will
55+
/// finish and [onError] will be called.
56+
Iterable<T> safeIterate<T>(Iterable<T> iterable, void onError(e, st)) sync* {
57+
try {
58+
for (var e in iterable) {
59+
yield e;
60+
}
61+
} catch (e, st) {
62+
onError(e, st);
63+
}
64+
}
65+
5266
Iterable<Element> _getElements(CompilationUnitMember member) {
5367
if (member is TopLevelVariableDeclaration) {
5468
return member.variables.variables

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: source_gen
2-
version: 0.5.4+4
2+
version: 0.5.5
33
author: Dart Team <[email protected]>
44
description: Automatic sourcecode generation for Dart
55
homepage: https://github.com/dart-lang/source_gen
66
environment:
7-
sdk: '>=1.12.0 <2.0.0'
7+
sdk: '>=1.21.1 <2.0.0'
88
dependencies:
99
analyzer: ^0.29.2
1010
build: '>=0.7.1 <0.9.0'

0 commit comments

Comments
 (0)