From fd6ac93ed7d1e5c4dc3893c88b3fe140202f7745 Mon Sep 17 00:00:00 2001 From: Rafael Escaleira Date: Fri, 25 Oct 2024 19:32:33 -0400 Subject: [PATCH] New RefdsError --- Sources/RefdsShared/Error/RefdsError.swift | 58 +++++++------------ .../Extension/EncodableExtensions.swift | 2 +- .../Extension/ErrorExtensions.swift | 2 +- .../RefdsShared/Extension/URLExtensions.swift | 2 +- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/Sources/RefdsShared/Error/RefdsError.swift b/Sources/RefdsShared/Error/RefdsError.swift index 6f61876..a3e4f51 100644 --- a/Sources/RefdsShared/Error/RefdsError.swift +++ b/Sources/RefdsShared/Error/RefdsError.swift @@ -1,47 +1,33 @@ import Foundation -public enum RefdsError: - Error, - CustomStringConvertible, - Equatable, - RefdsLogger, - RefdsAlert -{ - case decodedError(type: Decodable.Type) - case encodedError(type: Encodable.Type) - case requestError(error: Error) - case notFound(type: Any.Type) - case custom(message: String) +public enum RefdsError: Error, LocalizedError { + case with(message: String) - public var description: String { - switch self { - case .decodedError(let decodedType): return "Error on decoded - \(decodedType.self)" - case .encodedError(let encodedType): return "Error on encoded - \(encodedType.self)" - case .requestError(let description): return "Error on request - \(description)" - case .notFound(let type): return "Error - not found - \(type.self)" - case .custom(let message): return "Custom Error - \(message)" - } + public static func decode(for type: T.Type) -> Error { + DecodingError.typeMismatch( + T.self, + .init( + codingPath: [], + debugDescription: "Decoding failed for \(type)" + ) + ) } - public var title: String? { - switch self { - case .decodedError: return "Decoded Error" - case .encodedError: return "Encoded Error" - case .requestError: return "Request Error" - case .notFound: return "Not Found" - case .custom: return "Unknown error" - } + public static func encode(for type: T.Type) -> Error { + EncodingError.invalidValue( + T.self, + .init( + codingPath: [], + debugDescription: "Encoding failed for \(type)" + ) + ) } - public var message: String? { - description + public static func request(for code: URLError.Code) -> Error { + URLError(code) } - public static func == (lhs: RefdsError, rhs: RefdsError) -> Bool { - lhs.description == rhs.description - } - - public func logger() async { - await Self.loggerInstance.error(message: description) + public static func cocoa(code: CocoaError.Code) -> Error { + CocoaError(code) } } diff --git a/Sources/RefdsShared/Extension/EncodableExtensions.swift b/Sources/RefdsShared/Extension/EncodableExtensions.swift index 3df7ace..4d57d9b 100755 --- a/Sources/RefdsShared/Extension/EncodableExtensions.swift +++ b/Sources/RefdsShared/Extension/EncodableExtensions.swift @@ -13,7 +13,7 @@ public extension Encodable { else { return ( success: false, - content: RefdsError.encodedError(type: Self.self).description + content: RefdsError.encode(for: Self.self).localizedDescription ) } return (success: true, content: string) diff --git a/Sources/RefdsShared/Extension/ErrorExtensions.swift b/Sources/RefdsShared/Extension/ErrorExtensions.swift index 64dd969..20f867a 100644 --- a/Sources/RefdsShared/Extension/ErrorExtensions.swift +++ b/Sources/RefdsShared/Extension/ErrorExtensions.swift @@ -2,6 +2,6 @@ import Foundation public extension Error { var refdsError: RefdsError { - .custom(message: localizedDescription) + .with(message: self.localizedDescription) } } diff --git a/Sources/RefdsShared/Extension/URLExtensions.swift b/Sources/RefdsShared/Extension/URLExtensions.swift index 88a0d3f..d9c7103 100644 --- a/Sources/RefdsShared/Extension/URLExtensions.swift +++ b/Sources/RefdsShared/Extension/URLExtensions.swift @@ -3,7 +3,7 @@ import Foundation public extension URL { static func storeURL(for appGroup: String, databaseName: String) throws -> URL { guard let fileContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else { - throw RefdsError.notFound(type: FileManager.self) + throw RefdsError.cocoa(code: .fileNoSuchFile) } return fileContainer.appendingPathComponent("\(databaseName).sqlite") }