-
Notifications
You must be signed in to change notification settings - Fork 49
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
45634e6
commit 88553a3
Showing
5 changed files
with
149 additions
and
35 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,25 +1,67 @@ | ||
/// Represents various errors that can occur when attempting to unwrap an optional value. | ||
public enum ImperialError: Error, CustomStringConvertible { | ||
|
||
public struct ImperialError: Error, Sendable, Equatable { | ||
public struct ErrorType: Sendable, Hashable, CustomStringConvertible, Equatable { | ||
enum Base: String, Sendable, Equatable { | ||
case missingEnvVar | ||
} | ||
|
||
let base: Base | ||
|
||
private init(_ base: Base) { | ||
self.base = base | ||
} | ||
|
||
public static let missingEnvVar = Self(.missingEnvVar) | ||
|
||
public var description: String { | ||
base.rawValue | ||
} | ||
} | ||
|
||
private struct Backing: Sendable, Equatable { | ||
fileprivate let errorType: ErrorType | ||
fileprivate let variable: String? | ||
|
||
init(errorType: ErrorType, variable: String? = nil) { | ||
self.errorType = errorType | ||
self.variable = variable | ||
} | ||
|
||
static func == (lhs: ImperialError.Backing, rhs: ImperialError.Backing) -> Bool { | ||
lhs.errorType == rhs.errorType | ||
} | ||
} | ||
|
||
private var backing: Backing | ||
|
||
public var errorType: ErrorType { backing.errorType } | ||
public var variable: String? { backing.variable } | ||
|
||
private init(backing: Backing) { | ||
self.backing = backing | ||
} | ||
|
||
/// Thrown when no environment varibale is found with a given name. | ||
/// - warning: This error is never thrown; rather, the application will fatal error. | ||
case missingEnvVar(String) | ||
|
||
/// Thrown when we attempt to create a `FederatedCreatable` model and there is | ||
/// no JSON in the response from the the request to `dataUri`. | ||
case missingJSONFromResponse(String) | ||
|
||
|
||
|
||
/// Thrown when `request.fetch` is called with a type that has not been run through `request.create`. | ||
case typeNotInitialized(String) | ||
|
||
public static func missingEnvVar(_ variable: String) -> Self { | ||
.init(backing: .init(errorType: .missingEnvVar, variable: variable)) | ||
} | ||
|
||
public static func == (lhs: ImperialError, rhs: ImperialError) -> Bool { | ||
lhs.backing == rhs.backing | ||
} | ||
} | ||
|
||
extension ImperialError: CustomStringConvertible { | ||
/// A human readable version of the error thrown. | ||
public var description: String { | ||
switch self { | ||
case let .missingEnvVar(variable): return "Missing enviroment variable '\(variable)'" | ||
case let .missingJSONFromResponse(uri): return "Reponse returned from '\(uri)' does not contain JSON" | ||
case let .typeNotInitialized(type): return "No instence of type '\(type)' has been created" | ||
var result = #"ImperialError(errorType: \#(self.errorType)"# | ||
|
||
if let variable { | ||
result.append(", missing enviroment variable: \(variable)") | ||
} | ||
|
||
result.append(")") | ||
|
||
return result | ||
} | ||
} |
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,20 +1,84 @@ | ||
/// Represents an error that occurs during a service action. | ||
public enum ServiceError: Error, CustomStringConvertible { | ||
|
||
public struct ServiceError: Error, Sendable, Equatable { | ||
public struct ErrorType: Sendable, Hashable, CustomStringConvertible, Equatable { | ||
enum Base: String, Sendable, Equatable { | ||
case noServiceFound | ||
case noServiceEndpoint | ||
} | ||
|
||
let base: Base | ||
|
||
private init(_ base: Base) { | ||
self.base = base | ||
} | ||
|
||
public static let noServiceFound = Self(.noServiceFound) | ||
public static let noServiceEndpoint = Self(.noServiceEndpoint) | ||
|
||
public var description: String { | ||
base.rawValue | ||
} | ||
} | ||
|
||
private struct Backing: Sendable, Equatable { | ||
fileprivate let errorType: ErrorType | ||
fileprivate let name: String? | ||
fileprivate let endpoint: String? | ||
|
||
init( | ||
errorType: ErrorType, | ||
name: String? = nil, | ||
endpoint: String? = nil | ||
) { | ||
self.errorType = errorType | ||
self.name = name | ||
self.endpoint = endpoint | ||
} | ||
|
||
static func == (lhs: ServiceError.Backing, rhs: ServiceError.Backing) -> Bool { | ||
lhs.errorType == rhs.errorType | ||
} | ||
} | ||
|
||
private var backing: Backing | ||
|
||
public var errorType: ErrorType { backing.errorType } | ||
public var name: String? { backing.name } | ||
public var endpoint: String? { backing.endpoint } | ||
|
||
private init(backing: Backing) { | ||
self.backing = backing | ||
} | ||
|
||
/// Thrown when no service is registered with a given name. | ||
case noServiceFound(String) | ||
|
||
/// Thrown when no `FederatedSewrvice` type is found whgen creating a `Service` from JSON. | ||
case noExistingService(String) | ||
|
||
public static func noServiceFound(_ name: String) -> Self { | ||
.init(backing: .init(errorType: .noServiceFound, name: name)) | ||
} | ||
|
||
/// Thrown when a `FederatedCreatable` type has a `serviceKey` that does not match any available endpoints in the service. | ||
case noServiceEndpoint(String) | ||
|
||
public static func noServiceEndpoint(_ endpoint: String) -> Self { | ||
.init(backing: .init(errorType: .noServiceEndpoint, endpoint: endpoint)) | ||
} | ||
|
||
public static func == (lhs: ServiceError, rhs: ServiceError) -> Bool { | ||
lhs.backing == rhs.backing | ||
} | ||
} | ||
|
||
extension ServiceError: CustomStringConvertible { | ||
public var description: String { | ||
switch self { | ||
case let .noServiceFound(name): return "No service was found with the name '\(name)'" | ||
case let .noExistingService(name): return "No service exists with the name '\(name)'" | ||
case let .noServiceEndpoint(endpoint): return "Service does not have available endpoint for key '\(endpoint)'" | ||
var result = #"ServiceError(errorType: \#(self.errorType)"# | ||
|
||
if let name { | ||
result.append(", no service was found with the name: \(name)") | ||
} | ||
|
||
if let endpoint { | ||
result.append(", service does not have available endpoint for key: \(endpoint)") | ||
} | ||
|
||
result.append(")") | ||
|
||
return result | ||
} | ||
} |
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