2021-02-03 09:01:08 +01:00
|
|
|
//
|
|
|
|
// APIService+Error.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-2-2.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import MastodonSDK
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonLocalization
|
2021-02-03 09:01:08 +01:00
|
|
|
|
|
|
|
extension APIService {
|
|
|
|
enum APIError: Error {
|
|
|
|
|
|
|
|
case implicit(ErrorReason)
|
|
|
|
case explicit(ErrorReason)
|
|
|
|
|
|
|
|
enum ErrorReason {
|
|
|
|
// application internal error
|
|
|
|
case authenticationMissing
|
|
|
|
case badRequest
|
|
|
|
case badResponse
|
|
|
|
case requestThrottle
|
|
|
|
|
2021-03-04 11:53:29 +01:00
|
|
|
case voteExpiredPoll
|
|
|
|
|
2021-02-03 09:01:08 +01:00
|
|
|
// Server API error
|
|
|
|
case mastodonAPIError(Mastodon.API.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var errorReason: ErrorReason {
|
|
|
|
switch self {
|
|
|
|
case .implicit(let errorReason): return errorReason
|
|
|
|
case .explicit(let errorReason): return errorReason
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - LocalizedError
|
|
|
|
extension APIService.APIError: LocalizedError {
|
|
|
|
|
2021-07-26 14:09:23 +02:00
|
|
|
public var errorDescription: String? {
|
2021-02-03 09:01:08 +01:00
|
|
|
switch errorReason {
|
|
|
|
case .authenticationMissing: return "Fail to Authenticatie"
|
|
|
|
case .badRequest: return "Bad Request"
|
|
|
|
case .badResponse: return "Bad Response"
|
|
|
|
case .requestThrottle: return "Request Throttled"
|
2021-03-04 11:53:29 +01:00
|
|
|
case .voteExpiredPoll: return L10n.Common.Alerts.VoteFailure.title
|
2021-02-03 09:01:08 +01:00
|
|
|
case .mastodonAPIError(let error):
|
|
|
|
guard let responseError = error.mastodonError else {
|
|
|
|
guard error.httpResponseStatus != .ok else {
|
|
|
|
return "Unknown Error"
|
|
|
|
}
|
|
|
|
return error.httpResponseStatus.reasonPhrase
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseError.errorDescription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var failureReason: String? {
|
|
|
|
switch errorReason {
|
|
|
|
case .authenticationMissing: return "Account credential not found."
|
|
|
|
case .badRequest: return "Request invalid."
|
|
|
|
case .badResponse: return "Response invalid."
|
|
|
|
case .requestThrottle: return "Request too frequency."
|
2021-08-09 13:44:04 +02:00
|
|
|
case .voteExpiredPoll: return L10n.Common.Alerts.VoteFailure.pollEnded
|
2021-02-03 09:01:08 +01:00
|
|
|
case .mastodonAPIError(let error):
|
|
|
|
guard let responseError = error.mastodonError else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return responseError.failureReason
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var helpAnchor: String? {
|
|
|
|
switch errorReason {
|
|
|
|
case .authenticationMissing: return "Please request after authenticated."
|
2021-03-04 11:53:29 +01:00
|
|
|
case .badRequest: return L10n.Common.Alerts.Common.pleaseTryAgain
|
|
|
|
case .badResponse: return L10n.Common.Alerts.Common.pleaseTryAgain
|
|
|
|
case .requestThrottle: return L10n.Common.Alerts.Common.pleaseTryAgainLater
|
|
|
|
case .voteExpiredPoll: return nil
|
2021-02-03 09:01:08 +01:00
|
|
|
case .mastodonAPIError(let error):
|
|
|
|
guard let responseError = error.mastodonError else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return responseError.helpAnchor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|