[#711] Reduce the number of unsafe functions at the call site
Description
Some constructors/converters returning Either
have their unsafe counter-parts calling error
. Most of them are either (error . pretty) id
, which produces many similar functions at the call site, e.g.:
buildEpName = ...
unsafeBuildEpName = either (error . pretty) id . buildEpName
parseAddress = ...
unsafeParseAddress = either (error . pretty) id . parseAddress
To reduce duplication, we add an unsafe polymorphic converter from Either
to Unsafe
module:
unsafe :: (HasCallStack, Buildable a) => Either a b -> b
unsafe = either (error . pretty) id
As a result, all unsafeFName
usages are replaced with unsafe . fName
, e.g.
unsafeBuildEpName "a"
unsafe . buildEpName $ "a"
There are also Maybe
converters/constructors which have an unsafe counter-parts and thus use non-trivial transformations like either (error $ errorMsg <> ... <> pretty arg) id
. It's better to have them as Either
ones too. So that fromHex
, mkPos
and mkChainId
were refactored, as long as their usages.
In addition, it is also unsafeM
function introduced in morley-prelude
. It is pretty similar to unsafe
, but throws a monadic exception via fail
.
Related issue(s)
Resolves #711 (closed)
✅ Checklist for your Merge Request
Related changes (conditional)
-
Tests (see short guidelines)
-
If I added new functionality, I added tests covering it. -
If I fixed a bug, I added a regression test to prevent the bug from silently reappearing again.
-
-
Documentation
Stylistic guide (mandatory)
-
My commits comply with the following policy. -
My code complies with the style guide.