Skip to content

Commit 876b110

Browse files
committed
Improved error output when unable to create an instance from an annotation.
1 parent b6bc20e commit 876b110

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

CHANGELOG.md

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

33
* Added support for `Symbol` and `Type` in annotations.
44

5+
* Improved error output when unable to create an instance from an annotation.
6+
57
## 0.4.7+2
68

79
* Upgrade to `analyzer '^0.27.1'` and removed a work-around for a fixed `analyzer` issue.

lib/src/annotation.dart

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import 'package:path/path.dart' as p;
1616

1717
dynamic instantiateAnnotation(ElementAnnotationImpl annotation) {
1818
var annotationObjectImpl = annotation.evaluationResult.value;
19-
if (annotationObjectImpl.hasKnownValue) {
20-
try {
21-
return _getValue(
22-
annotationObjectImpl, annotation.element.context.typeProvider);
23-
} on CannotCreateFromAnnotationException catch (_) {
24-
// NOOP
19+
try {
20+
return _getValue(
21+
annotationObjectImpl, annotation.element.context.typeProvider);
22+
} on CannotCreateFromAnnotationException catch (e) {
23+
if (e.innerException != null) {
24+
// If there was a issue creating a nested object, there's not much we
25+
// can do.
26+
rethrow;
2527
}
2628
}
2729

@@ -81,34 +83,48 @@ dynamic _getValue(DartObject object, TypeProvider typeProvider) {
8183
object, 'object', "Provided type value is not supported.");
8284
}
8385

84-
var listValue = object.toListValue();
85-
if (listValue != null) {
86-
return listValue
87-
.map((DartObject element) => _getValue(element, typeProvider))
88-
.toList();
89-
}
86+
try {
87+
var listValue = object.toListValue();
88+
if (listValue != null) {
89+
return listValue
90+
.map((DartObject element) => _getValue(element, typeProvider))
91+
.toList();
92+
}
9093

91-
var mapValue = object.toMapValue();
92-
if (mapValue != null) {
93-
var result = {};
94-
mapValue.forEach((DartObject key, DartObject value) {
95-
dynamic mappedKey = _getValue(key, typeProvider);
96-
if (mappedKey != null) {
97-
result[mappedKey] = _getValue(value, typeProvider);
98-
}
99-
});
100-
return result;
94+
var mapValue = object.toMapValue();
95+
if (mapValue != null) {
96+
var result = {};
97+
mapValue.forEach((DartObject key, DartObject value) {
98+
dynamic mappedKey = _getValue(key, typeProvider);
99+
if (mappedKey != null) {
100+
result[mappedKey] = _getValue(value, typeProvider);
101+
}
102+
});
103+
return result;
104+
}
105+
} on CannotCreateFromAnnotationException catch (e) {
106+
throw new CannotCreateFromAnnotationException._(object, e);
101107
}
102108

103109
throw new CannotCreateFromAnnotationException._(object);
104110
}
105111

106112
class CannotCreateFromAnnotationException {
107113
final DartObject object;
114+
final CannotCreateFromAnnotationException innerException;
108115

109-
CannotCreateFromAnnotationException._(this.object);
116+
CannotCreateFromAnnotationException._(this.object, [this.innerException]);
110117

111-
String toString() => "Cannot create object from $object";
118+
String toString() {
119+
var buffer = new StringBuffer("Cannot create object from ${object}");
120+
121+
if (innerException != null) {
122+
buffer.writeln();
123+
buffer.write('due to inner object: $innerException');
124+
}
125+
126+
return buffer.toString();
127+
}
112128
}
113129

114130
final _cannotCreate = new Object();

0 commit comments

Comments
 (0)