-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward localized descriptions of extension errors to host (#36)
- Loading branch information
Showing
5 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
TophatKit/Sources/TophatKit/Internal/XPC/ExtensionXPCGenericLocalizedError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
TophatKit/Sources/TophatKit/Internal/XPC/NSError+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |