From c4a0c7c17b3c45d7483458fc6080115c58d47540 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 23 Feb 2024 14:07:12 -0800 Subject: [PATCH] Improve SubprocessException --- tool/src/subprocess_launcher.dart | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tool/src/subprocess_launcher.dart b/tool/src/subprocess_launcher.dart index aa07b6cffb..f1cfe5b5cc 100644 --- a/tool/src/subprocess_launcher.dart +++ b/tool/src/subprocess_launcher.dart @@ -6,8 +6,6 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; -class SubprocessException implements Exception {} - class SubprocessLauncher { final String context; final Map defaultEnvironment; @@ -121,15 +119,19 @@ class SubprocessLauncher { workingDirectory: workingDirectory, environment: environment, includeParentEnvironment: includeParentEnvironment); + // Stream stdout and stderr to _this_ process's stdout and stderr. var stdoutFuture = _printStream(process.stdout, stdout, prefix: prefix, filter: jsonCallback); var stderrFuture = _printStream(process.stderr, stderr, prefix: prefix, filter: jsonCallback); - await Future.wait([stderrFuture, stdoutFuture, process.exitCode]); + var (_, _, exitCode) = + await (stdoutFuture, stderrFuture, process.exitCode).wait; - var exitCode = await process.exitCode; if (exitCode != 0) { - throw SubprocessException(); + throw SubprocessException( + command: [executable, ...arguments].join(' '), + workingDirectory: workingDirectory, + exitCode: exitCode); } return jsonObjects; } @@ -174,3 +176,21 @@ class SubprocessLauncher { static final _quotables = RegExp(r'[ "\r\n\$]'); } + +/// An exception that represents an issue during subprocess execution. +class SubprocessException implements Exception { + final String command; + final String? workingDirectory; + final int exitCode; + + SubprocessException( + {required this.command, + required this.workingDirectory, + required this.exitCode}); + + @override + String toString() => 'SubprocessException[' + 'command: "$command", ' + 'workingDirectory: "${workingDirectory ?? '(null)'}", ' + 'exitCode: $exitCode]'; +}