This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Improve Error Messages and Recovery Suggestions #44
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// NetworkError.swift | ||
// GitHub Scanner | ||
// | ||
// Created by Aaron McTavish on 20/02/2017. | ||
// Copyright © 2017 ustwo Fampany Ltd. All rights reserved. | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
|
||
public enum NetworkError: LocalizedError { | ||
case failedRequest(status: Int) | ||
case invalidJSON | ||
case rateLimited | ||
case unauthorized | ||
case unknown(error: Error?) | ||
} | ||
|
||
|
||
extension NetworkError { | ||
|
||
|
||
public var errorDescription: String? { | ||
switch self { | ||
case let .failedRequest(status): | ||
return "Failed Request. Status Code: \(status)" | ||
case .invalidJSON: | ||
return "Invalid JSON returned from the server" | ||
case .rateLimited: | ||
return "Exceed rate limit for requests" | ||
case .unauthorized: | ||
return "Not authorized" | ||
case let .unknown(error): | ||
if let localError = error as? LocalizedError, | ||
let description = localError.errorDescription { | ||
|
||
return "Unknown Error: " + description | ||
} else { | ||
return "Unknown Error" | ||
} | ||
} | ||
} | ||
|
||
public var recoverySuggestion: String? { | ||
switch self { | ||
case .rateLimited, .unauthorized: | ||
return "Use the '--oauth' flag and supply an access token" | ||
case .failedRequest, .invalidJSON, .unknown: | ||
return nil | ||
} | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,22 @@ | |
// GitHub Scanner | ||
// | ||
// Created by Aaron McTavish on 10/02/2017. | ||
// Copyright © 2016 ustwo Fampany Ltd. All rights reserved. | ||
// Copyright © 2017 ustwo Fampany Ltd. All rights reserved. | ||
// | ||
|
||
|
||
// swiftlint:disable large_tuple | ||
|
||
import Foundation | ||
import Result | ||
|
||
|
||
public typealias NetworkValidationResult = Result<(Data, HTTPURLResponse), NetworkError> | ||
|
||
|
||
public enum ResponseHandlers { | ||
static let `default` = JSONResponseHandler() | ||
} | ||
|
||
|
||
public protocol ResponseHandler { | ||
|
@@ -24,6 +33,73 @@ public protocol ResponseHandler { | |
} | ||
|
||
|
||
public enum ResponseHandlers { | ||
static let `default` = JSONResponseHandler() | ||
extension ResponseHandler { | ||
|
||
|
||
/// Checks the response's status code to ensure it is in the 200 range. | ||
/// | ||
/// - Parameter response: The `HTTPURLResponse` to validate. | ||
/// - Returns: Whether or not the response is valid. | ||
public static func isValidResponseStatus(_ response: HTTPURLResponse) -> Bool { | ||
return 200..<300 ~= response.statusCode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 3xx range is valid as well. Perhaps in a future iteration you should handle at least a few of them (i.e. 301, 302 and 307) https://developer.github.com/v3/#http-redirects There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added Issue #45 |
||
} | ||
|
||
/// Checks the response to see if it failed due to rate limiting. | ||
/// | ||
/// - Parameter response: The `HTTPURLResponse` to validate. | ||
/// - Returns: Whether or not the response has been rate limited. | ||
public static func isRateLimitedResponse(_ response: HTTPURLResponse) -> Bool { | ||
if [401, 403].contains(response.statusCode), | ||
let rateLimitString = response.allHeaderFields["X-RateLimit-Remaining"] as? String, | ||
let rateLimit = Int(rateLimitString), | ||
rateLimit == 0 { | ||
|
||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
/// Checks the response to see if it failed due to not having sufficient authorization on the request. | ||
/// | ||
/// - Parameter response: The `HTTPURLResponse` to validate. | ||
/// - Returns: Whether or not the response failed due to being unauthorized. | ||
/// | ||
/// - Note: Should check `ResponseHandler.isRateLimitedResponse(_:)` before calling this method, | ||
/// as being rate limited could also return a 401 status code. | ||
public static func isUnauthorized(_ response: HTTPURLResponse) -> Bool { | ||
return response.statusCode == 401 | ||
} | ||
|
||
/// Validates a network response. | ||
/// | ||
/// - Parameters: | ||
/// - data: `Data` from the body of the response. | ||
/// - response: `URLResponse` to validate. | ||
/// - error: `Error` from making the network request. | ||
/// - Returns: If the validation is successful, returns the `data` unwrapped and the response | ||
/// as an `HTTPURLResponse`. If the validation is unsuccessful, returns the `NetworkError` | ||
/// representing the issue. | ||
/// | ||
/// - Note: Checks for a valid status code, rate limiting error, and authorization. | ||
public static func validateResponse(data: Data?, response: URLResponse?, error: Error?) -> NetworkValidationResult { | ||
guard let httpResponse = response as? HTTPURLResponse, | ||
let data = data else { | ||
|
||
return .failure(NetworkError.unknown(error: error)) | ||
} | ||
|
||
guard Self.isValidResponseStatus(httpResponse) else { | ||
if Self.isRateLimitedResponse(httpResponse) { | ||
return .failure(NetworkError.rateLimited) | ||
} else if Self.isUnauthorized(httpResponse) { | ||
return .failure(NetworkError.unauthorized) | ||
} | ||
|
||
return .failure(NetworkError.failedRequest(status: httpResponse.statusCode)) | ||
} | ||
|
||
return .success((data, httpResponse)) | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful