Skip to content

Commit

Permalink
Add TheErrorConvertible
Browse files Browse the repository at this point in the history
  • Loading branch information
KaQuMiQ authored Jun 7, 2023
1 parent a8a6fe3 commit bb7df4b
Show file tree
Hide file tree
Showing 19 changed files with 444 additions and 50 deletions.
36 changes: 36 additions & 0 deletions Sources/MQ/Errors/Commons/DataCorrupted.swift
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
}
45 changes: 45 additions & 0 deletions Sources/MQ/Errors/Commons/InvalidValue.swift
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
}
41 changes: 41 additions & 0 deletions Sources/MQ/Errors/Commons/KeyNotFound.swift
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.
40 changes: 40 additions & 0 deletions Sources/MQ/Errors/Commons/TypeMismatch.swift
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.
40 changes: 40 additions & 0 deletions Sources/MQ/Errors/Commons/ValueNotFound.swift
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
}
50 changes: 0 additions & 50 deletions Sources/MQ/Errors/StringEncodingFailure.swift

This file was deleted.

12 changes: 12 additions & 0 deletions Sources/MQ/Errors/TheErrorConvertible.swift
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 Sources/MQ/Extensions/Codable/DecodingError+TheError.swift
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
)
}
}
}
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
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ extension Error {
case let theError as TheError:
return theError

case let convertible as TheErrorConvertible:
return convertible
.convertToTheError(
file: file,
line: line
)

case let error:
return customConversion(
Unidentified
Expand Down
Loading

0 comments on commit bb7df4b

Please sign in to comment.