1.0.0 • Published 4 years ago

nv-centralize-error v1.0.0

Weekly downloads
5
License
ISC
Repository
-
Last release
4 years ago

nv-centralize-error

  • nv-centralize-error is a simple util for error-manage
  • it use a EventTarget to control/handle all error
  • it is suitable for unimportant errors
  • your runtime need to support EventTarget

install

  • npm install nv-centralize-error

usage

const nv_cerr = require("nv--centralize-error")
const ERROR_POOL = new nv_cerr.ErrorPool()

var ERR = new Error("some-stupid-error-from-third-party")

var handler = (err)=>{
    console.log(err.data)
}
ERROR_POOL.regis_err(ERR,handler)


> ERROR_POOL.ls()
Map(1) {
  '6ab94b6ea413e2f9d9d02e51c31c50531b930cee' => {
    handler: [Function: handler],
    json: {
      code: undefined,
      name: 'Error',
      message: 'some-stupid-error-from-third-party'
    }
  }
}
>


function tst() {
    setTimeout(
        ()=>{
            ERROR_POOL.dispatch_err(ERR)
        },
        5000
    )
}

tst()
/*
{
  code: undefined,
  name: 'Error',
  message: 'some-stupid-error-from-third-party',
  id: '6ab94b6ea413e2f9d9d02e51c31c50531b930cee',
  stack: 'Error: some-stupid-error-from-third-party\n' +
    '    at REPL4:1:11\n' +
    '    at Script.runInThisContext (node:vm:133:18)\n' +
    '    at _run_in_context (node:repl:288:23)\n' +
    '    at run_in_context (node:repl:300:22)\n' +
    '    at run_in_context_and_handle_top_await (node:repl:224:34)\n' +
    '    at _defaultEval (node:repl:429:5)\n' +
    '    at REPLServer.defaultEval (node:repl:1899:9)\n' +
    '    at bound (node:domain:416:15)\n' +
    '    at REPLServer.runBound [as eval] (node:domain:427:12)\n' +
    '    at _onLine (node:repl:1034:10)',
  timestamp: 2021-02-06T06:00:56.146Z,
  hrtime: '747050161178641'
}
*/


> nv_cerr.brief_jsonize(ERR)
{
  code: undefined,
  name: 'Error',
  message: 'some-stupid-error-from-third-party'
}
>
> nv_cerr.full_jsonize(ERR)
{
  code: undefined,
  name: 'Error',
  message: 'some-stupid-error-from-third-party',
  id: '6ab94b6ea413e2f9d9d02e51c31c50531b930cee',
  stack: 'Error: some-stupid-error-from-third-party\n' +
    '    at REPL4:1:11\n' +
    '    at Script.runInThisContext (node:vm:133:18)\n' +
    '    at _run_in_context (node:repl:288:23)\n' +
    '    at run_in_context (node:repl:300:22)\n' +
    '    at run_in_context_and_handle_top_await (node:repl:224:34)\n' +
    '    at _defaultEval (node:repl:429:5)\n' +
    '    at REPLServer.defaultEval (node:repl:1899:9)\n' +
    '    at bound (node:domain:416:15)\n' +
    '    at REPLServer.runBound [as eval] (node:domain:427:12)\n' +
    '    at _onLine (node:repl:1034:10)',
  timestamp: 2021-02-06T06:01:51.797Z,
  hrtime: '747105812282066'
}
>
> nv_cerr.errorize(nv_cerr.full_jsonize(ERR))
Error: some-stupid-error-from-third-party
  code: undefined
}
>
> nv_cerr.creat_signature(ERR)
'6ab94b6ea413e2f9d9d02e51c31c50531b930cee'
>

METHODS

ERROR_POOL.dispatch_err
ERROR_POOL.ls                  //list registered err handler
ERROR_POOL.regis_err
ERROR_POOL.rm_err

APIS

  • creat_signature(err)
  • brief_jsonize(err)
  • full_jsonize(err)
  • errorize(json)
  • creat_signature(err)

REQUIREMENT

  • your runtime need to support EventTarget

LICENSE

  • ISC
1.0.0

4 years ago