Skip to content

Commit

Permalink
Made ParserError a FormatException to follow typical Dart exception s…
Browse files Browse the repository at this point in the history
…tyle.
  • Loading branch information
renggli committed Aug 29, 2019
1 parent aecb962 commit afc9a82
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 2.5.0

* Made ParserError a FormatException to follow typical Dart exception style.

## 2.4.0

* Dart 2.4 compatibility and requirement.
Expand Down
11 changes: 10 additions & 1 deletion petitparser/lib/src/core/contexts/exception.dart
Expand Up @@ -3,11 +3,20 @@ library petitparser.core.contexts.exception;
import 'package:petitparser/src/core/contexts/failure.dart';

/// An exception raised in case of a parse error.
class ParserException implements Exception {
class ParserException implements FormatException {
final Failure failure;

ParserException(this.failure);

@override
String get message => failure.message;

@override
int get offset => failure.position;

@override
String get source => failure.buffer;

@override
String toString() => '${failure.message} at ${failure.toPositionString()}';
}
9 changes: 9 additions & 0 deletions petitparser/test/petitparser_test.dart
Expand Up @@ -985,6 +985,9 @@ void main() {
fail('Expected ParserError to be thrown');
} on ParserException catch (error) {
expect(error.failure, same(failure));
expect(error.message, 'error');
expect(error.offset, 0);
expect(error.source, buffer);
expect(error.toString(), 'error at 1:1');
}
expect(failure.message, 'error');
Expand All @@ -1001,6 +1004,9 @@ void main() {
fail('Expected ParserError to be thrown');
} on ParserException catch (error) {
expect(error.failure, same(failure));
expect(error.message, 'error');
expect(error.offset, 2);
expect(error.source, buffer);
expect(error.toString(), 'error at 2:1');
}
expect(failure.message, 'error');
Expand All @@ -1019,6 +1025,9 @@ void main() {
fail('Expected ParserError to be thrown');
} on ParserException catch (error) {
expect(error.failure, same(failure));
expect(error.message, 'error');
expect(error.offset, 2);
expect(error.source, buffer);
expect(error.toString(), 'error at 2:1');
}
expect(failure.message, 'error');
Expand Down

0 comments on commit afc9a82

Please sign in to comment.