From 0cb8d1bf6e35606d6e41dbb4e4110aaa9fe94634 Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Mon, 1 Mar 2021 15:40:40 +0800 Subject: [PATCH] chore: add error detail --- .../Entity/Mastodon+Entity+Error.swift | 4 +- .../Entity/Mastodon+Entity+ErrorDetail.swift | 101 ++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ErrorDetail.swift diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Error.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Error.swift index 9d5ae2a5..a3602574 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Error.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Error.swift @@ -19,10 +19,12 @@ extension Mastodon.Entity { public struct Error: Codable { public let error: String public let errorDescription: String? - + public let details: ErrorDetail? + enum CodingKeys: String, CodingKey { case error case errorDescription = "error_description" + case details } } } diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ErrorDetail.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ErrorDetail.swift new file mode 100644 index 00000000..397475e0 --- /dev/null +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+ErrorDetail.swift @@ -0,0 +1,101 @@ +// +// Mastodon+Entity+ErrorDetail.swift +// +// +// Created by sxiaojian on 2021/3/1. +// + +import Foundation +extension Mastodon.Entity.Error { + /// ERR_BLOCKED When e-mail provider is not allowed + /// ERR_UNREACHABLE When e-mail address does not resolve to any IP via DNS (MX, A, AAAA) + /// ERR_TAKEN When username or e-mail are already taken + /// ERR_RESERVED When a username is reserved, e.g. "webmaster" or "admin" + /// ERR_ACCEPTED When agreement has not been accepted + /// ERR_BLANK When a required attribute is blank + /// ERR_INVALID When an attribute is malformed, e.g. wrong characters or invalid e-mail address + /// ERR_TOO_LONG When an attribute is over the character limit + /// ERR_INCLUSION When an attribute is not one of the allowed values, e.g. unsupported locale + public enum SignUpError: RawRepresentable, Codable { + case ERR_BLOCKED + case ERR_UNREACHABLE + case ERR_TAKEN + case ERR_RESERVED + case ERR_ACCEPTED + case ERR_BLANK + case ERR_INVALID + case ERR_TOO_LONG + case ERR_TOO_SHORT + case ERR_INCLUSION + case _other(String) + + public init?(rawValue: String) { + switch rawValue { + case "ERR_BLOCKED": self = .ERR_BLOCKED + case "ERR_UNREACHABLE": self = .ERR_UNREACHABLE + case "ERR_TAKEN": self = .ERR_TAKEN + case "ERR_RESERVED": self = .ERR_RESERVED + case "ERR_ACCEPTED": self = .ERR_ACCEPTED + case "ERR_BLANK": self = .ERR_BLANK + case "ERR_INVALID": self = .ERR_INVALID + case "ERR_TOO_LONG": self = .ERR_TOO_LONG + case "ERR_TOO_SHORT": self = .ERR_TOO_SHORT + case "ERR_INCLUSION": self = .ERR_INCLUSION + + default: + self = ._other(rawValue) + } + } + + public var rawValue: String { + switch self { + case .ERR_BLOCKED: return "ERR_BLOCKED" + case .ERR_UNREACHABLE: return "ERR_UNREACHABLE" + case .ERR_TAKEN: return "ERR_TAKEN" + case .ERR_RESERVED: return "ERR_RESERVED" + case .ERR_ACCEPTED: return "ERR_ACCEPTED" + case .ERR_BLANK: return "ERR_BLANK" + case .ERR_INVALID: return "ERR_INVALID" + case .ERR_TOO_LONG: return "ERR_TOO_LONG" + case .ERR_TOO_SHORT: return "ERR_TOO_SHORT" + case .ERR_INCLUSION: return "ERR_INCLUSION" + + case ._other(let value): return value + } + } + } +} + +public struct ErrorDetail: Codable { + public let username: [ErrorDetailReson]? + public let email: [ErrorDetailReson]? + public let password: [ErrorDetailReson]? + public let agreement: [ErrorDetailReson]? + public let locale: [ErrorDetailReson]? + public let reason: [ErrorDetailReson]? + + enum CodingKeys: String, CodingKey { + case username + case email + case password + case agreement + case locale + case reason + } +} + +public struct ErrorDetailReson: Codable { + public init(error: String, errorDescription: String?) { + self.error = Mastodon.Entity.Error.SignUpError(rawValue: error) ?? ._other(error) + self.errorDescription = errorDescription + } + + public let error: Mastodon.Entity.Error.SignUpError + public let errorDescription: String? + + + enum CodingKeys: String, CodingKey { + case error + case errorDescription = "description" + } +}