28 lines
582 B
Swift
28 lines
582 B
Swift
|
import Foundation
|
||
|
|
||
|
struct ContentError: Error {
|
||
|
|
||
|
let reason: String
|
||
|
|
||
|
let source: String
|
||
|
|
||
|
let error: Error?
|
||
|
}
|
||
|
|
||
|
extension Optional {
|
||
|
|
||
|
func unwrapped(or error: ContentError) throws -> Wrapped {
|
||
|
guard case let .some(value) = self else {
|
||
|
throw error
|
||
|
}
|
||
|
return value
|
||
|
}
|
||
|
|
||
|
func unwrapOrFail(_ reason: String, source: String, error: Error? = nil) throws -> Wrapped {
|
||
|
guard case let .some(value) = self else {
|
||
|
throw ContentError(reason: reason, source: source, error: error)
|
||
|
}
|
||
|
return value
|
||
|
}
|
||
|
}
|