Subprocess icon indicating copy to clipboard operation
Subprocess copied to clipboard

Added exit code to message for exitedWithNonZeroStatus error

Open macblazer opened this issue 1 year ago • 0 comments

The SubprocessError type nicely encapsulates the errors that come from the (deprecated) Shell code. However, when the process being called exits with a non-zero exit code and output no other info to stdout or stderr, the description of the error is empty for the calling code.

Example code:

do {
	_ = try Shell(["/usr/bin/false"]).exec()
} catch {
	os_log("Error description was: '\(error.localizedDescription)'")
	if let exitError = error as? SubprocessError,
	   case .exitedWithNonZeroStatus(let exitCode, let text) = exitError {
		os_log("Exit code was: \(exitCode); text was '\(text)'")
	}
}

Actual output:

Error description was: ''
Exit code was: 1; text was ''

Expected output (something like this):

Error description was: 'Process exited with code 1'
Exit code was: 1; text was ''

Current usages of . exitedWithNonZeroStatus: https://github.com/jamf/Subprocess/blob/5568e4283200c4fe305158a94ff69355b4c9da25/Sources/Subprocess/Shell.swift#L110 https://github.com/jamf/Subprocess/blob/5568e4283200c4fe305158a94ff69355b4c9da25/Sources/Subprocess/Shell.swift#L154

Current code that converts the error to text: https://github.com/jamf/Subprocess/blob/5568e4283200c4fe305158a94ff69355b4c9da25/Sources/Subprocess/Errors.swift#L55

macblazer avatar Jan 05 '24 18:01 macblazer