Skip to content

Commit

Permalink
fix: Added runGuardedErrorValidation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jan 22, 2024
1 parent 5e0edab commit aa3a5bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/katana/lib/katana.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
library katana;

// Dart imports:
import 'dart:async';
import 'dart:convert';
import 'dart:core' as core;
import 'dart:core';
Expand Down
27 changes: 27 additions & 0 deletions packages/katana/lib/src/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,30 @@ bool jsonEncodable(Object? o) {
}
return o is num || o is bool || o is String;
}

/// Verify errors including Async.
///
/// The process that raises an Exception is passed to [process], and [onError] is passed what to do when the Exception is raised.
///
/// Asyncを含むエラーの検証を行います。
///
/// [process]にExceptionを発生させる処理を渡し、[onError]にはExceptionが発生した場合の処理を渡します。
Future<void> runGuardedErrorValidation(
FutureOr<void> Function() process,
void Function(Object error, StackTrace stackTrace) onError,
) async {
Completer<void>? completer = Completer();
runZonedGuarded(
() async {
await process();
completer?.completeError("Should not be here");
completer = null;
},
(error, stack) {
completer?.complete();
completer = null;
onError(error, stack);
},
);
await completer?.future;
}

0 comments on commit aa3a5bc

Please sign in to comment.