summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2022-02-17 13:18:34 +0100
committerAndrej Mihajlov <and@mullvad.net>2022-02-22 11:24:14 +0100
commitdcb8a8a29734dce970025c122c3402640fd215ae (patch)
tree62a3d6630bc24bee9fcac9fac52c8cc31592651e /ios/MullvadVPN
parent2923b851ec06c9823171559c3f4334bcca22f6ae (diff)
downloadmullvadvpn-dcb8a8a29734dce970025c122c3402640fd215ae.tar.xz
mullvadvpn-dcb8a8a29734dce970025c122c3402640fd215ae.zip
Implement custom error messages for Coding errors
Diffstat (limited to 'ios/MullvadVPN')
-rw-r--r--ios/MullvadVPN/CodingErrors+ChainedError.swift53
1 files changed, 53 insertions, 0 deletions
diff --git a/ios/MullvadVPN/CodingErrors+ChainedError.swift b/ios/MullvadVPN/CodingErrors+ChainedError.swift
new file mode 100644
index 0000000000..e14d61473b
--- /dev/null
+++ b/ios/MullvadVPN/CodingErrors+ChainedError.swift
@@ -0,0 +1,53 @@
+//
+// CodingErrors+ChainedError.swift
+// MullvadVPN
+//
+// Created by pronebird on 17/02/2022.
+// Copyright © 2022 Mullvad VPN AB. All rights reserved.
+//
+
+import Foundation
+
+extension DecodingError: CustomChainedErrorDescriptionProtocol {
+ var customErrorDescription: String? {
+ switch self {
+ case .typeMismatch(let type, let context):
+ return "Type mismatch, expected \(type) for key at \"\(context.codingPath.codingPathString)\"."
+
+ case .valueNotFound(_, let context):
+ return "Value not found at \"\(context.codingPath.codingPathString)\"."
+
+ case .keyNotFound(let codingKey, let context):
+ return "Key \"\(codingKey.stringValue)\" not found at \"\(context.codingPath.codingPathString)\"."
+
+ case .dataCorrupted:
+ return "Data corrupted."
+
+ @unknown default:
+ return nil
+ }
+ }
+}
+
+extension EncodingError: CustomChainedErrorDescriptionProtocol {
+ var customErrorDescription: String? {
+ switch self {
+ case .invalidValue(_, let context):
+ return "Invalid value at \"\(context.codingPath.codingPathString)\""
+
+ @unknown default:
+ return nil
+ }
+ }
+}
+
+private extension Array where Element == CodingKey {
+ var codingPathString: String {
+ if isEmpty {
+ return "<root>"
+ } else {
+ return map { $0.stringValue }
+ .joined(separator: ".")
+ }
+ }
+}