LiveUI / S3

S3 Client written in Swift
MIT License
94 stars 60 forks source link

Conform S3.Error to AbortError Protocol #31

Closed calebkleveter closed 5 years ago

calebkleveter commented 5 years ago

By conforming S3.Error to Vapor's AbortError protocol, the client would receive better error messages when a problem occurs. I don't have time for a PR, but this is what I managed to throw together:

extension S3.Error: AbortError {
    public var status: HTTPResponseStatus {
        switch self {
        case .s3NotRegistered: return .internalServerError
        case .notFound: return .notFound
        case .missingData: return .failedDependency
        case .invalidUrl: return .badRequest
        case .badStringData: return .badRequest
        case let .badResponse(response): return response.http.status
        case let .errorResponse(status, _): return status
        }
    }

    public var reason: String {
        switch self {
        case .s3NotRegistered: return "No S3 instance found registered with the current container"
        case .notFound: return "Resource not found at URL"
        case .missingData: return "Unexpectedly got empty response body from S3 API"
        case .invalidUrl: return "Cannot convert string to URL"
        case .badStringData: return "Cannot convert specified string to byte array"
        case let .badResponse(response): return String(data: response.http.body.data ?? Data(), encoding: .utf8) ?? ""
        case let .errorResponse(_, message): return "[" + message.code + "] " + message.message
        }
    }

    public var identifier: String {
        switch self {
        case .s3NotRegistered: return "s3NotRegistered"
        case .notFound: return "notFound"
        case .missingData: return "missingData"
        case .invalidUrl: return "invalidUrl"
        case .badStringData: return "badStringData"
        case .badResponse: return "badResponse"
        case .errorResponse: return "errorResponse"
        }
    }
}
rafiki270 commented 5 years ago

I know, but we are trying to remove all dependencies on Vapor so the package can be used on it's own :( ... I already had this on one branch somewhere while back ... :(

calebkleveter commented 5 years ago

Alright, sounds good!