0.1.0 • Published 2 years ago
p-allay v0.1.0
p-allay
Allay expected promise rejections: Resolve with your failure value instead.
API
This module exports an object that holds these methods:
.ifPropDef(key, pr)
Without pr
missing or false-y,
returns a function allayIfPropDef(err)
that will:
- Lookup
err[key]
, and if it's notundefined
, return it. - Otherwise, throw something:
- If
err
is an object, throw that. - If
err
is a non-empty string, throw anew Error(err)
. - If neither, throw a
TypeError
that complains abouterr
.
- If
If pr
is true-y, instead return the result of calling
pr.then(null, allayIfPropDef)
.
.byCode(dict, pr)
Returns a function allayByCode(err)
that will:
- Try to look up an
err.code
-named own property in dictionary objectdict
. If found, and its value is notundefined
, return that value. - Otherwise, throw as described for
.ifPropDef
.
pr
works like with .ifPropDef
.
.eNoEnt(pr)
Shorthand for .byCode({ ENOENT: false }, pr)
.
.eIsDir(pr)
Shorthand for .byCode({ EISDIR: false }, pr)
.
Usage
see test/usage.mjs.
Known issues
- Needs more/better tests and docs.
License
ISC
0.1.0
2 years ago