{"id":13483504,"url":"https://github.com/necojackarc/extensible-custom-error","last_synced_at":"2025-10-25T12:18:44.736Z","repository":{"id":33106450,"uuid":"151960058","full_name":"necojackarc/extensible-custom-error","owner":"necojackarc","description":"JavaScript extensible custom error that can take a message and/or an Error object","archived":false,"fork":false,"pushed_at":"2024-11-18T23:05:09.000Z","size":710,"stargazers_count":95,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-13T02:16:34.965Z","etag":null,"topics":["ecmascript","error","error-handling","javascript","node"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/extensible-custom-error","language":"JavaScript","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/necojackarc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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-10-07T16:16:12.000Z","updated_at":"2025-04-04T14:50:29.000Z","dependencies_parsed_at":"2023-01-14T23:30:48.811Z","dependency_job_id":"077b3398-160e-4c66-8b6e-9dc3e9c4dabc","html_url":"https://github.com/necojackarc/extensible-custom-error","commit_stats":{"total_commits":59,"total_committers":3,"mean_commits":"19.666666666666668","dds":0.3728813559322034,"last_synced_commit":"f1484e45fbac4efc6714bd0b146979ddac0b5d5e"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necojackarc%2Fextensible-custom-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necojackarc%2Fextensible-custom-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necojackarc%2Fextensible-custom-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/necojackarc%2Fextensible-custom-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/necojackarc","download_url":"https://codeload.github.com/necojackarc/extensible-custom-error/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248654104,"owners_count":21140237,"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":["ecmascript","error","error-handling","javascript","node"],"created_at":"2024-07-31T17:01:12.094Z","updated_at":"2025-10-25T12:18:44.731Z","avatar_url":"https://github.com/necojackarc.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Extensible Custom Error\n\n![CI](https://github.com/necojackarc/extensible-custom-error/workflows/CI/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/necojackarc/extensible-custom-error/badge.svg?branch=master)](https://coveralls.io/github/necojackarc/extensible-custom-error?branch=master)\n[![npm version](https://badge.fury.io/js/extensible-custom-error.svg)](https://badge.fury.io/js/extensible-custom-error)\n\nJavaScript extensible custom error that can take a message and/or an Error object\n\n```js\nclass MyError extends ExtensibleCustomError {}\n\nnew MyError('message'); // Take a message\nnew MyError(error); // Take an error\nnew MyError('message', error); // Take a message and an error\n```\n\n## Contents\n\n- [Extensible Custom Error](#extensible-custom-error)\n  - [Contents](#contents)\n  - [Features](#features)\n    - [Define custom errors easily](#define-custom-errors-easily)\n    - [Wrap errors without losing any data](#wrap-errors-without-losing-any-data)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [Define custom errors](#define-custom-errors)\n    - [Instantiate custom errors](#instantiate-custom-errors)\n  - [Examples](#examples)\n    - [Wrap an error](#wrap-an-error)\n    - [Wrap an error while passing a new message](#wrap-an-error-while-passing-a-new-message)\n  - [Special Thanks](#special-thanks)\n  - [License](#license)\n\n## Features\n\nThere are some pains around JavaScript error handling.\nTwo of them are:\n\n1. Define custom errors easily\n2. Wrap errors without losing any data\n\nThis `ExtensibleCustomError` class enables you to do both - you can define your custom errors easily and wrap errors with them while merging stack traces prettily.\n\n### Define custom errors easily\n\nTo define custom errors in Vanilla JS, you need to set names and stack traces manually, but you no longer need to do that with `ExtensibleCustomError`.\n\n```js\nclass MyError extends ExtensibleCustomError {}\n```\n\n### Wrap errors without losing any data\n\nBuilt-in errors only take a message, so they can't wrap any errors, which means stack traces so far will be lost.\nHowever, `ExtensibleCustomError` can take a message and/or an Error object while merging stack traces.\n\n```js\ncatch (error) {\n  throw new MyError(error);\n}\n```\n\n```js\ncatch (error) {\n  throw new MyError('message', error);\n}\n```\n\n## Installation\n\nUsing npm:\n\n```bash\n$ npm install extensible-custom-error\n```\n\n## Usage\n\n### Define custom errors\n\n```js\n// CommonJS\nconst ExtensibleCustomError = require('extensible-custom-error');\n// ESM\n// import ExtensibleCustomError from 'extensible-custom-error';\n\n// That's it!\nclass MyError extends ExtensibleCustomError {}\n```\n\n```js\n// Should you need to set custom properties\nclass MyErrorWithCustomProperty extends ExtensibleCustomError {\n  constructor(...args) {\n    // Ensure calling the super constructor\n    super(...args);\n\n    Object.defineProperty(this, 'customProperty', {\n      configurable: true,\n      enumerable : false,\n      value : 'I am the Bone of my Sword',\n      writable : true,\n    });\n  }\n}\n```\n\nN.B. With an uglifier, class names might get obsecure. See [this issue comment](https://github.com/bjyoungblood/es6-error/issues/31#issuecomment-301128220).\n\n### Instantiate custom errors\n\nYou can instantiate your custom errors in the same way as built-in errors.\n\n```js\n// Throw it as usual!\nthrow new MyError('Steel is my Body and Fire is my Blood');\n```\n\n```js\ntry {\n  // Do something that may cause errors\n} catch (error) {\n  // Pass an error instance, then stack traces will be merged\n  throw new MyError(error);\n}\n```\n\n```js\ntry {\n  // Do something that may cause errors\n} catch (error) {\n  // Pass a message and an error instance, then stack traces will be merged\n  throw new MyError('I have created over a Thousand Blades', error);\n}\n```\n\n## Examples\n\n### Wrap an error\n\nIf you run:\n\n```js\nconst ExtensibleCustomError = require('extensible-custom-error');\n\nclass MyError extends ExtensibleCustomError {}\n\nfunction throwBuiltinError() {\n  throw new Error('Unknown to Death, Nor known to Life');\n}\n\nfunction wrapErrorWithMyError() {\n  try {\n    throwBuiltinError();\n  } catch (error) {\n    throw new MyError(error);\n  }\n}\n\nfunction main() {\n  try {\n    wrapErrorWithMyError();\n  } catch (error) {\n    console.log(error);\n  }\n}\n\nmain();\n```\n\nyou'll get:\n\n```bash\nMyError: Error: Unknown to Death, Nor known to Life\n    at wrapErrorWithMyError (/home/necojackarc/custom_error.js:101:11)\nError: Unknown to Death, Nor known to Life\n    at throwBuiltinError (/home/necojackarc/custom_error.js:94:9)\n    at wrapErrorWithMyError (/home/necojackarc/custom_error.js:99:5)\n    at main (/home/necojackarc/custom_error.js:107:5)\n    at Object.\u003canonymous\u003e (/home/necojackarc/custom_error.js:113:1)\n    at Module._compile (module.js:652:30)\n    at Object.Module._extensions..js (module.js:663:10)\n    at Module.load (module.js:565:32)\n    at tryModuleLoad (module.js:505:12)\n    at Function.Module._load (module.js:497:3)\n    at Function.Module.runMain (module.js:693:10)\n```\n\n### Wrap an error while passing a new message\n\nIf you run:\n\n```js\nconst ExtensibleCustomError = require('extensible-custom-error');\n\nclass MyError extends ExtensibleCustomError {}\n\nfunction throwBuiltinError() {\n  throw new Error('Have withstood Pain to create many Weapons');\n}\n\nfunction wrapErrorWithMyError() {\n  try {\n    throwBuiltinError();\n  } catch (error) {\n    throw new MyError('Unlimited Blade Works', error);\n  }\n}\n\nfunction main() {\n  try {\n    wrapErrorWithMyError();\n  } catch (error) {\n    console.log(error);\n  }\n}\n\nmain();\n```\n\nyou'll get:\n\n```bash\nMyError: Unlimited Blade Works\n    at wrapErrorWithMyError (/home/necojackarc/custom_error.js:101:11)\nError: Have withstood Pain to create many Weapons\n    at throwBuiltinError (/home/necojackarc/custom_error.js:94:9)\n    at wrapErrorWithMyError (/home/necojackarc/custom_error.js:99:5)\n    at main (/home/necojackarc/custom_error.js:107:5)\n    at Object.\u003canonymous\u003e (/home/necojackarc/custom_error.js:113:1)\n    at Module._compile (module.js:652:30)\n    at Object.Module._extensions..js (module.js:663:10)\n    at Module.load (module.js:565:32)\n    at tryModuleLoad (module.js:505:12)\n    at Function.Module._load (module.js:497:3)\n    at Function.Module.runMain (module.js:693:10)\n```\n\n## Special Thanks\n\n* [@yszk0123](https://github.com/yszk0123) as a reviewer\n* [bjyoungblood/es6-error](https://github.com/bjyoungblood/es6-error) as a reference\n\n## License\n\nThe module is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnecojackarc%2Fextensible-custom-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnecojackarc%2Fextensible-custom-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnecojackarc%2Fextensible-custom-error/lists"}