Improve error handling

This commit is contained in:
Peter Steinberger 2025-06-08 20:24:46 +01:00
parent 17e73f12f2
commit 1084050f18
4 changed files with 12 additions and 8 deletions

View file

@ -60,6 +60,8 @@ struct ImageCommand: AsyncParsableCommand {
outputResults(savedFiles)
} catch {
handleError(error)
// Throw a special exit error that AsyncParsableCommand can handle
throw ExitCode(Int32(1))
}
}
@ -98,7 +100,7 @@ struct ImageCommand: AsyncParsableCommand {
}
}
private func handleError(_ error: Error) -> Never {
private func handleError(_ error: Error) {
ImageErrorHandler.handleError(error, jsonOutput: jsonOutput)
}

View file

@ -2,7 +2,7 @@ import Foundation
import AppKit
struct ImageErrorHandler {
static func handleError(_ error: Error, jsonOutput: Bool) -> Never {
static func handleError(_ error: Error, jsonOutput: Bool) {
let captureError: CaptureError = if let err = error as? CaptureError {
err
} else {
@ -70,6 +70,6 @@ struct ImageErrorHandler {
var localStandardErrorStream = FileHandleTextOutputStream(FileHandle.standardError)
print("Error: \(captureError.localizedDescription)", to: &localStandardErrorStream)
}
Foundation.exit(captureError.exitCode)
// Don't call exit() here - let the caller handle process termination
}
}

View file

@ -41,10 +41,11 @@ struct AppsSubcommand: AsyncParsableCommand {
} catch {
handleError(error)
throw ExitCode(Int32(1))
}
}
private func handleError(_ error: Error) -> Never {
private func handleError(_ error: Error) {
let captureError: CaptureError = if let err = error as? CaptureError {
err
} else if let appError = error as? ApplicationError {
@ -75,7 +76,7 @@ struct AppsSubcommand: AsyncParsableCommand {
} else {
fputs("Error: \(captureError.localizedDescription)\n", stderr)
}
Foundation.exit(captureError.exitCode)
// Don't call exit() here - let the caller handle process termination
}
func printApplicationList(_ applications: [ApplicationInfo]) {
@ -156,10 +157,11 @@ struct WindowsSubcommand: AsyncParsableCommand {
} catch {
handleError(error)
throw ExitCode(Int32(1))
}
}
private func handleError(_ error: Error) -> Never {
private func handleError(_ error: Error) {
let captureError: CaptureError = if let err = error as? CaptureError {
err
} else if let appError = error as? ApplicationError {
@ -192,7 +194,7 @@ struct WindowsSubcommand: AsyncParsableCommand {
} else {
fputs("Error: \(captureError.localizedDescription)\n", stderr)
}
Foundation.exit(captureError.exitCode)
// Don't call exit() here - let the caller handle process termination
}
private func parseIncludeDetails() -> Set<WindowDetailOption> {

View file

@ -1,6 +1,6 @@
import Foundation
import CoreGraphics
import ScreenCaptureKit
@preconcurrency import ScreenCaptureKit
struct ScreenCapture: Sendable {
static func captureDisplay(