generated from rafaelesantos/swift-readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c78a6b
commit fd6ac93
Showing
4 changed files
with
25 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters