Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Implemented LocalizedError protocol for SpotifyError
Browse files Browse the repository at this point in the history
(using updated SwiftyOAuth and Avenue libs)
  • Loading branch information
radianttap committed Apr 18, 2019
1 parent d98eb1a commit 456e9c0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
@@ -1,5 +1,5 @@
github "radianttap/Ambar" "6.2"
github "radianttap/Avenue" "3.1"
github "radianttap/Avenue" "3.1.1"
github "radianttap/CardPresentationController" "1.5.1"
github "radianttap/Coordinator" "6.2.1"
github "radianttap/Kingfisher" "5.3.0"
Expand Down
5 changes: 2 additions & 3 deletions Spotify/Spotify.swift
Expand Up @@ -142,9 +142,8 @@ private extension Spotify {
case .success(let token):
print(token)
case .failure(let error):
print(error)
let callback = apiRequest.callback
callback(nil, .authError)
callback(nil, .authError(error))
}
}
}
Expand All @@ -167,7 +166,7 @@ private extension Spotify {
let callback = apiRequest.callback

guard let token = oauthProvider.token, token.isValid else {
callback(nil, .authError)
callback(nil, .invalidAuthToken)
return
}

Expand Down
64 changes: 62 additions & 2 deletions Spotify/SpotifyError.swift
Expand Up @@ -8,8 +8,9 @@

import Foundation
import Avenue
import SwiftyOAuth

enum SpotifyError: Error {
enum SpotifyError: Swift.Error {
case invalidBaseURL(basePath: String)
case invalidFinalURL(url: URL, queryParams: JSON)
case requestBuildFailed(Swift.Error)
Expand All @@ -19,5 +20,64 @@ enum SpotifyError: Error {
case emptyResponse
case unexpectedResponse(HTTPURLResponse, String?)
case httpError(HTTPURLResponse)
case authError

case invalidAuthToken
case authError(SwiftyOAuth.Error)
}


extension SpotifyError: LocalizedError {
var errorDescription: String? {
switch self {
case .invalidBaseURL, .invalidFinalURL, .requestBuildFailed:
return NSLocalizedString("Internal error", comment: "")

case .invalidResponseType, .emptyResponse, .unexpectedResponse, .httpError:
return NSLocalizedString("Invalid or unexpected response from Spotify", comment: "")

case .networkError(let netErr):
return netErr.errorDescription

case .invalidAuthToken:
return NSLocalizedString("Authorization error", comment: "")

case .authError(let authError):
return authError.errorDescription
}
}

var failureReason: String? {
switch self {
case .invalidBaseURL(let basePath):
return String(format: NSLocalizedString("Failed to build URL using: %@", comment: ""), basePath)

case .invalidFinalURL(let url, let queryParams):
return String(format: NSLocalizedString("Failed to build URL using: %@ and query-string parameters: %@", comment: ""), url.path, queryParams)

case .requestBuildFailed(let error):
return String(format: NSLocalizedString("Failed to build URLRequest: %@", comment: ""), error.localizedDescription)

case .invalidResponseType:
return NSLocalizedString("Unexpected response (non HTTP)", comment: "")

case .emptyResponse:
return NSLocalizedString("Empty response received (no data)", comment: "")

case .unexpectedResponse(let httpURLResponse, let str):
let s = str ?? String(describing: httpURLResponse.allHeaderFields)
return s

case .httpError(let httpURLResponse):
return String(describing: httpURLResponse.allHeaderFields)

case .networkError(let netErr):
return netErr.failureReason

case .invalidAuthToken:
return NSLocalizedString("Authorization token is missing or expired", comment: "")

case .authError(let authError):
return authError.failureReason
}
}
}

0 comments on commit 456e9c0

Please sign in to comment.