Files
sdk/pkg/testing/lib/src/error_handling.dart
T
Johnni Winther 0073b55575 [testing] Migrate pkg/testing to null safety
Change-Id: I64caff0a9163305ff122965105e7484c05b9cafb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/207138
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
2021-07-27 19:45:38 +00:00

32 lines
808 B
Dart

// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.
library testing.error_handling;
import 'dart:async' show Future;
import 'dart:io' show exitCode, stderr;
import 'dart:isolate' show ReceivePort;
import 'log.dart';
Future<T?> withErrorHandling<T>(Future<T> f(), {Logger? logger}) async {
final ReceivePort port = new ReceivePort();
try {
return await f();
} catch (e, trace) {
exitCode = 1;
stderr.writeln(e);
// ignore: unnecessary_null_comparison
if (trace != null) {
stderr.writeln(trace);
}
logger?.noticeFrameworkCatchError(e, trace);
return null;
} finally {
port.close();
}
}