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) outputResults(savedFiles)
} catch { } catch {
handleError(error) 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) ImageErrorHandler.handleError(error, jsonOutput: jsonOutput)
} }

View file

@ -2,7 +2,7 @@ import Foundation
import AppKit import AppKit
struct ImageErrorHandler { 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 { let captureError: CaptureError = if let err = error as? CaptureError {
err err
} else { } else {
@ -70,6 +70,6 @@ struct ImageErrorHandler {
var localStandardErrorStream = FileHandleTextOutputStream(FileHandle.standardError) var localStandardErrorStream = FileHandleTextOutputStream(FileHandle.standardError)
print("Error: \(captureError.localizedDescription)", to: &localStandardErrorStream) 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 { } catch {
handleError(error) 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 { let captureError: CaptureError = if let err = error as? CaptureError {
err err
} else if let appError = error as? ApplicationError { } else if let appError = error as? ApplicationError {
@ -75,7 +76,7 @@ struct AppsSubcommand: AsyncParsableCommand {
} else { } else {
fputs("Error: \(captureError.localizedDescription)\n", stderr) 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]) { func printApplicationList(_ applications: [ApplicationInfo]) {
@ -156,10 +157,11 @@ struct WindowsSubcommand: AsyncParsableCommand {
} catch { } catch {
handleError(error) 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 { let captureError: CaptureError = if let err = error as? CaptureError {
err err
} else if let appError = error as? ApplicationError { } else if let appError = error as? ApplicationError {
@ -192,7 +194,7 @@ struct WindowsSubcommand: AsyncParsableCommand {
} else { } else {
fputs("Error: \(captureError.localizedDescription)\n", stderr) 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> { private func parseIncludeDetails() -> Set<WindowDetailOption> {

View file

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