{"id":21064018,"url":"https://github.com/luochen1990/node-define-error","last_synced_at":"2025-07-10T17:04:24.380Z","repository":{"id":71923108,"uuid":"118148045","full_name":"luochen1990/node-define-error","owner":"luochen1990","description":"Define pretty printed, nested custom errors easily","archived":false,"fork":false,"pushed_at":"2019-01-09T07:36:22.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-03T05:04:03.662Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"CoffeeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luochen1990.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-19T16:16:18.000Z","updated_at":"2019-01-09T07:36:23.000Z","dependencies_parsed_at":"2023-05-22T19:00:20.188Z","dependency_job_id":null,"html_url":"https://github.com/luochen1990/node-define-error","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"58d8e466ae592d20118eb5d0bf188356ba629ae1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/luochen1990/node-define-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fnode-define-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fnode-define-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fnode-define-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fnode-define-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luochen1990","download_url":"https://codeload.github.com/luochen1990/node-define-error/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fnode-define-error/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264613865,"owners_count":23637448,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T17:48:00.668Z","updated_at":"2025-07-10T17:04:24.336Z","avatar_url":"https://github.com/luochen1990.png","language":"CoffeeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Node Define Error\n=================\n\nDefine pretty printed, nested custom errors easily.\n\nFeatures\n--------\n\n- define custom error easily\n- define custom fields for your error\n- pretty printed error message\n- nested error support\n- brief stack trace for nested error\n\nInstall\n-------\n\n```\nnpm install node-define-error\n```\n\nSimple Usage\n------------\n\n```\ndefineError = require('node-define-error')\n\nMyError = defineError('MyError')\n\nthrow new MyError('a simple custom error')\n```\n\nAdvanced Usage\n--------------\n\nFile `rpc-error.js`:\n\n```javascript\ndefineError = require('node-define-error')\n\nRpcError = defineError('RpcError', ['from', 'api'])  //NOTE: define custom fields here as specification\n\nfetch = (url) =\u003e Promise.reject(new Error('failed to fetch \"' + url + '\"'))\n\nrpcCall = async () =\u003e {\n    try {\n        const response = await fetch('http://path/to/some/api')\n        const result = JSON.parse(response)\n        if (result.success !== true) {\n            throw new RpcError({from: 'server', 'api': 'path/to/some/api'})\n        }\n        return result\n    } catch (err) {\n        throw new RpcError({from: 'client', 'api': 'path/to/some/api'}, err)  //NOTE: pass on an error as a cause of this error, this is optional\n    }\n}\nrpcCall().catch((e) =\u003e {\n    console.log(e.stack)\n})\n\n```\n\nError stack:\n\n```text\nRpcError:\n  [from]: \"client\"\n  [api]: \"path/to/some/api\"\n\n  Caused By: Error: failed to fetch \"http://path/to/some/api\"\n      at fetch (/the/very/very/long/path/to/rpc-error.js:5:33)\n\n    at rpcCall (/the/very/very/long/path/to/rpc-error.js:16:15)\n    at \u003canonymous\u003e\n    at runMicrotasksCallback (internal/process/next_tick.js:121:5)\n    at _combinedTickCallback (internal/process/next_tick.js:131:7)\n    at process._tickCallback (internal/process/next_tick.js:180:9)\n    at Function.Module.runMain (module.js:678:11)\n    at startup (bootstrap_node.js:187:16)\n    at bootstrap_node.js:608:3\n```\n\nDeep Nested Example\n-------------------\n\nFile `nested-error.js`:\n\n```javascript\nconst defineError = require('node-define-error')\n\nconst MyAdvError = defineError('MyAdvError', ['f', 'msg'])\n\nfunction f0() {\n    throw new Error('the inner custom error')\n}\n\nfunction f1() {\n    try {\n        return f0()\n    } catch (e) {\n        throw new MyAdvError({ f: 'f1', msg: 'the medium custom error' }, e)\n    }\n}\n\nfunction f2() {\n    try {\n        return f1()\n    } catch (e) {\n        throw new MyAdvError({ f: 'f2', msg: 'the outer custom error' }, e)\n    }\n}\n\nf2()\n```\n\nError stack:\n\n```text\nMyAdvError:\n  [f]: \"f2\"\n  [msg]: \"the outer custom error\"\n\n  Caused By: MyAdvError:\n    [f]: \"f1\"\n    [msg]: \"the medium custom error\"\n\n    Caused By: Error: the inner custom error\n        at f0 (/the/very/very/long/path/to/nested-error.js:6:11)\n\n      at f1 (/the/very/very/long/path/to/nested-error.js:13:15)\n\n    at f2 (/the/very/very/long/path/to/nested-error.js:21:15)\n    at Object.\u003canonymous\u003e (/the/very/very/long/path/to/nested-error.js:25:1)\n    at Module._compile (module.js:635:30)\n    at Object.Module._extensions..js (module.js:646:10)\n    at Module.load (module.js:554:32)\n    at tryModuleLoad (module.js:497:12)\n    at Function.Module._load (module.js:489:3)\n    at Function.Module.runMain (module.js:676:10)\n    at startup (bootstrap_node.js:187:16)\n    at bootstrap_node.js:608:3\n```\n\nReference\n---------\n\n- [Tero's answer @stackoverflow](http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript/5251506#5251506)\n- [Onur Yıldırım's answer @stackoverflow](https://stackoverflow.com/a/35881508/1608276)\n- [customizing-stack-traces @v8-wiki](https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluochen1990%2Fnode-define-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluochen1990%2Fnode-define-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluochen1990%2Fnode-define-error/lists"}