Skip to content

Instantly share code, notes, and snippets.

@ABridoux
Created June 16, 2024 21:38
Show Gist options
  • Save ABridoux/1df6957308a32955371fb9395a585780 to your computer and use it in GitHub Desktop.
Save ABridoux/1df6957308a32955371fb9395a585780 to your computer and use it in GitHub Desktop.
OSStatus map to error
// MARK: - Check error
extension OSStatus {
/// Error mapped from an `OSStatus`.
public struct StatusError: Error {
// MARK: Properties
let status: OSStatus
let message: String
}
/// Check an `OSStatus` to throw a `StatusError` if status is different from `noError`.
/// - Parameter message: Message of the error to be thrown.
/// - throws: If status is different from `noError`.
public func checkError(_ message: String) throws {
if self == noErr { return }
throw StatusError(status: self, message: message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment