Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makes Error extensible #48

Merged
merged 1 commit into from
Apr 20, 2021
Merged

makes Error extensible #48

merged 1 commit into from
Apr 20, 2021

Conversation

dnadoba
Copy link
Member

@dnadoba dnadoba commented Apr 15, 2021

Allows user code to add custom error codes

Motivation:

A user should be able to easily define and use custom application errors

Modifications:

  • Error is now a struct instead of an enum with two stored properties code and message
  • Error.Code is a new struct which is a thin wrapper around a UInt32 which allows to add custom error codes through extensions

Result:

A user can now define custom errors by writing two extensions:

extension Error.Code {
    static let myCustomError = Self(rawValue: 0x00000301)
}
extension Error {
    static func myCustomError(message: String) -> Self { .init(code: .myCustomError, message: message ) }
}

Custom Errors can be used in the same places as predefined errors and behave the same too.

Signed-off-by: David Nadoba <dnadoba@gmail.com>
Copy link
Member

@OlegDokuka OlegDokuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@OlegDokuka OlegDokuka changed the title Make Error Extensible makes Error extensible Apr 20, 2021
@OlegDokuka OlegDokuka merged commit 1bbc05f into main Apr 20, 2021
@OlegDokuka OlegDokuka deleted the custom-errors branch April 20, 2021 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants