@@ -37,7 +37,7 @@ public struct SubprocessError: Swift.Error, Sendable {
3737 public let underlyingError : UnderlyingError ?
3838
3939 /// Context associated with this error for better error message
40- private let context : [ Code : Context ]
40+ private let context : [ Code : Context ]
4141}
4242
4343// MARK: - Error Codes
@@ -117,7 +117,7 @@ extension SubprocessError {
117117 return String ( describing: error)
118118 #if os(Windows)
119119 case . internalError( let windowsError) :
120- return windowsError. localizedDescription
120+ return String ( describing : windowsError)
121121 #else
122122 case . internalError( let errno) :
123123 return errno. description
@@ -150,42 +150,51 @@ extension SubprocessError {
150150 }
151151 }
152152
153- private enum Context : Sendable , Equatable {
153+ private enum Context : Sendable , Equatable {
154154 case string( String )
155155 case int( Int )
156156 case processControlOperation( ProcessControlOperation )
157157 }
158158
159159 internal enum ProcessControlOperation : Sendable , Equatable {
160- case sendSignal( Int32 ) // Unix
161- case terminate // Windows
162- case suspend // Windows
163- case resume // Windows
160+ case sendSignal( Int32 ) // Unix
161+ case terminate // Windows
162+ case suspend // Windows
163+ case resume // Windows
164164 }
165165}
166166
167-
168167// MARK: - Description
169168extension SubprocessError : CustomStringConvertible , CustomDebugStringConvertible {
170169 /// A textual representation of this subprocess error.
171170 public var description : String {
172171 switch self . code. storage {
173172 case . spawnFailed:
173+ var message = [ " Failed to spawn the new process. " ]
174+
175+ if let context = self . context [ self . code] ,
176+ case . string( let reason) = context
177+ {
178+ message. append ( " Reason: \( reason) " )
179+ }
180+
174181 if let underlying = self . underlyingError {
175- return " Failed to spawn the new process with underlying error: \( underlying) "
176- } else {
177- return " Failed to spawn the new process "
182+ message. append ( " Underlying error: \( underlying) " )
178183 }
184+
185+ return message. joined ( separator: " " )
179186 case . executableNotFound:
180187 if let context = self . context [ self . code] ,
181- case . string( let executableName) = context {
188+ case . string( let executableName) = context
189+ {
182190 return " Executable \" \( executableName) \" is not found or cannot be executed. "
183191 } else {
184192 return " Executable is not found or cannot be executed. "
185193 }
186194 case . failedToChangeWorkingDirectory:
187195 if let context = self . context [ self . code] ,
188- case . string( let directory) = context {
196+ case . string( let directory) = context
197+ {
189198 return " Failed to set working directory to \" \( directory) \" . "
190199 } else {
191200 return " Failed to change working directory. "
@@ -211,7 +220,8 @@ extension SubprocessError: CustomStringConvertible, CustomDebugStringConvertible
211220 }
212221 case . outputLimitExceeded:
213222 if let context = self . context [ self . code] ,
214- case . int( let limit) = context {
223+ case . int( let limit) = context
224+ {
215225 return " Child process output exceeded the limit of \( limit) bytes. "
216226 } else {
217227 return " Child process output exceeded the limit. "
@@ -231,7 +241,8 @@ extension SubprocessError: CustomStringConvertible, CustomDebugStringConvertible
231241
232242 case . processControlFailed:
233243 if let context = self . context [ self . code] ,
234- case . processControlOperation( let operation) = context {
244+ case . processControlOperation( let operation) = context
245+ {
235246 switch operation {
236247 case . sendSignal( let signal) :
237248 return " Failed to send signal \( signal) to child process "
@@ -280,7 +291,7 @@ extension SubprocessError {
280291 return SubprocessError (
281292 code: . executableNotFound,
282293 underlyingError: . init( internalError) ,
283- context: [ . executableNotFound : . string( executable) ]
294+ context: [ . executableNotFound: . string( executable) ]
284295 )
285296 }
286297
@@ -296,7 +307,7 @@ extension SubprocessError {
296307 return SubprocessError (
297308 code: . processControlFailed,
298309 underlyingError: . init( internalError) ,
299- context: [ . processControlFailed : . processControlOperation( operation) ]
310+ context: [ . processControlFailed: . processControlOperation( operation) ]
300311 )
301312 }
302313
@@ -308,11 +319,18 @@ extension SubprocessError {
308319 )
309320 }
310321
311- internal static func spawnFailed< E: Swift . Error > ( withInternalError internalError: E ? ) -> Self {
322+ internal static func spawnFailed(
323+ withInternalError internalError: InternalError ? ,
324+ reason: String ? = nil
325+ ) -> Self {
326+ var context : [ SubprocessError . Code : SubprocessError . Context ] = [ : ]
327+ if let reason = reason {
328+ context [ . spawnFailed] = . string( reason)
329+ }
312330 return SubprocessError (
313331 code: . spawnFailed,
314332 underlyingError: . init( internalError) ,
315- context: [ : ]
333+ context: context
316334 )
317335 }
318336
@@ -321,7 +339,7 @@ extension SubprocessError {
321339 code: . outputLimitExceeded,
322340 underlyingError: nil ,
323341 context: [
324- . outputLimitExceeded : . int( limit)
342+ . outputLimitExceeded: . int( limit)
325343 ]
326344 )
327345 }
@@ -366,8 +384,8 @@ extension SubprocessError {
366384 )
367385 }
368386
369- internal static func failedToWriteToProcess< E : Swift . Error > (
370- withInternalError internalError: E ?
387+ internal static func failedToWriteToProcess(
388+ withInternalError internalError: SubprocessError . InternalError ?
371389 ) -> Self {
372390 return SubprocessError (
373391 code: . failedToWriteToSubprocess,
@@ -390,7 +408,7 @@ extension SubprocessError {
390408 _ target: String ? ,
391409 internalError: InternalError ?
392410 ) -> Self {
393- var context : [ SubprocessError . Code : SubprocessError . Context ] = [ : ]
411+ var context : [ SubprocessError . Code : SubprocessError . Context ] = [ : ]
394412 if let targetPath = target {
395413 context [ . failedToChangeWorkingDirectory] = . string( targetPath)
396414 }
@@ -411,4 +429,3 @@ extension SubprocessError {
411429 )
412430 }
413431}
414-
0 commit comments