-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
19 changed files
with
444 additions
and
50 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/// ``TheError`` for corrupted data errors. | ||
public struct DataCorrupted: TheError { | ||
|
||
/// Create instance of ``DataCorrupted`` error. | ||
/// | ||
/// - Parameters: | ||
/// - message: Message associated with this error. | ||
/// Default value is "DataCorrupted". | ||
/// - displayableMessage: Message which can be displayed | ||
/// to the end user. Default value is based on ``TheErrorDisplayableMessages``. | ||
/// - file: Source code file identifier. | ||
/// Filled automatically based on compile time constants. | ||
/// - line: Line in given source code file. | ||
/// Filled automatically based on compile time constants. | ||
/// - Returns: New instance of ``DataCorrupted`` error with given context. | ||
public static func error( | ||
message: StaticString, | ||
displayableMessage: DisplayableString = TheErrorDisplayableMessages.message(for: Self.self), | ||
file: StaticString = #fileID, | ||
line: UInt = #line | ||
) -> Self { | ||
Self( | ||
context: .context( | ||
message: message, | ||
file: file, | ||
line: line | ||
), | ||
displayableString: displayableMessage | ||
) | ||
} | ||
|
||
/// Source code context of this error. | ||
public var context: SourceCodeContext | ||
/// String representation displayable to the end user. | ||
public var displayableString: DisplayableString | ||
} |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// ``TheError`` for invalid value errors. | ||
/// | ||
/// ``InvalidValue`` error can occur when trying to use invalid value. | ||
/// It can be use both to indicate data validation issues | ||
/// as well as passing invalid function arguments. | ||
public struct InvalidValue: TheError { | ||
|
||
/// Create instance of ``InvalidValue`` error. | ||
/// | ||
/// - Parameters: | ||
/// - message: Message associated with this error. | ||
/// Should descrive validation rule which was not passing. | ||
/// - displayableMessage: Message which can be displayed | ||
/// to the end user. Default value is based on ``TheErrorDisplayableMessages``. | ||
/// - value: Value which was invalid. | ||
/// - file: Source code file identifier. | ||
/// Filled automatically based on compile time constants. | ||
/// - line: Line in given source code file. | ||
/// Filled automatically based on compile time constants. | ||
/// - Returns: New instance of ``InvalidValue`` error with given context. | ||
public static func error<Value>( | ||
message: StaticString, | ||
displayableMessage: DisplayableString = TheErrorDisplayableMessages.message(for: Self.self), | ||
value: Value, | ||
file: StaticString = #fileID, | ||
line: UInt = #line | ||
) -> Self { | ||
Self( | ||
context: .context( | ||
message: message, | ||
file: file, | ||
line: line | ||
), | ||
displayableString: displayableMessage, | ||
value: value | ||
) | ||
} | ||
|
||
/// Source code context of this error. | ||
public var context: SourceCodeContext | ||
/// String representation displayable to the end user. | ||
public var displayableString: DisplayableString | ||
/// Value which was recognized as invalid. | ||
public let value: Any | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/// ``TheError`` for key not found errors. | ||
public struct KeyNotFound<Key>: TheError { | ||
|
||
/// Create instance of ``KeyNotFound`` error. | ||
/// | ||
/// - Parameters: | ||
/// - message: Message associated with this error. | ||
/// Default value is "KeyNotFound". | ||
/// - displayableMessage: Message which can be displayed | ||
/// to the end user. Default value is based on ``TheErrorDisplayableMessages``. | ||
/// - key: Missing key. | ||
/// - file: Source code file identifier. | ||
/// Filled automatically based on compile time constants. | ||
/// - line: Line in given source code file. | ||
/// Filled automatically based on compile time constants. | ||
/// - Returns: New instance of ``KeyNotFound`` error with given context. | ||
public static func error( | ||
message: StaticString, | ||
displayableMessage: DisplayableString = TheErrorDisplayableMessages.message(for: Self.self), | ||
key: Key, | ||
file: StaticString = #fileID, | ||
line: UInt = #line | ||
) -> Self { | ||
Self( | ||
context: .context( | ||
message: message, | ||
file: file, | ||
line: line | ||
), | ||
displayableString: displayableMessage, | ||
key: key | ||
) | ||
} | ||
|
||
/// Source code context of this error. | ||
public var context: SourceCodeContext | ||
/// String representation displayable to the end user. | ||
public var displayableString: DisplayableString | ||
/// Missing key. | ||
public let key: Key | ||
} |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// ``TheError`` for type mismatch errors. | ||
public struct TypeMismatch: TheError { | ||
|
||
/// Create instance of ``TypeMismatch`` error. | ||
/// | ||
/// - Parameters: | ||
/// - message: Message associated with this error. | ||
/// Default value is "TypeMismatch". | ||
/// - displayableMessage: Message which can be displayed | ||
/// to the end user. Default value is based on ``TheErrorDisplayableMessages``. | ||
/// - file: Source code file identifier. | ||
/// Filled automatically based on compile time constants. | ||
/// - line: Line in given source code file. | ||
/// Filled automatically based on compile time constants. | ||
/// - Returns: New instance of ``TypeMismatch`` error with given context. | ||
public static func error( | ||
message: StaticString, | ||
displayableMessage: DisplayableString = TheErrorDisplayableMessages.message(for: Self.self), | ||
type: Any.Type, | ||
file: StaticString = #fileID, | ||
line: UInt = #line | ||
) -> Self { | ||
Self( | ||
context: .context( | ||
message: message, | ||
file: file, | ||
line: line | ||
), | ||
displayableString: displayableMessage, | ||
type: type | ||
) | ||
} | ||
|
||
/// Source code context of this error. | ||
public var context: SourceCodeContext | ||
/// String representation displayable to the end user. | ||
public var displayableString: DisplayableString | ||
/// Type that was required. | ||
public let type: Any.Type | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// ``TheError`` for value not found errors. | ||
public struct ValueNotFound: TheError { | ||
|
||
/// Create instance of ``ValueNotFound`` error. | ||
/// | ||
/// - Parameters: | ||
/// - message: Message associated with this error. | ||
/// Default value is "ValueNotFound". | ||
/// - displayableMessage: Message which can be displayed | ||
/// to the end user. Default value is based on ``TheErrorDisplayableMessages``. | ||
/// - file: Source code file identifier. | ||
/// Filled automatically based on compile time constants. | ||
/// - line: Line in given source code file. | ||
/// Filled automatically based on compile time constants. | ||
/// - Returns: New instance of ``ValueNotFound`` error with given context. | ||
public static func error( | ||
message: StaticString, | ||
displayableMessage: DisplayableString = TheErrorDisplayableMessages.message(for: Self.self), | ||
type: Any.Type, | ||
file: StaticString = #fileID, | ||
line: UInt = #line | ||
) -> Self { | ||
Self( | ||
context: .context( | ||
message: message, | ||
file: file, | ||
line: line | ||
), | ||
displayableString: displayableMessage, | ||
type: type | ||
) | ||
} | ||
|
||
/// Source code context of this error. | ||
public var context: SourceCodeContext | ||
/// String representation displayable to the end user. | ||
public var displayableString: DisplayableString | ||
/// Type of missing value. | ||
public let type: Any.Type | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// Type convertible to ``TheError``. | ||
/// | ||
/// Type which can be representable by instance of ``TheError``. | ||
/// This conversion will be executed automatically when using | ||
/// ``asTheError`` error conversion. | ||
public protocol TheErrorConvertible { | ||
|
||
func convertToTheError( | ||
file: StaticString, | ||
line: UInt | ||
) -> TheError | ||
} |
57 changes: 57 additions & 0 deletions
57
Sources/MQ/Extensions/Codable/DecodingError+TheError.swift
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
extension DecodingError: TheErrorConvertible { | ||
|
||
public func convertToTheError( | ||
file: StaticString, | ||
line: UInt | ||
) -> TheError { | ||
switch self { | ||
case .valueNotFound(let type, let context): | ||
return ValueNotFound | ||
.error( | ||
message: "Trying to decode missing value", | ||
type: type, | ||
file: file, | ||
line: line | ||
) | ||
.with(context, for: "decoding context") | ||
|
||
case .dataCorrupted(let context): | ||
return DataCorrupted | ||
.error( | ||
message: "Trying to decode corrupted data", | ||
file: file, | ||
line: line | ||
) | ||
.with(context, for: "decoding context") | ||
|
||
case .typeMismatch(let type, let context): | ||
return TypeMismatch | ||
.error( | ||
message: "Trying to decode invalid value", | ||
type: type, | ||
file: file, | ||
line: line | ||
) | ||
.with(context, for: "decoding context") | ||
|
||
case .keyNotFound(let key, let context): | ||
return KeyNotFound | ||
.error( | ||
message: "Trying to decode missing value", | ||
key: key, | ||
file: file, | ||
line: line | ||
) | ||
.with(context, for: "decoding context") | ||
|
||
@unknown case _: | ||
return Unexpected | ||
.error( | ||
message: "Unknown decoding error", | ||
underlyingError: self, | ||
file: file, | ||
line: line | ||
) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
Sources/MQ/Extensions/Codable/EncodingError+TheErrorConvertible.swift
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
extension EncodingError: TheErrorConvertible { | ||
|
||
public func convertToTheError( | ||
file: StaticString, | ||
line: UInt | ||
) -> TheError { | ||
switch self { | ||
case .invalidValue(let value, let context): | ||
return InvalidValue | ||
.error( | ||
message: "Trying to encode invalid value", | ||
value: value, | ||
file: file, | ||
line: line | ||
) | ||
.with(context, for: "encoding context") | ||
|
||
@unknown case _: | ||
return Unexpected | ||
.error( | ||
message: "Unknown encoding error", | ||
underlyingError: self, | ||
file: file, | ||
line: line | ||
) | ||
} | ||
} | ||
} |
File renamed without changes.
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.