-
Notifications
You must be signed in to change notification settings - Fork 217
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
Conversation
There was a problem hiding this 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 :)
lib/bech32/bech32.cabal
Outdated
@@ -31,7 +31,9 @@ library | |||
build-depends: | |||
array | |||
, base | |||
, bifunctors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bifunctors are already available in base
: https://hackage.haskell.org/package/base-4.11.1.0/docs/Data-Bifunctor.html
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically
when
fromControl.Monad
when condition $ return MyError
It's very close! Tantalizingly close. :)
53f6a9b
to
2e3085e
Compare
Issue Number
Issue #238
Overview
Currently, the Bech32
encode
,decode
, andmkHumanReadablePart
functions indicate failure by evaluating toNothing
. 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 ofMaybe
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.)