This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from vapor/singleton-updates
singleton updates
- Loading branch information
Showing
12 changed files
with
91 additions
and
210 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,39 @@ | ||
import Foundation | ||
import Debugging | ||
|
||
public struct ServiceError: Error, Debuggable, Encodable { | ||
var problem: Problem | ||
|
||
init(_ problem: Problem) { | ||
self.problem = problem | ||
} | ||
|
||
enum Problem { | ||
case other(identifier: String, reason: String) | ||
case multipleInstances( | ||
type: Any.Type | ||
) | ||
case disambiguationRequired( | ||
key: String, | ||
available: [String], | ||
type: Any.Type | ||
) | ||
case unknownService( | ||
available: [String], | ||
type: Any.Type | ||
) | ||
case incorrectType( | ||
type: Any.Type, | ||
desired: Any.Type | ||
) | ||
case noneAvailable(type: Any.Type) | ||
case unknown(Error) | ||
} | ||
} | ||
|
||
extension ServiceError { | ||
public var reason: String { | ||
switch self.problem { | ||
case .other(_, let reason): | ||
return reason | ||
case .multipleInstances(let type): | ||
return "Multiple instances available for '\(type)'. Unable to disambiguate." | ||
case .noneAvailable(let type): | ||
return "No services are available for '\(type)'" | ||
case .disambiguationRequired(_, _, let type): | ||
return "Multiple services available for '\(type)', please disambiguate using config." | ||
case .unknownService(_, let type): | ||
return "No service was found while making a `\(type)`." | ||
case .incorrectType(let type, let desired): | ||
return "Service factory for `\(type)` did not create a service that conforms to `\(desired)`." | ||
case .unknown(let error): | ||
return "Unknown: \(error)" | ||
} | ||
} | ||
/// An error using Services. | ||
public struct ServiceError: Debuggable { | ||
public static let readableName = "Service Error" | ||
public let identifier: String | ||
public var reason: String | ||
public var file: String | ||
public var function: String | ||
public var line: UInt | ||
public var column: UInt | ||
public var stackTrace: [String] | ||
public var possibleCauses: [String] | ||
public var suggestedFixes: [String] | ||
|
||
public var identifier: String { | ||
switch self.problem { | ||
case .other(let identifier, _): | ||
return identifier | ||
case .multipleInstances: | ||
return "multipleInstances" | ||
case .noneAvailable: | ||
return "none" | ||
case .disambiguationRequired: | ||
return "disambiguationRequired" | ||
case .unknownService: | ||
return "unknownService" | ||
case .incorrectType: | ||
return "incorrectType" | ||
case .unknown: | ||
return "unknown" | ||
} | ||
} | ||
|
||
public var possibleCauses: [String] { | ||
switch self.problem { | ||
case .other(_): | ||
return [] | ||
case .multipleInstances: | ||
return [] | ||
case .noneAvailable: | ||
return [ | ||
"A provider for this service was not properly configured." | ||
] | ||
case .disambiguationRequired: | ||
return [] | ||
case .unknownService: | ||
return [] | ||
case .incorrectType: | ||
return [] | ||
case .unknown: | ||
return [] | ||
} | ||
} | ||
|
||
public var suggestedFixes: [String] { | ||
switch self.problem { | ||
case .other(_): | ||
return [] | ||
case .multipleInstances: | ||
return [ | ||
"Register instances as service types instead, so they can be disambiguated using config." | ||
] | ||
case .noneAvailable(let type): | ||
return [ | ||
"Register a service that conforms to '\(type)' to the Container." | ||
] | ||
case .disambiguationRequired(let key, let available, _): | ||
return [ | ||
"Specify one of the available services in `app.json` at key `\(key)`.", | ||
"Use `try config.set(\"app\", \"\(key)\", to: \"<service>\")` with one of the available service names.", | ||
"Available services: \(available)" | ||
] | ||
case .unknownService(let available, _): | ||
let string = available.joined(separator: ", ") | ||
return ["Try using one of the available types: \(string)"] | ||
case .incorrectType: | ||
return [] | ||
case .unknown: | ||
return [] | ||
} | ||
/// Creates a new Apple TLS error | ||
public init( | ||
identifier: String, | ||
reason: String, | ||
possibleCauses: [String] = [], | ||
suggestedFixes: [String] = [], | ||
file: String = #file, | ||
function: String = #function, | ||
line: UInt = #line, | ||
column: UInt = #column | ||
) { | ||
self.identifier = identifier | ||
self.reason = reason | ||
self.file = file | ||
self.function = function | ||
self.line = line | ||
self.column = column | ||
self.stackTrace = ServiceError.makeStackTrace() | ||
self.possibleCauses = possibleCauses | ||
self.suggestedFixes = suggestedFixes | ||
} | ||
} | ||
|
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
Oops, something went wrong.