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

Use richer errors for Bech32 encoder and decoder #271

Merged
merged 3 commits into from
May 16, 2019

Conversation

jonathanknowles
Copy link
Member

Issue Number

Issue #238

Overview

Currently, the Bech32 encode, decode, and mkHumanReadablePart functions indicate failure by evaluating to Nothing. Although this solution is simple, it isn't very helpful for when we want to give feedback about the exact cause of an error.

This PR uses Either instead of Maybe to indicate failure, and adds a set of error conditions.

Comments

This PR doesn't include the ability to spot checksum errors. (That will be included in a later PR.)

@jonathanknowles jonathanknowles changed the title WIP: use richer errors for Bech32 encoder and decoder Use richer errors for Bech32 encoder and decoder May 16, 2019
@jonathanknowles jonathanknowles self-assigned this May 16, 2019
Copy link
Member

@KtorZ KtorZ left a comment

Choose a reason for hiding this comment

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

Some minor remarks, but looks indeed better :)

@@ -31,7 +31,9 @@ library
build-depends:
array
, base
, bifunctors
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Nicely spotted. I hadn't realized they were available in base.

guardE (length dcp >= checksumLength)
StringToDecodeTooShort
guardE (bech32VerifyChecksum hrp dcp)
StringToDecodeContainsInvalidChars
Copy link
Member

Choose a reason for hiding this comment

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

This looks actually clearer than before :)

-------------------------------------------------------------------------------}

guardE :: Bool -> e -> Either e ()
guardE b e = if b then Right () else Left e
Copy link
Member

Choose a reason for hiding this comment

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

This is basically when from Control.Monad

when condition $
    return MyError

Copy link
Member Author

Choose a reason for hiding this comment

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

This is basically when from Control.Monad

when condition $
    return MyError

It's very close! Tantalizingly close. :)

@KtorZ KtorZ force-pushed the jonathanknowles/bech32-errors branch from 53f6a9b to 2e3085e Compare May 16, 2019 12:01
@KtorZ KtorZ merged commit 368c6db into master May 16, 2019
@KtorZ KtorZ deleted the jonathanknowles/bech32-errors branch May 16, 2019 15:18
@KtorZ KtorZ mentioned this pull request May 17, 2019
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.

None yet

2 participants