This commit is contained in:
Max Goedjen 2025-09-08 23:21:41 -07:00
parent eaef1e801b
commit fd0686be0c
No known key found for this signature in database
3 changed files with 31 additions and 6 deletions

View File

@ -31,7 +31,11 @@ public final class XPCServiceDelegate: NSObject, NSXPCListenerDelegate {
let encoded = try JSONEncoder().encode(result)
reply(encoded, nil)
} catch {
reply(nil, error)
if let error = error as? Codable & Error {
reply(nil, NSError(error))
} else {
reply(nil, error)
}
}
}
}
@ -43,3 +47,24 @@ public final class XPCServiceDelegate: NSObject, NSXPCListenerDelegate {
}
extension NSError {
private enum Constants {
static let domain = "com.maxgoedjen.secretive.xpcwrappers"
static let code = -1
static let dataKey = "underlying"
}
@nonobjc convenience init<ErrorType: Codable & Error>(_ error: ErrorType) {
let encoded = try? JSONEncoder().encode(error)
self.init(domain: Constants.domain, code: Constants.code, userInfo: [Constants.dataKey: encoded as Any])
}
@nonobjc public func underlying<ErrorType: Codable & Error>(as errorType: ErrorType.Type) -> ErrorType? {
guard domain == Constants.domain && code == Constants.code, let data = userInfo[Constants.dataKey] as? Data else { return nil }
return try? JSONDecoder().decode(ErrorType.self, from: data)
}
}

View File

@ -32,10 +32,12 @@ public struct XPCTypedSession<ResponseType: Codable & Sendable, ErrorType: Error
}
let decoded = try JSONDecoder().decode(ResponseType.self, from: data)
continuation.resume(returning: decoded)
} catch let error as ErrorType {
continuation.resume(throwing: error)
} catch {
continuation.resume(throwing: error)
if let typed = (error as NSError).underlying(as: ErrorType.self) {
continuation.resume(throwing: typed)
} else {
continuation.resume(throwing: error)
}
}
}
}

View File

@ -5,8 +5,6 @@ import Brief
final class SecretiveUpdater: NSObject, XPCProtocol {
private let logger = Logger(subsystem: "com.maxgoedjen.secretive.ReleasesDownloader", category: "ReleasesDownloader")
enum Constants {
static let updateURL = URL(string: "https://api.github.com/repos/maxgoedjen/secretive/releases")!
}