2021-01-27 11:46:14 +01:00
|
|
|
//
|
|
|
|
// Mastodon+API+Error.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021/1/26.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import enum NIOHTTP1.HTTPResponseStatus
|
|
|
|
|
|
|
|
extension Mastodon.API {
|
|
|
|
public struct Error: Swift.Error {
|
|
|
|
|
|
|
|
public var httpResponseStatus: HTTPResponseStatus
|
2021-01-28 07:52:35 +01:00
|
|
|
public var mastodonError: MastodonError?
|
2021-01-27 11:46:14 +01:00
|
|
|
|
|
|
|
init(
|
|
|
|
httpResponseStatus: HTTPResponseStatus,
|
2021-01-28 07:52:35 +01:00
|
|
|
mastodonError: Mastodon.API.Error.MastodonError?
|
2021-01-27 11:46:14 +01:00
|
|
|
) {
|
|
|
|
self.httpResponseStatus = httpResponseStatus
|
2021-01-28 07:52:35 +01:00
|
|
|
self.mastodonError = mastodonError
|
2021-01-27 11:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
init(
|
|
|
|
httpResponseStatus: HTTPResponseStatus,
|
2021-01-28 07:52:35 +01:00
|
|
|
error: Mastodon.Entity.Error
|
2021-01-27 11:46:14 +01:00
|
|
|
) {
|
|
|
|
self.init(
|
|
|
|
httpResponseStatus: httpResponseStatus,
|
2021-01-28 07:52:35 +01:00
|
|
|
mastodonError: MastodonError(error: error)
|
2021-01-27 11:46:14 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 12:54:08 +01:00
|
|
|
|
|
|
|
extension Mastodon.API.Error: LocalizedError {
|
|
|
|
|
|
|
|
public var errorDescription: String? {
|
|
|
|
guard let mastodonError = mastodonError else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch mastodonError {
|
|
|
|
case .generic(let error):
|
|
|
|
return error.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var failureReason: String? {
|
|
|
|
guard let mastodonError = mastodonError else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch mastodonError {
|
|
|
|
case .generic(let error):
|
|
|
|
return error.errorDescription
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|