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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error messages and recovery suggestions
Resolves #35 `Improve Error Explanation for Rate Limit`. - Adds checks for rate limiting and uses new, specific error - Refactors response validation to static methods in an extension of `ResponseHandler`, thus providing a default implementation to all response handlers - Refactors the deserialisation of JSON in `JSONResponseHandler` into a separate method to improve readability - Updates `GitHubScannerProtocolError` to inherit from `LocalizedError` and provides a default implementation for `CustomStringConvertible` - Adds `errorDescription` and `recoverySuggestion` to all error types - Moves `NetworkError` and `ScanOptionsError` into own files - Expands test suite for error handling - Refactors accessing the `HTTPURLResponse` next link header into an extension property to improve readability of `JSONResponseHandler` and to help maintain single responsibility principle
- Loading branch information
Aaron McTavish
committed
Feb 20, 2017
1 parent
017ddc4
commit 2ce0784
Showing
11 changed files
with
636 additions
and
178 deletions.
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
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.