Improve error handling when no windows are available

This commit is contained in:
Peter Steinberger 2025-06-08 04:05:52 +01:00
parent 17b74b4f1f
commit e9e2cdb6cb
3 changed files with 8 additions and 3 deletions

View file

@ -111,7 +111,7 @@ struct ImageCommand: ParsableCommand {
.PERMISSION_ERROR_ACCESSIBILITY
case .appNotFound:
.APP_NOT_FOUND
case .windowNotFound:
case .windowNotFound, .noWindowsFound:
.WINDOW_NOT_FOUND
case .fileWriteError:
.FILE_IO_ERROR
@ -224,7 +224,7 @@ struct ImageCommand: ParsableCommand {
let windows = try WindowManager.getWindowsForApp(pid: targetApp.processIdentifier)
guard !windows.isEmpty else {
throw CaptureError.windowNotFound
throw CaptureError.noWindowsFound(targetApp.localizedName ?? appIdentifier)
}
let targetWindow: WindowData
@ -270,7 +270,7 @@ struct ImageCommand: ParsableCommand {
let windows = try WindowManager.getWindowsForApp(pid: targetApp.processIdentifier)
guard !windows.isEmpty else {
throw CaptureError.windowNotFound
throw CaptureError.noWindowsFound(targetApp.localizedName ?? appIdentifier)
}
var savedFiles: [SavedFile] = []

View file

@ -112,6 +112,7 @@ enum CaptureError: Error, LocalizedError {
case invalidWindowIndex(Int)
case invalidArgument(String)
case unknownError(String)
case noWindowsFound(String)
var errorDescription: String? {
switch self {
@ -158,6 +159,8 @@ enum CaptureError: Error, LocalizedError {
return "Invalid argument: \(message)"
case let .unknownError(message):
return "An unexpected error occurred: \(message)"
case let .noWindowsFound(appName):
return "The '\(appName)' process is running, but no capturable windows were found."
}
}
@ -175,6 +178,7 @@ enum CaptureError: Error, LocalizedError {
case .invalidWindowIndex: 19
case .invalidArgument: 20
case .unknownError: 1
case .noWindowsFound: 7
}
}
}

View file

@ -64,6 +64,7 @@ function mapExitCodeToErrorMessage(
: `Swift CLI execution failed (exit code: ${exitCode})`;
const errorCodeMap: { [key: number]: { message: string, code: string } } = {
1: { message: "An unknown error occurred in the Swift CLI.", code: "SWIFT_CLI_UNKNOWN_ERROR" },
7: { message: "The specified application is running but has no capturable windows. Try setting 'capture_focus' to 'foreground' to un-hide application windows.", code: "SWIFT_CLI_NO_WINDOWS_FOUND" },
10: { message: "No displays available for capture.", code: "SWIFT_CLI_NO_DISPLAYS" },
11: {
message: "Screen Recording permission is not granted. Please enable it in System Settings > Privacy & Security > Screen Recording.",