5
5
// See the Mustachio README at tool/mustachio/README.md for high-level
6
6
// documentation.
7
7
8
+ /// @docImport 'package:source_span/source_span.dart';
9
+ library ;
10
+
8
11
import 'dart:collection' ;
9
12
import 'dart:convert' show htmlEscape;
10
13
11
14
import 'package:analyzer/file_system/file_system.dart' ;
12
15
import 'package:meta/meta.dart' ;
16
+
13
17
import 'parser.dart' ;
14
18
15
19
// TODO(devoncarew): See is we can make this synchronous.
@@ -119,7 +123,7 @@ class Template {
119
123
partialTemplates: {...partialTemplates});
120
124
partialTemplates[partialFile] = partialTemplate;
121
125
} on FileSystemException catch (e) {
122
- throw MustachioResolutionError (span.message (
126
+ throw MustachioResolutionException (span.message (
123
127
'FileSystemException (${e .message }) when reading partial:' ));
124
128
}
125
129
}
@@ -188,27 +192,26 @@ abstract class RendererBase<T extends Object?> {
188
192
if (property != null ) {
189
193
try {
190
194
return property.renderVariable (context, property, remainingNames);
191
- // ignore: avoid_catching_errors
192
- } on PartialMustachioResolutionError catch (e) {
193
- // The error thrown by [Property.renderVariable] does not have all of
194
- // the names required for a decent error. We throw a new error here.
195
- throw MustachioResolutionError (node.keySpan.message (
195
+ } on PartialMustachioResolutionException catch (e) {
196
+ // The exception thrown by [Property.renderVariable] does not have all
197
+ // of the names required for a decent error. We throw a new error
198
+ // here.
199
+ throw MustachioResolutionException (node.keySpan.message (
196
200
"Failed to resolve '${e .name }' on ${e .contextType } while "
197
201
'resolving $remainingNames as a property chain on any types in '
198
202
'the context chain: $contextChainString , after first resolving '
199
203
"'$firstName ' to a property on $T " ));
200
204
}
201
205
}
202
- // ignore: avoid_catching_errors
203
- } on _MustachioResolutionErrorWithoutSpan catch (e) {
204
- throw MustachioResolutionError (node.keySpan.message (e.message));
206
+ } on _MustachioResolutionExceptionWithoutSpan catch (e) {
207
+ throw MustachioResolutionException (node.keySpan.message (e.message));
205
208
}
206
209
207
210
final parent = this .parent;
208
211
if (parent != null ) {
209
212
return parent.getFields (node);
210
213
} else {
211
- throw MustachioResolutionError (node.keySpan.message (
214
+ throw MustachioResolutionException (node.keySpan.message (
212
215
"Failed to resolve '${names .first }' as a property on any types in "
213
216
'the context chain: $contextChainString ' ));
214
217
}
@@ -241,7 +244,7 @@ abstract class RendererBase<T extends Object?> {
241
244
if (property == null ) {
242
245
final parent = this .parent;
243
246
if (parent == null ) {
244
- throw MustachioResolutionError (node.keySpan.message (
247
+ throw MustachioResolutionException (node.keySpan.message (
245
248
"Failed to resolve '$key ' as a property on any types in the "
246
249
'current context' ));
247
250
} else {
@@ -312,7 +315,7 @@ class SimpleRenderer extends RendererBase<Object?> {
312
315
@override
313
316
Property <Object >? getProperty (String key) {
314
317
if (_invisibleGetters.contains (key)) {
315
- throw MustachioResolutionError (_failedKeyVisibilityMessage (key));
318
+ throw MustachioResolutionException (_failedKeyVisibilityMessage (key));
316
319
} else {
317
320
return null ;
318
321
}
@@ -325,7 +328,8 @@ class SimpleRenderer extends RendererBase<Object?> {
325
328
if (names.length == 1 && firstName == '.' ) {
326
329
return context.toString ();
327
330
} else if (_invisibleGetters.contains (firstName)) {
328
- throw MustachioResolutionError (_failedKeyVisibilityMessage (firstName));
331
+ throw MustachioResolutionException (
332
+ _failedKeyVisibilityMessage (firstName));
329
333
} else if (parent != null ) {
330
334
return parent! .getFields (node);
331
335
} else {
@@ -380,7 +384,7 @@ class Property<T extends Object?> {
380
384
if (remainingNames.isEmpty) {
381
385
return getValue (c).toString ();
382
386
} else {
383
- throw MustachioResolutionError (
387
+ throw MustachioResolutionException (
384
388
_simpleResolveErrorMessage (remainingNames, typeString));
385
389
}
386
390
}
@@ -391,43 +395,43 @@ class Property<T extends Object?> {
391
395
"annotation's 'visibleTypes' list" ;
392
396
}
393
397
394
- /// An error indicating that a renderer failed to resolve a key.
395
- class MustachioResolutionError extends Error {
398
+ /// An exception indicating that a renderer failed to resolve a key.
399
+ class MustachioResolutionException implements Exception {
396
400
final String message;
397
401
398
- MustachioResolutionError (this .message);
402
+ MustachioResolutionException (this .message);
399
403
400
404
@override
401
- String toString () => 'MustachioResolutionError : $message ' ;
405
+ String toString () => 'MustachioResolutionException : $message ' ;
402
406
}
403
407
404
- /// An error indicating that a renderer failed to resolve a follow-on name in a
405
- /// multi-name key.
406
- class PartialMustachioResolutionError extends Error {
408
+ /// An exception indicating that a renderer failed to resolve a follow-on name
409
+ /// in a multi-name key.
410
+ class PartialMustachioResolutionException implements Exception {
407
411
final String name;
408
412
409
413
final Type contextType;
410
414
411
- PartialMustachioResolutionError (this .name, this .contextType);
415
+ PartialMustachioResolutionException (this .name, this .contextType);
412
416
}
413
417
414
- /// A Mustachio resolution error which is thrown in a position where the AST
415
- /// node is not known.
418
+ /// A Mustachio resolution exception which is thrown in a position where the
419
+ /// AST node is not known.
416
420
///
417
- /// This error should be caught and "re-thrown" as a [MustachioResolutionError]
418
- /// with a message derived from a [SourceSpan] .
419
- class _MustachioResolutionErrorWithoutSpan extends Error {
421
+ /// This exception should be caught and "re-thrown" as a
422
+ /// [MustachioResolutionException] with a message derived from a [SourceSpan] .
423
+ class _MustachioResolutionExceptionWithoutSpan implements Exception {
420
424
final String message;
421
425
422
- _MustachioResolutionErrorWithoutSpan (this .message);
426
+ _MustachioResolutionExceptionWithoutSpan (this .message);
423
427
}
424
428
425
429
extension MapExtensions <T > on Map <String , Property <T >> {
426
430
Property <T > getValue (String name) {
427
431
if (containsKey (name)) {
428
432
return this [name]! ;
429
433
} else {
430
- throw PartialMustachioResolutionError (name, T );
434
+ throw PartialMustachioResolutionException (name, T );
431
435
}
432
436
}
433
437
}
0 commit comments