Skip to content

Commit 4109d9e

Browse files
authored
Change AliasError to AliasException (dart-archive/yaml_edit#57)
1 parent ec7691b commit 4109d9e

File tree

7 files changed

+47
-23
lines changed

7 files changed

+47
-23
lines changed

pkgs/yaml_edit/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 2.2.0
2+
- `AliasError` is changed to `AliasException` and exposed in the public API.
3+
4+
All node-mutating methods on `YamlEditor`, i.e. `update()`, `appendToList()`,
5+
`prependToList()`, `insertIntoList()`, `spliceList()`, `remove()` will now
6+
throw an exception instead of an error when encountering an alias on the path
7+
to modify.
8+
9+
This allows catching and handling when this is happening.
10+
111
## 2.1.1
212

313
- Require Dart 2.19

pkgs/yaml_edit/lib/src/editor.dart

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class YamlEditor {
199199
///
200200
/// Throws a [ArgumentError] if [path] is invalid.
201201
///
202+
/// Throws an [AliasException] if a node on [path] is an alias or anchor.
203+
///
202204
/// **Example:** (using [update])
203205
/// ```dart
204206
/// final doc = YamlEditor('''
@@ -279,6 +281,8 @@ class YamlEditor {
279281
/// Throws a [ArgumentError] if the element at the given path is not a
280282
/// [YamlList] or if the path is invalid.
281283
///
284+
/// Throws an [AliasException] if a node on [path] is an alias or anchor.
285+
///
282286
/// **Example:**
283287
/// ```dart
284288
/// final doc = YamlEditor('[0, 1]');
@@ -295,6 +299,8 @@ class YamlEditor {
295299
/// Throws a [ArgumentError] if the element at the given path is not a
296300
/// [YamlList] or if the path is invalid.
297301
///
302+
/// Throws an [AliasException] if a node on [path] is an alias or anchor.
303+
///
298304
/// **Example:**
299305
/// ```dart
300306
/// final doc = YamlEditor('[1, 2]');
@@ -311,6 +317,8 @@ class YamlEditor {
311317
/// Throws a [ArgumentError] if the element at the given path is not a
312318
/// [YamlList] or if the path is invalid.
313319
///
320+
/// Throws an [AliasException] if a node on [path] is an alias or anchor.
321+
///
314322
/// **Example:**
315323
/// ```dart
316324
/// final doc = YamlEditor('[0, 2]');
@@ -340,6 +348,8 @@ class YamlEditor {
340348
/// Throws a [ArgumentError] if the element at the given path is not a
341349
/// [YamlList] or if the path is invalid.
342350
///
351+
/// Throws an [AliasException] if a node on [path] is an alias or anchor.
352+
///
343353
/// **Example:**
344354
/// ```dart
345355
/// final doc = YamlEditor('[Jan, March, April, June]');
@@ -375,7 +385,9 @@ class YamlEditor {
375385
/// Removes the node at [path]. Comments "belonging" to the node will be
376386
/// removed while surrounding comments will be left untouched.
377387
///
378-
/// Throws a [ArgumentError] if [path] is invalid.
388+
/// Throws an [ArgumentError] if [path] is invalid.
389+
///
390+
/// Throws an [AliasException] if a node on [path] is an alias or anchor.
379391
///
380392
/// **Example:**
381393
/// ```dart
@@ -441,7 +453,7 @@ class YamlEditor {
441453
///
442454
/// If [orElse] is omitted, it defaults to throwing a [PathError].
443455
///
444-
/// If [checkAlias] is `true`, throw [AliasError] if an aliased node is
456+
/// If [checkAlias] is `true`, throw [AliasException] if an aliased node is
445457
/// encountered.
446458
YamlNode _traverse(Iterable<Object?> path,
447459
{bool checkAlias = false, YamlNode Function()? orElse}) {
@@ -454,7 +466,7 @@ class YamlEditor {
454466
final keyOrIndex = pathList[i];
455467

456468
if (checkAlias && _aliases.contains(currentNode)) {
457-
throw AliasError(path, currentNode);
469+
throw AliasException(path, currentNode);
458470
}
459471

460472
if (currentNode is YamlList) {
@@ -473,7 +485,7 @@ class YamlEditor {
473485
final keyNode = getKeyNode(map, keyOrIndex);
474486

475487
if (checkAlias) {
476-
if (_aliases.contains(keyNode)) throw AliasError(path, keyNode);
488+
if (_aliases.contains(keyNode)) throw AliasException(path, keyNode);
477489
}
478490

479491
currentNode = map.nodes[keyNode]!;
@@ -498,7 +510,7 @@ class YamlEditor {
498510
/// Asserts that [node] and none its children are aliases
499511
void _assertNoChildAlias(Iterable<Object?> path, [YamlNode? node]) {
500512
if (node == null) return _assertNoChildAlias(path, _traverse(path));
501-
if (_aliases.contains(node)) throw AliasError(path, node);
513+
if (_aliases.contains(node)) throw AliasException(path, node);
502514

503515
if (node is YamlScalar) return;
504516

@@ -514,7 +526,7 @@ class YamlEditor {
514526
for (var i = 0; i < node.length; i++) {
515527
final updatedPath = [...path, keyList[i]];
516528
if (_aliases.contains(keyList[i])) {
517-
throw AliasError(path, keyList[i] as YamlNode);
529+
throw AliasException(path, keyList[i] as YamlNode);
518530
}
519531
_assertNoChildAlias(updatedPath, node.nodes[keyList[i]]);
520532
}
@@ -527,9 +539,10 @@ class YamlEditor {
527539
///
528540
/// Throws [ArgumentError] if the element at the given path is not a
529541
/// [YamlList] or if the path is invalid. If [checkAlias] is `true`, and an
530-
/// aliased node is encountered along [path], an [AliasError] will be thrown.
542+
/// aliased node is encountered along [path], an [AliasException] will be
543+
/// thrown.
531544
YamlList _traverseToList(Iterable<Object?> path, {bool checkAlias = false}) {
532-
final possibleList = _traverse(path, checkAlias: true);
545+
final possibleList = _traverse(path, checkAlias: checkAlias);
533546

534547
if (possibleList is YamlList) {
535548
return possibleList;

pkgs/yaml_edit/lib/src/errors.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class PathError extends ArgumentError {
4141
}
4242
}
4343

44-
/// Error thrown when the path contains an alias along the way.
44+
/// Exception thrown when the path contains an alias along the way.
4545
///
4646
/// When a path contains an aliased node, the behavior becomes less well-defined
4747
/// because we cannot be certain if the user wishes for the change to
@@ -50,14 +50,14 @@ class PathError extends ArgumentError {
5050
/// the detection that our change will impact an alias, and we do not intend
5151
/// on supporting such changes for the foreseeable future.
5252
@sealed
53-
class AliasError extends UnsupportedError {
53+
class AliasException extends FormatException {
5454
/// The path that caused the error
5555
final Iterable<Object?> path;
5656

5757
/// The anchor node of the alias
5858
final YamlNode anchor;
5959

60-
AliasError(this.path, this.anchor)
60+
AliasException(this.path, this.anchor)
6161
: super('Encountered an alias node along $path! '
6262
'Alias nodes are nodes that refer to a previously serialized '
6363
'nodes, and are denoted by either the "*" or the "&" indicators in '

pkgs/yaml_edit/lib/yaml_edit.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
library yaml_edit;
2424

2525
export 'src/editor.dart';
26+
export 'src/errors.dart' show AliasException;
2627
export 'src/source_edit.dart';
2728
export 'src/wrap.dart' show wrapAsYamlNode;

pkgs/yaml_edit/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: yaml_edit
2-
version: 2.1.1
2+
version: 2.2.0
33
description: A library for YAML manipulation with comment and whitespace preservation.
44
repository: https://github.com/dart-lang/yaml_edit
55
issue_tracker: https://github.com/dart-lang/yaml_edit/issues

pkgs/yaml_edit/test/alias_test.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void main() {
1616
- &SS Sammy Sosa
1717
- *SS
1818
''');
19-
expect(() => doc.remove([0]), throwsAliasError);
19+
expect(() => doc.remove([0]), throwsAliasException);
2020
});
2121

2222
test('removing an alias reference results in AliasError', () {
@@ -25,7 +25,7 @@ void main() {
2525
- *SS
2626
''');
2727

28-
expect(() => doc.remove([1]), throwsAliasError);
28+
expect(() => doc.remove([1]), throwsAliasException);
2929
});
3030

3131
test('it is okay to remove a non-alias node', () {
@@ -50,7 +50,7 @@ a: &SS Sammy Sosa
5050
b: *SS
5151
''');
5252

53-
expect(() => doc.remove(['a']), throwsAliasError);
53+
expect(() => doc.remove(['a']), throwsAliasException);
5454
});
5555

5656
test('removing an alias reference value results in AliasError', () {
@@ -59,7 +59,7 @@ a: &SS Sammy Sosa
5959
b: *SS
6060
''');
6161

62-
expect(() => doc.remove(['b']), throwsAliasError);
62+
expect(() => doc.remove(['b']), throwsAliasException);
6363
});
6464

6565
test('removing an alias anchor key results in AliasError', () {
@@ -68,7 +68,7 @@ b: *SS
6868
b: *SS
6969
''');
7070

71-
expect(() => doc.remove(['Sammy Sosa']), throwsAliasError);
71+
expect(() => doc.remove(['Sammy Sosa']), throwsAliasException);
7272
});
7373

7474
test('removing an alias reference key results in AliasError', () {
@@ -77,7 +77,7 @@ a: &SS Sammy Sosa
7777
*SS : b
7878
''');
7979

80-
expect(() => doc.remove(['Sammy Sosa']), throwsAliasError);
80+
expect(() => doc.remove(['Sammy Sosa']), throwsAliasException);
8181
});
8282

8383
test('it is okay to remove a non-alias node', () {
@@ -103,7 +103,7 @@ b: *SS
103103
- *SS
104104
''');
105105

106-
expect(() => doc.remove([0]), throwsAliasError);
106+
expect(() => doc.remove([0]), throwsAliasException);
107107
});
108108

109109
test('nested list alias references are detected too', () {
@@ -113,7 +113,7 @@ b: *SS
113113
- *SS
114114
''');
115115

116-
expect(() => doc.remove([1]), throwsAliasError);
116+
expect(() => doc.remove([1]), throwsAliasException);
117117
});
118118

119119
test('removing nested map alias anchor results in AliasError', () {
@@ -123,7 +123,7 @@ a:
123123
b: *SS
124124
''');
125125

126-
expect(() => doc.remove(['a']), throwsAliasError);
126+
expect(() => doc.remove(['a']), throwsAliasException);
127127
});
128128

129129
test('removing nested map alias reference results in AliasError', () {
@@ -133,7 +133,7 @@ b:
133133
c: *SS
134134
''');
135135

136-
expect(() => doc.remove(['b']), throwsAliasError);
136+
expect(() => doc.remove(['b']), throwsAliasException);
137137
});
138138
});
139139
}

pkgs/yaml_edit/test/test_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Matcher notEquals(dynamic expected) => isNot(equals(expected));
3232
Matcher throwsPathError = throwsA(isA<PathError>());
3333

3434
/// A matcher for functions that throw [AliasError].
35-
Matcher throwsAliasError = throwsA(isA<AliasError>());
35+
Matcher throwsAliasException = throwsA(isA<AliasException>());
3636

3737
/// Enum to hold the possible modification methods.
3838
enum YamlModificationMethod {

0 commit comments

Comments
 (0)