1.0.1 • Published 1 year ago

nv-error-jserde v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

nv-error-jserde

  • error AND json
  • for compatible with nvlang err-db(which is just a insert-only database to record all errors)

install

  • npm install nv-error-jserde

usage

const {from_json,to_json} = require("nv-error-jserde")

example

            var err =  er.creat_er("one-err","unknown");
            var erd =  er.to_json(err)
            /*
            {
              name: 'Error',
              message: 'one-err',
              stack: [
                'Error: one-err',
                '    at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)',
                '    at REPL3:1:15',
                '    at Script.runInThisContext (node:vm:129:12)',
                '    at REPLServer.defaultEval (node:repl:562:29)',
                '    at bound (node:domain:421:15)',
                '    at REPLServer.runBound [as eval] (node:domain:432:12)',
                '    at REPLServer.onLine (node:repl:889:10)',
                '    at REPLServer.emit (node:events:402:35)',
                '    at REPLServer.emit (node:domain:475:12)',
                '    at REPLServer.Interface._onLine (node:readline:487:10)'
              ],
              cause: 'unknown'
            }
            */
            var duped_err = er.from_json(erd)
            /*
            > duped_err
            Error: one-err
                at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)
                at REPL11:1:15
                at Script.runInThisContext (node:vm:129:12)
                at REPLServer.defaultEval (node:repl:562:29)
                at bound (node:domain:421:15)
                at REPLServer.runBound [as eval] (node:domain:432:12)
                at REPLServer.onLine (node:repl:889:10)
                at REPLServer.emit (node:events:402:35)
                at REPLServer.emit (node:domain:475:12)
                at REPLServer.Interface._onLine (node:readline:487:10) {
              cause: 'unknown'
            }
            */



            var cerr =  er.creat_er("child-err",err);
            var cerr2 =  er.creat_er("child2-err",cerr);

            er.get_root_cause(cerr2)
            /*
            Error: one-err
                at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)
                at REPL69:1:15
                at Script.runInThisContext (node:vm:129:12)
                at REPLServer.defaultEval (node:repl:562:29)
                at bound (node:domain:421:15)
                at REPLServer.runBound [as eval] (node:domain:432:12)
                at REPLServer.onLine (node:repl:889:10)
                at REPLServer.emit (node:events:402:35)
                at REPLServer.emit (node:domain:475:12)
                at REPLServer.Interface._onLine (node:readline:487:10)
            */

            var cerr2d = er.to_json(cerr2)
            /*
            {
              name: 'Error',
              message: 'child2-err',
              stack: [
                'Error: child2-err',
                '    at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)',
                '    at REPL8:1:17',
                '    at Script.runInThisContext (node:vm:129:12)',
                '    at REPLServer.defaultEval (node:repl:562:29)',
                '    at bound (node:domain:421:15)',
                '    at REPLServer.runBound [as eval] (node:domain:432:12)',
                '    at REPLServer.onLine (node:repl:889:10)',
                '    at REPLServer.emit (node:events:402:35)',
                '    at REPLServer.emit (node:domain:475:12)',
                '    at REPLServer.Interface._onLine (node:readline:487:10)'
              ],
              cause: {
                name: 'Error',
                message: 'child-err',
                stack: [
                  'Error: child-err',
                  '    at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)',
                  '    at REPL7:1:16',
                  '    at Script.runInThisContext (node:vm:129:12)',
                  '    at REPLServer.defaultEval (node:repl:562:29)',
                  '    at bound (node:domain:421:15)',
                  '    at REPLServer.runBound [as eval] (node:domain:432:12)',
                  '    at REPLServer.onLine (node:repl:889:10)',
                  '    at REPLServer.emit (node:events:402:35)',
                  '    at REPLServer.emit (node:domain:475:12)',
                  '    at REPLServer.Interface._onLine (node:readline:487:10)'
                ],
                cause: {
                  name: 'Error',
                  message: 'one-err',
                  stack: [Array],
                  cause: 'unknown'
                }
              }
            }
            */

            var duped_cerr2 = er.from_json(cerr2d)
            /*
            Error: child2-err
                at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)
                at REPL8:1:17
                at Script.runInThisContext (node:vm:129:12)
                at REPLServer.defaultEval (node:repl:562:29)
                at bound (node:domain:421:15)
                at REPLServer.runBound [as eval] (node:domain:432:12)
                at REPLServer.onLine (node:repl:889:10)
                at REPLServer.emit (node:events:402:35)
                at REPLServer.emit (node:domain:475:12)
                at REPLServer.Interface._onLine (node:readline:487:10) {
              cause: Error: child-err
                  at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)
                  at REPL7:1:16
                  at Script.runInThisContext (node:vm:129:12)
                  at REPLServer.defaultEval (node:repl:562:29)
                  at bound (node:domain:421:15)
                  at REPLServer.runBound [as eval] (node:domain:432:12)
                  at REPLServer.onLine (node:repl:889:10)
                  at REPLServer.emit (node:events:402:35)
                  at REPLServer.emit (node:domain:475:12)
                  at REPLServer.Interface._onLine (node:readline:487:10) {
                cause: Error: one-err
                    at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:43)
                    at REPL3:1:15
                    at Script.runInThisContext (node:vm:129:12)
                    at REPLServer.defaultEval (node:repl:562:29)
                    at bound (node:domain:421:15)
                    at REPLServer.runBound [as eval] (node:domain:432:12)
                    at REPLServer.onLine (node:repl:889:10)
                    at REPLServer.emit (node:events:402:35)
                    at REPLServer.emit (node:domain:475:12)
                    at REPLServer.Interface._onLine (node:readline:487:10) {
                  cause: 'unknown'
                }
              }
            }
            */

agg err

            var root_err = er.creat_er("root")
            var agg_err = er.creat_agger(["e0","e1","e2"],"agg",root_err)

            /*
            > agg_err
            AggregateError: agg
                at _creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:38:52)
                at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:52)
                at REPL12:1:18
                at Script.runInThisContext (node:vm:129:12)
                at REPLServer.defaultEval (node:repl:562:29)
                at bound (node:domain:421:15)
                at REPLServer.runBound [as eval] (node:domain:432:12)
                at REPLServer.onLine (node:repl:889:10)
                at REPLServer.emit (node:events:402:35)
                at REPLServer.emit (node:domain:475:12)
            > agg_err.errors
            [
              Error: e0
                  at creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
                  at /mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:79
                  at Array.map (<anonymous>)
                  at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:70)
                  at REPL12:1:18
                  at Script.runInThisContext (node:vm:129:12)
                  at REPLServer.defaultEval (node:repl:562:29)
                  at bound (node:domain:421:15)
                  at REPLServer.runBound [as eval] (node:domain:432:12)
                  at REPLServer.onLine (node:repl:889:10),
              Error: e1
                  at creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
                  at /mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:79
                  at Array.map (<anonymous>)
                  at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:70)
                  at REPL12:1:18
                  at Script.runInThisContext (node:vm:129:12)
                  at REPLServer.defaultEval (node:repl:562:29)
                  at bound (node:domain:421:15)
                  at REPLServer.runBound [as eval] (node:domain:432:12)
                  at REPLServer.onLine (node:repl:889:10),
              Error: e2
                  at creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
                  at /mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:79
                  at Array.map (<anonymous>)
                  at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:70)
                  at REPL12:1:18
                  at Script.runInThisContext (node:vm:129:12)
                  at REPLServer.defaultEval (node:repl:562:29)
                  at bound (node:domain:421:15)
                  at REPLServer.runBound [as eval] (node:domain:432:12)
                  at REPLServer.onLine (node:repl:889:10)
            ]
            > agg_err.message
            'agg'
            > agg_err.stack
            'AggregateError: agg\n' +
              '    at _creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:38:52)\n' +
              '    at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:52)\n' +
              '    at REPL12:1:18\n' +
              '    at Script.runInThisContext (node:vm:129:12)\n' +
              '    at REPLServer.defaultEval (node:repl:562:29)\n' +
              '    at bound (node:domain:421:15)\n' +
              '    at REPLServer.runBound [as eval] (node:domain:432:12)\n' +
              '    at REPLServer.onLine (node:repl:889:10)\n' +
              '    at REPLServer.emit (node:events:402:35)\n' +
              '    at REPLServer.emit (node:domain:475:12)'
            > agg_err.cause
            Error: root
                at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
                at REPL3:1:19
                at Script.runInThisContext (node:vm:129:12)
                at REPLServer.defaultEval (node:repl:562:29)
                at bound (node:domain:421:15)
                at REPLServer.runBound [as eval] (node:domain:432:12)
                at REPLServer.onLine (node:repl:889:10)
                at REPLServer.emit (node:events:402:35)
                at REPLServer.emit (node:domain:475:12)
                at REPLServer.Interface._onLine (node:readline:487:10)
            >
            */

            var erd = er.to_json(agg_err)
            /*
            >
            {
              name: 'AggregateError',
              message: 'agg',
              stack: [
                'AggregateError: agg',
                '    at _creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:38:52)',
                '    at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:52)',
                '    at REPL12:1:18',
                '    at Script.runInThisContext (node:vm:129:12)',
                '    at REPLServer.defaultEval (node:repl:562:29)',
                '    at bound (node:domain:421:15)',
                '    at REPLServer.runBound [as eval] (node:domain:432:12)',
                '    at REPLServer.onLine (node:repl:889:10)',
                '    at REPLServer.emit (node:events:402:35)',
                '    at REPLServer.emit (node:domain:475:12)'
              ],
              cause: {
                name: 'Error',
                message: 'root',
                stack: [
                  'Error: root',
                  '    at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)',
                  '    at REPL3:1:19',
                  '    at Script.runInThisContext (node:vm:129:12)',
                  '    at REPLServer.defaultEval (node:repl:562:29)',
                  '    at bound (node:domain:421:15)',
                  '    at REPLServer.runBound [as eval] (node:domain:432:12)',
                  '    at REPLServer.onLine (node:repl:889:10)',
                  '    at REPLServer.emit (node:events:402:35)',
                  '    at REPLServer.emit (node:domain:475:12)',
                  '    at REPLServer.Interface._onLine (node:readline:487:10)'
                ],
                cause: null
              },
              errors: [
                { name: 'Error', message: 'e0', stack: [Array], cause: null },
                { name: 'Error', message: 'e1', stack: [Array], cause: null },
                { name: 'Error', message: 'e2', stack: [Array], cause: null }
              ]
            }
            >
            */

            var duped = er.from_json(erd)

    /*
    > duped
    AggregateError: agg
        at _creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:38:52)
        at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:52)
        at REPL4:1:18
        at Script.runInThisContext (node:vm:129:12)
        at REPLServer.defaultEval (node:repl:562:29)
        at bound (node:domain:421:15)
        at REPLServer.runBound [as eval] (node:domain:432:12)
        at REPLServer.onLine (node:repl:889:10)
        at REPLServer.emit (node:events:402:35)
        at REPLServer.emit (node:domain:475:12) {
      cause: Error: root
          at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
          at REPL3:1:19
          at Script.runInThisContext (node:vm:129:12)
          at REPLServer.defaultEval (node:repl:562:29)
          at bound (node:domain:421:15)
          at REPLServer.runBound [as eval] (node:domain:432:12)
          at REPLServer.onLine (node:repl:889:10)
          at REPLServer.emit (node:events:402:35)
          at REPLServer.emit (node:domain:475:12)
          at REPLServer.Interface._onLine (node:readline:487:10) {
        cause: null
      }
    }
    > duped.message
    'agg'
    > duped.stack
    'AggregateError: agg\n' +
      '    at _creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:38:52)\n' +
      '    at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:52)\n' +
      '    at REPL4:1:18\n' +
      '    at Script.runInThisContext (node:vm:129:12)\n' +
      '    at REPLServer.defaultEval (node:repl:562:29)\n' +
      '    at bound (node:domain:421:15)\n' +
      '    at REPLServer.runBound [as eval] (node:domain:432:12)\n' +
      '    at REPLServer.onLine (node:repl:889:10)\n' +
      '    at REPLServer.emit (node:events:402:35)\n' +
      '    at REPLServer.emit (node:domain:475:12)'
    >
    > duped.name
    'AggregateError'
    > duped.cause
    Error: root
        at Object.creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
        at REPL3:1:19
        at Script.runInThisContext (node:vm:129:12)
        at REPLServer.defaultEval (node:repl:562:29)
        at bound (node:domain:421:15)
        at REPLServer.runBound [as eval] (node:domain:432:12)
        at REPLServer.onLine (node:repl:889:10)
        at REPLServer.emit (node:events:402:35)
        at REPLServer.emit (node:domain:475:12)
        at REPLServer.Interface._onLine (node:readline:487:10) {
      cause: null
    }
    > duped.errors
    [
      Error: e0
          at creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
          at /mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:79
          at Array.map (<anonymous>)
          at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:70)
          at REPL4:1:18
          at Script.runInThisContext (node:vm:129:12)
          at REPLServer.defaultEval (node:repl:562:29)
          at bound (node:domain:421:15)
          at REPLServer.runBound [as eval] (node:domain:432:12)
          at REPLServer.onLine (node:repl:889:10) {
        cause: null
      },
      Error: e1
          at creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
          at /mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:79
          at Array.map (<anonymous>)
          at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:70)
          at REPL4:1:18
          at Script.runInThisContext (node:vm:129:12)
          at REPLServer.defaultEval (node:repl:562:29)
          at bound (node:domain:421:15)
          at REPLServer.runBound [as eval] (node:domain:432:12)
          at REPLServer.onLine (node:repl:889:10) {
        cause: null
      },
      Error: e2
          at creat_er (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:31:52)
          at /mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:79
          at Array.map (<anonymous>)
          at Object.creat_agger (/mnt/sda3/NV6-/nverr/nv-error-jserde/cmmn.js:39:70)
          at REPL4:1:18
          at Script.runInThisContext (node:vm:129:12)
          at REPLServer.defaultEval (node:repl:562:29)
          at bound (node:domain:421:15)
          at REPLServer.runBound [as eval] (node:domain:432:12)
          at REPLServer.onLine (node:repl:889:10) {
        cause: null
      }
    ]
    >
    */

APIS

funcs

            //just to avoid using  <new>,  <new> is boring for automation 

            const creat_er     = (msg:Str, cause:NU|Error)            => new _ER(msg,{cause});
            const creat_evler  = (msg:Str, cause:NU|Error)            => new _EVLER(msg,{cause});
            const creat_refer  = (msg:Str, cause:NU|Error)            => new _REFER(msg,{cause});
            const creat_rnger  = (msg:Str, cause:NU|Error)            => new _RNGER(msg,{cause});
            const creat_stxer  = (msg:Str, cause:NU|Error)            => new _STXER(msg,{cause});
            const creat_typer  = (msg:Str, cause:NU|Error)            => new _TYPER(msg,{cause});
            const creat_urier  = (msg:Str, cause:NU|Error)            => new _URIER(msg,{cause}); 
            const _creat_agger = (errs:Array<Error>,msg,cause)        => new _AGGER(errs,msg,{cause});
            const creat_agger  = (msgs:Array<Str>,agg_msg,cause)      => _creat_agger(msgs.map(msg=>creat_er(msg)),agg_msg,cause);

            
            //just to avoid using <instanceof>

            const is_er        = (o) => _istof(o,_ER);
            const is_evler     = (o) => _istof(o,_EVLER);
            const is_refer     = (o) => _istof(o,_REFER);
            const is_rnger     = (o) => _istof(o,_RNGER);
            const is_stxer     = (o) => _istof(o,_STXER);
            const is_typer     = (o) => _istof(o,_TYPER);
            const is_urier     = (o) => _istof(o,_URIER);
            const is_agger     = (o) => _istof(o,_AGGER);
            
            //just avoid using throw directly in biz code
            
            const thrw_msg    = (msg) => {throw(creat_er(msg))};
            const thrw_evlmsg = (msg) => {throw(creat_evler(msg))};
            const thrw_refmsg = (msg) => {throw(creat_refer(msg))};
            const thrw_rngmsg = (msg) => {throw(creat_rnger(msg))};
            const thrw_stxmsg = (msg) => {throw(creat_stxer(msg))};
            const thrw_typmsg = (msg) => {throw(creat_typer(msg))};
            const thrw_urimsg = (msg) => {throw(creat_urier(msg))};
            const thrw_aggmsg = (msgs)=> {throw(creat_agger(msgs))};
            
            
            // .cause.cause....
            const _get_root_cause_without_check_circular = (err)=> Error
            const get_root_cause                         = (err)=> Error
            
            const to_json_ignore_cause_and_children      = (err)=> Dict
            const to_json_without_check_circular         = (err)=> Dict

            const to_json                                = (err)=> Dict


            // 
            ErrorDict : {
               name                 : String
               message              : String
               stack                : String
               //
               cause?               : Error | String 
               errors?              : Array<Error|String>
            }

            const is_err_dict    = (d)=> ERD_MANDATORY_PROPS.every(k=>d.hasOwnProperty(k));
            const is_agger_dict  = (d)=>is_err_dict(d) && d.hasOwnProperty("errors");
            
            const _from_json_ignore_nest_without_check    = (d: T'is_err_dict(self)) => Error
            const _from_json_ignore_nest                  = (d: T'is_err_dict(self)) => Error
            const _from_json_without_check                = (d: T'is_err_dict(self)) => Error
            const from_json                               = (d: T'is_err_dict(self)) => Error

alias AND consts

      cnst: {
        _ER: [Function: Error] { stackTraceLimit: 10 },
        _AGGER: [Function: AggregateError],
        _EVLER: [Function: EvalError],
        _REFER: [Function: ReferenceError],
        _RNGER: [Function: RangeError],
        _STXER: [Function: SyntaxError],
        _TYPER: [Function: TypeError],
        _URIER: [Function: URIError],
        NAME_ARY: [
          'Error',
          'AggregateError',
          'EvalError',
          'ReferenceError',
          'RangeError',
          'SyntaxError',
          'TypeError',
          'URIError'
        ],
        NM2CLS: {
          Error: [Function],
          AggregateError: [Function: AggregateError],
          EvalError: [Function: EvalError],
          ReferenceError: [Function: ReferenceError],
          RangeError: [Function: RangeError],
          SyntaxError: [Function: SyntaxError],
          TypeError: [Function: TypeError],
          URIError: [Function: URIError]
        },
        cls2nm: [Function: cls2nm],
        ERD_MANDATORY_PROPS: [ 'name', 'message', 'stack' ],
        ERD_OPTIONAL_PROPS: [ 'cause' ],
        AGGER_ERD_MANDATORY_PROPS: [ 'errors' ],
        ERRORD: {
          circular: 'circular',
          must_be_dict: 'must_be_dict',
          must_be_erd: 'must_be_erd_with_name_message_stack'
        },
        PROPSD: {
          name: 'name',
          message: 'message',
          stack: 'stack',
          cause: 'cause',
          errors: 'errors'
        }
      },

LICENSE

  • ISC