Skip to content

Commit ec9c8e0

Browse files
authored
Improve SubprocessException (#3679)
1 parent 2fa7811 commit ec9c8e0

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

tool/src/subprocess_launcher.dart

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import 'dart:async';
66
import 'dart:convert';
77
import 'dart:io';
88

9-
class SubprocessException implements Exception {}
10-
119
class SubprocessLauncher {
1210
final String context;
1311
final Map<String, String> defaultEnvironment;
@@ -121,15 +119,19 @@ class SubprocessLauncher {
121119
workingDirectory: workingDirectory,
122120
environment: environment,
123121
includeParentEnvironment: includeParentEnvironment);
122+
// Stream stdout and stderr to _this_ process's stdout and stderr.
124123
var stdoutFuture = _printStream(process.stdout, stdout,
125124
prefix: prefix, filter: jsonCallback);
126125
var stderrFuture = _printStream(process.stderr, stderr,
127126
prefix: prefix, filter: jsonCallback);
128-
await Future.wait([stderrFuture, stdoutFuture, process.exitCode]);
127+
var (_, _, exitCode) =
128+
await (stdoutFuture, stderrFuture, process.exitCode).wait;
129129

130-
var exitCode = await process.exitCode;
131130
if (exitCode != 0) {
132-
throw SubprocessException();
131+
throw SubprocessException(
132+
command: [executable, ...arguments].join(' '),
133+
workingDirectory: workingDirectory,
134+
exitCode: exitCode);
133135
}
134136
return jsonObjects;
135137
}
@@ -174,3 +176,21 @@ class SubprocessLauncher {
174176

175177
static final _quotables = RegExp(r'[ "\r\n\$]');
176178
}
179+
180+
/// An exception that represents an issue during subprocess execution.
181+
class SubprocessException implements Exception {
182+
final String command;
183+
final String? workingDirectory;
184+
final int exitCode;
185+
186+
SubprocessException(
187+
{required this.command,
188+
required this.workingDirectory,
189+
required this.exitCode});
190+
191+
@override
192+
String toString() => 'SubprocessException['
193+
'command: "$command", '
194+
'workingDirectory: "${workingDirectory ?? '(null)'}", '
195+
'exitCode: $exitCode]';
196+
}

0 commit comments

Comments
 (0)