Skip to content

Commit

Permalink
New RefdsError
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelesantos committed Oct 25, 2024
1 parent 2c78a6b commit fd6ac93
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
58 changes: 22 additions & 36 deletions Sources/RefdsShared/Error/RefdsError.swift
Original file line number Diff line number Diff line change
@@ -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<T>(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<T>(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)
}
}
2 changes: 1 addition & 1 deletion Sources/RefdsShared/Extension/EncodableExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/RefdsShared/Extension/ErrorExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Foundation

public extension Error {
var refdsError: RefdsError {
.custom(message: localizedDescription)
.with(message: self.localizedDescription)
}
}
2 changes: 1 addition & 1 deletion Sources/RefdsShared/Extension/URLExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit fd6ac93

Please sign in to comment.