Skip to content

Commit

Permalink
ErrorResult::handler fix
Browse files Browse the repository at this point in the history
  • Loading branch information
PROGrand committed Jan 28, 2025
1 parent 20679ac commit a546211
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkgs/async/lib/src/result/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ class ErrorResult implements Result<Never> {
/// has to be a function expecting only one argument, which will be called
/// with only the error.
void handle(Function errorHandler) {
if (errorHandler is ZoneBinaryCallback) {
if (errorHandler is dynamic Function(Object, StackTrace)) {
errorHandler(error, stackTrace);
} else if (errorHandler is dynamic Function(Object)) {
errorHandler(error);
} else {
(errorHandler as ZoneUnaryCallback)(error);
throw ArgumentError(
'is nor Function(Object, StackTrace) neither Function(Object)',
'errorHandler',
);
}
}

Expand Down
23 changes: 23 additions & 0 deletions pkgs/async/test/result/result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,29 @@ void main() {
expect(called, isTrue);
});

test('handle typed binary', () {
final result = ErrorResult('error', stack);
var called = false;
void f(Object error, StackTrace stackTrace) {
called = true;
}

result.handle(f);
expect(called, isTrue);
});

test('handle typed unary', () {
final result = ErrorResult('error', stack);
var called = false;

void f(Object error) {
called = true;
}

result.handle(f);
expect(called, isTrue);
});

test('handle unary and binary', () {
var result = ErrorResult('error', stack);
var called = false;
Expand Down

0 comments on commit a546211

Please sign in to comment.