Created
April 25, 2017 18:28
-
-
Save gravicle/588260bb9565340009b0ec331231cfe8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public enum HTTPError: Error, ExpressibleByIntegerLiteral { | |
case badRequest(description: String?) | |
case unauthorized(description: String?) | |
case notFound(description: String?) | |
case serverError(description: String?) | |
case unprocessableRequest(description: String?) | |
case unknown(description: String?) | |
} | |
// MARK: - ExpressibleByIntegerLiteral | |
public extension HTTPError { | |
public init(integerLiteral value: Int) { | |
self = .init(code: value, description: nil) | |
} | |
} | |
// MARK: - Init | |
extension HTTPError { | |
init(code: Int, description: String?) { | |
switch code { | |
case 400: | |
self = .badRequest(description: description) | |
case 401: | |
self = .unauthorized(description: description) | |
case 404: | |
self = .notFound(description: description) | |
case 422: | |
self = .unprocessableRequest(description: description) | |
case 500: | |
self = .serverError(description: description) | |
default: | |
self = .unknown(description: description) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment