Skip to content

Commit

Permalink
Merge pull request #43 from outfoxx/task/error-descs
Browse files Browse the repository at this point in the history
Add human readable descriptions to `SundayError` and it’s “reason”s
  • Loading branch information
kdubb committed May 10, 2023
2 parents de5b2a1 + ed4609d commit 8f272f4
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Sources/Sunday/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,78 @@ public enum SundayError: Error {
case invalidHTTPResponse
case pathParameterEncodingFailed(parameter: String, reason: Error?)
}

extension SundayError: LocalizedError {

public var errorDescription: String? {
switch self {
case .requestEncodingFailed(reason: let reason):
return "Request Encoding Failed: \(reason)"
case .responseDecodingFailed(reason: let reason):
return "Response Decoding Failed: \(reason)"
case .responseValidationFailed(reason: let reason):
return "Resonse Validation Failed: \(reason)"
case .unexpectedEmptyResponse:
return "Unexpected Empty Response"
case .invalidURL(let url):
return "Invalid URL\(url.map { " url=\($0.description)" } ?? "")"
case .invalidHTTPResponse:
return "Invalid HTTP Response"
case .pathParameterEncodingFailed(parameter: let param, reason: let reason):
return "Path Parameter Encoding Failed\(reason.map { ": \($0)" } ?? ""): param=\(param)"
}
}

}

extension RequestEncodingFailureReason: CustomStringConvertible {

public var description: String {
switch self {
case .noSupportedContentTypes(let contentTypes):
return "No Supported Content-Types: requires=\(contentTypes)"
case .noSupportedAcceptTypes(let contentTypes):
return "No Supported Accept Content-Types: requires=\(contentTypes)"
case .unsupportedContentType(let contentType):
return "Unsupported Content-Type: type=\(contentType)"
case .serializationFailed(contentType: let contentType, error: let error):
return "Serialization Failed\(error.map { ": \($0)" } ?? ""): content-type=\(contentType)"
case .unsupportedHeaderParameterValue(let value):
return "Unsupported Header Value: value=\(String(describing: value))"
}
}

}

extension ResponseDecodingFailureReason: CustomStringConvertible {

public var description: String {
switch self {
case .invalidContentType(let contentType):
return "Invalid Content-Type: type=\(contentType)"
case .unsupportedContentType(let contentType):
return "Unsupported Content-Type: type=\(contentType)"
case .noData:
return "No Data"
case .deserializationFailed(contentType: let contentType, error: let error):
return "Deserialization Failed\(error.map { ": \($0)" } ?? ""): content-type=\(contentType)"
case .missingValue:
return "Missing Value"
}
}
}

extension ResponseValidationFailureReason: CustomStringConvertible {

public var description: String {
switch self {
case .unacceptableStatusCode(response: let response, data: let data):
var params: [(String, String)] = [("status", "\(response.statusCode)")]
if let url = response.url {
params.append(("url", url.absoluteString))
}
params.append(("response-size", "\(data.map { "\($0)" } ?? "empty")"))
return "Unacceptable Status Code: \(params.map { "\($0)=\($1)" }.joined(separator: ", "))"
}
}
}
48 changes: 48 additions & 0 deletions Tests/SundayTests/ErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 Outfox, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Sunday
import XCTest

final class ErrorTests: XCTestCase {

func testDescriptions() throws {
XCTAssertEqual(SundayError.unexpectedEmptyResponse.errorDescription, "Unexpected Empty Response")
XCTAssertEqual(
SundayError.invalidURL(URLComponents(string: "http://example.com")).errorDescription,
"Invalid URL url=http://example.com"
)
XCTAssertEqual(SundayError.invalidHTTPResponse.errorDescription, "Invalid HTTP Response")
XCTAssertEqual(
RequestEncodingFailureReason.unsupportedContentType(.json).description,
"Unsupported Content-Type: type=application/json"
)
XCTAssertEqual(
ResponseDecodingFailureReason.unsupportedContentType(.json).description,
"Unsupported Content-Type: type=application/json"
)

let response = HTTPURLResponse(url: URL(string: "http://example.com")!,
mimeType: nil,
expectedContentLength: 0,
textEncodingName: nil)
XCTAssertEqual(
ResponseValidationFailureReason.unacceptableStatusCode(response: response, data: nil).description,
"Unacceptable Status Code: status=200, url=http://example.com, response-size=empty"
)
}

}

0 comments on commit 8f272f4

Please sign in to comment.