error-to
v0.0.1-0
Published
π§ β»οΈ Serialize an error into a short object or array with customizable formatter π§
Downloads
1
Maintainers
Readme
ERROR-TO
β»οΈ Serialize an error into a short object or array with customizable formatter
- π Short Readable formats
- 𧬠Custom formatters
- π¦ Zero Dependency
- π Works in ESM & CJS
- γ½οΈ Minimal Obsessive Disorder
Options
toArray
?: transform to arrayjoin
?: the separator characterwithKeys
?: prefix with keyswithStack
?: whether to include stack π§
errorTo(new Error('xyzzy', cause: new SyntaxError('hoge')), {
toArray: true, // result in array output
join: '::', // whether to concat values with value as separator
withKeys: true, // whether to prefix keys to each value
})
Usage
# install
npm install error-to
// ESM
import errorTo from 'error-to'
// CJS
const errorTo = require('error-to')
const { log } = console
// βββββββββββββββββββββββ
// βββββββββββββββββββββββ
// ββββ SIMPLE ERRORS ββββ
const e1 = new Error('xyzzy')
// :: Plain Object
log(error-to(e1))
// { name: 'Error', message: 'xyzzy' }
// :: Plain Array
log(error-to(e1, { toArray: true }))
// ['Error', 'xyzzy']
// :: Joined Array
log(error-to(e1, { toArray: true, join: '::' }))
// ['Error::xyzzy']
// :: Plain Array with Keys
log(error-to(e1, { toArray: true, withKeys: true }))
// ['name:Error', 'message:xyzzy']
// :: Joined Array with Keys
log(error-to(e1, { toArray: true, withKeys: true, join: '__' }))
// ['name:Error__message:xyzzy']
// ββββββββββββββββββββ
// ββββββββββββββββββββ
// ββββ WITH CAUSE ββββ
const e2 = new SyntaxError('hoge', { cause: e1 })
// :: Plain Object with Cause
log(error-to(e2))
// { name: 'SyntaxError', message: 'hoge', cause: { name: 'Error', message: 'xyzzy' } }
// :: Plain Array with Cause
log(error-to(e2, { toArray: true }))
// ['SyntaxError', 'hoge', ['Error', 'xyzzy']]
// :: Joined Array with Cause
log(error-to(e2, { toArray: true, join: '::' }))
// ['SyntaxError::hoge', 'Error::xyzzy']
// :: Plain Array with Cause and Keys
log(error-to(e2, { toArray: true, join: withKeys: true }))
// ['name:SyntaxError', 'message:hoge', 'cause.name:Error', cause.message:'xyzzy']
// :: Joined Array with Cause and Keys
log(error-to(e2, { toArray: true, join: withKeys: true, join: '__' }))
// ['name:SyntaxError__message:hoge__cause.name:Error__cause.message:xyzzy']
// βββββββββββββββββββββββ
// βββββββββββββββββββββββ
// βββ AggregateError ββββ
const e3 = new AggregateError([e1, new Error('frob'), e2], 'nyoro')
// :: Plain Object on AggregateError
log(error-to(e3))
/*
{
name: 'AggregateError',
message: 'nyoro',
errors: [
{ name: 'Error', message: 'xyzzy' },
{ name: 'Error', message: 'frob' },
{ name: 'SyntaxError', message: 'nyoro' },
]
}
*/
// :: Plain Array on AggregateError
// TODO::WRITE:DOC::
// :: Joined Array with Cause on AggregateError
// TODO::WRITE:DOC::
// :: Plain Array with Cause and Keys on AggregateError
// TODO::WRITE:DOC::
// :: Joined Array with Cause and Keys on AggregateError
// TODO::WRITE:DOC::
// TODO::
// 'Error:no such file or directory:ENOENT:-2:nada'
// a1 = ['Error', 'no such file or directory', 'ENOENT', '-2', 'nada']
// a2 = ['name:Error', 'message:no such file or directory', 'code:ENOENT', 'errno:-2', 'path:nada']