runSafely function
Run the given action in a safe try/catch block, allowing Dartle to handle any errors by logging the appropriate build failure.
If doNotExit
is true
, then this method will not call exit on build
completion, re-throwing Exceptions. Otherwise, the process will exit with
0 on success, or the appropriate error code on error.
Implementation
Future<void> runSafely(List<String> args, bool doNotExit,
FutureOr<void> Function(Stopwatch, Options) action) async {
final stopWatch = Stopwatch()..start();
var options = const Options();
try {
options = parseOptions(args);
await action(stopWatch, options);
if (!doNotExit) abort(0);
} on DartleException catch (e) {
// activate SEVERE logging in case logging was not enabled yet
activateLogging(log.Level.SEVERE);
logger.severe(e.message);
if (logger.isLoggable(log.Level.FINE)) {
_logStackTrace(e);
}
if (options.logBuildTime) {
logger.severe(ColoredLogMessage(
'✗ Build failed in ${elapsedTime(stopWatch)}', LogColor.red));
}
if (doNotExit) {
rethrow;
} else {
abort(e.exitCode);
}
} on Exception catch (e, st) {
// activate SEVERE logging in case logging was not enabled yet
activateLogging(log.Level.SEVERE);
logger.severe('Unexpected error', e, st);
if (options.logBuildTime) {
logger.severe(ColoredLogMessage(
'✗ Build failed in ${elapsedTime(stopWatch)}', LogColor.red));
}
if (doNotExit) {
rethrow;
} else {
abort(22);
}
}
}