Skip to content

Commit

Permalink
Forward localized descriptions of extension errors to host (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfroms authored Nov 28, 2024
1 parent c424cda commit e3bb928
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions TophatKit/Sources/TophatKit/ArtifactProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public protocol ArtifactProvider {
///
/// Throw any errors if they ocurred. Use any parameters wrapped with ``Parameter`` to
/// collect inputs from Tophat to implement the retrieval mechanism.
///
/// To display an error message in the user interface, conform thrown errors to the
/// `LocalizedError` protocol.
/// - Returns: A ``ArtifactProviderResult`` containing the output.
func retrieve() async throws -> Result

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// ExtensionXPCGenericLocalizedError.swift
// TophatKit
//
// Created by Lukas Romsicki on 2024-11-27.
//

import Foundation

struct ExtensionXPCGenericLocalizedError: LocalizedError {
let errorDescription: String?
let failureReason: String?
let recoverySuggestion: String?
let helpAnchor: String?

init?(nsError: NSError) {
let userInfo = nsError.userInfo

self.errorDescription = userInfo["errorDescription"] as? String
self.failureReason = userInfo["failureReason"] as? String
self.recoverySuggestion = userInfo["recoverySuggestion"] as? String
self.helpAnchor = userInfo["helpAnchor"] as? String

guard errorDescription != nil || failureReason != nil || recoverySuggestion != nil || helpAnchor != nil else {
return nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct ExtensionXPCReceivedMessageContainer {
replyHandler(nil, error)
}
case .failure(let error):
replyHandler(nil, error)
// The error will be an NSError over XPC anyway.
replyHandler(nil, NSError(embeddingLocalizedDescriptionsFrom: error))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ extension ExtensionXPCSession {
}

service.send(identifier: message.identifier, data: dataToSend) { dataFromReply, error in
if let error {
if let nsError = error as? NSError, let localizedError = ExtensionXPCGenericLocalizedError(nsError: nsError) {
continuation.resume(throwing: localizedError)
return
} else if let error {
continuation.resume(throwing: error)
return
}
Expand Down
24 changes: 24 additions & 0 deletions TophatKit/Sources/TophatKit/Internal/XPC/NSError+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// NSError+Extensions.swift
// TophatKit
//
// Created by Lukas Romsicki on 2024-11-27.
//

import Foundation

extension NSError {
convenience init(embeddingLocalizedDescriptionsFrom error: Error) {
let nsError = error as NSError
var newUserInfo = nsError.userInfo

if let localizedError = error as? LocalizedError {
newUserInfo["errorDescription"] = localizedError.errorDescription
newUserInfo["failureReason"] = localizedError.failureReason
newUserInfo["recoverySuggestion"] = localizedError.recoverySuggestion
newUserInfo["helpAnchor"] = localizedError.helpAnchor
}

self.init(domain: nsError.domain, code: nsError.code, userInfo: newUserInfo)
}
}

0 comments on commit e3bb928

Please sign in to comment.