{"id":22246189,"url":"https://github.com/jpbaking/error-extender","last_synced_at":"2026-06-15T03:02:40.939Z","repository":{"id":52158006,"uuid":"139248918","full_name":"jpbaking/error-extender","owner":"jpbaking","description":"error-extender","archived":false,"fork":false,"pushed_at":"2023-10-31T12:22:30.000Z","size":117,"stargazers_count":0,"open_issues_count":3,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-06T15:20:31.573Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/jpbaking.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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-06-30T12:47:27.000Z","updated_at":"2019-12-24T12:16:00.000Z","dependencies_parsed_at":"2024-12-03T05:28:34.726Z","dependency_job_id":"ae6b3318-1825-4846-bc7f-5cafe97618df","html_url":"https://github.com/jpbaking/error-extender","commit_stats":{"total_commits":30,"total_committers":3,"mean_commits":10.0,"dds":0.06666666666666665,"last_synced_commit":"2c32b09e7a5c4902b9116eb31a48d8662ad29651"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Ferror-extender","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Ferror-extender/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Ferror-extender/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpbaking%2Ferror-extender/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpbaking","download_url":"https://codeload.github.com/jpbaking/error-extender/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245450783,"owners_count":20617417,"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-12-03T05:26:30.320Z","updated_at":"2026-06-15T03:02:35.920Z","avatar_url":"https://github.com/jpbaking.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# error-extender-1.0.2\n\nSimplifies creation of custom `Error` classes for Node.js!\n\n...which then produces `stack` with appended stacks of supplied `cause` _(very much like in Java)_!\n\n```javascript\nconst extendError = require('error-extender');\n\nconst CustomError = extendError('CustomError');\n\nconst rootCause = new Error('the root cause');\n\nconsole.log(new CustomError({ message: 'An error has occurred.', cause: rootCause }));\n```\n\nShall output:\n\n```\nCustomError: An error has occurred.\n    at Object.\u003canonymous\u003e (/opt/app/index.js:7:13)\n    at Module._compile (internal/modules/cjs/loader.js:702:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)\n    at Module.load (internal/modules/cjs/loader.js:612:32)\n    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)\n    at Function.Module._load (internal/modules/cjs/loader.js:543:3)\n    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)\n    at startup (internal/bootstrap/node.js:240:19)\n    at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)\nCaused by: Error: the root cause\n    at Object.\u003canonymous\u003e (/opt/app/index.js:5:19)\n    at Module._compile (internal/modules/cjs/loader.js:702:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)\n    at Module.load (internal/modules/cjs/loader.js:612:32)\n    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)\n    at Function.Module._load (internal/modules/cjs/loader.js:543:3)\n    at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)\n    at startup (internal/bootstrap/node.js:240:19)\n    at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)\n```\n\n### 100% Code Coverage\nOh, by the way, 100% test coverage. See for yourself (via `npm test`)!\n\n## Features\n\n### \"Extending\" Errors\n\nIt's quite simple! See below:\n\n```javascript\nconst extendError = require('error-extender');\n\nconst AppError = extendError('AppError'); // extends `Error` (default)\n```\n\nOr... A bit more complex using the second argument _(options)_:\n\n```javascript\nconst extendError = require('error-extender');\n\nconst AppError = extendError('AppError', {\n  defaultMessage: 'An unhandled error has occurred.',\n  defaultData: { status: 503, message: 'An unhandled error has occurred.' }\n});\n\nconst ServiceError = extendError('ServiceError', {\n  parent: AppError, // extends `AppError`\n  defaultMessage: 'A service error has occurred.',\n  defaultData: { status: 500, message: 'A service error has occurred.' }\n});\n\nconst DatabaseError = extendError('DatabaseError', {\n  parent: ServiceError, // extends `ServiceError`\n  defaultMessage: 'A service database error has occurred.',\n  defaultData: { message: 'A service database error has occurred.' }\n});\n\nrequire('assert').deepStrictEqual(\n  DatabaseError.defaultData, {\n    status: 500,\n    message: 'A service database error has occurred.'\n  });\n// no error\n```\n\nYes, `defaultData` merges!\n\n#### `error-extender` Arguments\n\n`error-extender` accepts a single [object literal](https://www.w3schools.com/js/js_objects.asp) as second argument.\n\nThe options (object literal keys) are as follows:\n\n| key              | expected type                              |\n| ---------------- | ------------------------------------------ |\n| `parent`         | `Error.prototype` _or one that extends it_ |\n| `defaultMessage` | `string`                                   |\n| `defaultData`    | `any`                                      |\n\n### \"Extended Errors\"\n\n1) Creates prototype-based `Error` classes (child/subclass) : _\"Extended Errors\"_.\n1) Those _\"Extended Errors\"_, accepts `cause` (`Error`); very much like how it is with Java `Exception`.\n1) Appends stack of `cause` to the bottom of instantiated _\"Extended Errors\"_ stack.\n1) _\"Extended Errors\"_ constructor \u0026 argument _(w/ optional `new`)_:\n    1) `new ExtendedError(options)`\n    1) `ExtendedError(options)`\n\nYes, much like JavaScript's native `Error`, \"Extended Errors\" can be written/used \"factory-like\" (without the `new` keyword).\n\n#### \"Extended Errors\" Arguments (constructor)\n\n\"Extended Errors\" accepts a single [object literal](https://www.w3schools.com/js/js_objects.asp) as argument:\n\n```javascript\nconst extendError = require('error-extender');\nconst ServiceError = extendError('ServiceError');\ntry {\n  // ... something throws something\n} catch (error) {\n  throw new ServiceError({\n    message: 'An error has occurred',\n    data: { ref: '7e9f876ca116' },\n    cause: error\n  });\n}\n```\n\nThe options (object literal keys) are as follows:\n\n| key       | alias | expected type       |\n| :-------- | :---: | ------------------- |\n| `message` | `m`   | `string`            |\n| `data`    | `d`   | `any`               |\n| `cause`   | `c`   | `instancedof Error` |\n\nGiven the alias, you may construct extended errors by:\n\n```javascript\nconst extendError = require('error-extender');\nconst ServiceError = extendError('ServiceError');\ntry {\n  // ... something throws something\n} catch (error) {\n  throw new ServiceError({\n    m: 'An error has occurred',\n    d: { ref: '7e9f876ca116' },\n    c: error\n  });\n}\n```\n\n**Note**: Aliases are evaluated first; hence if you have both `m` and `message`, if `m`'s value is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), then `m`'s value will be used.\n\n#### Instance Properties\n\nAs with `Error`, \"Extended Errors\" would have the following properties:\n\n* `name`\n* `message`\n* `stack`\n\n... \"Extended Errors\" shall have the following additiona properties:\n\n* `data` - _(as set in constructor args)_\n* `cause` - _(as set in constructor args)_\n\n#### `data` merging w/ `defaultData`\n\nYes, you heard right, instance `data` merges with `defaultData`!!!\n\nSee example below:\n\n```javascript\nconst extendError = require('error-extender');\n\nconst AppError = extendError('ServiceError', {\n  defaultData: { status: 503, message: 'An unhandled error has occurred.' }\n});\n\nconst appError = new AppError({ d: { status: 401 } });\n\nrequire('assert').deepStrictEqual(\n  appError.data, {\n    status: 401,\n    message: 'An unhandled error has occurred.'\n  });\n// no error\n```\n\n## The inspiration (thanks [`bluebird`](https://www.npmjs.com/package/bluebird)!):\n\n```javascript\nconst Promise = require('bluebird');\n// ...\nconst extendError = require('error-extender');\n// ...\nconst ServiceError = extendError('ServiceError');\nconst ServiceStateError = extendError(\n  'ServiceStateError',\n  { parent: ServiceError });\n// ...\nfunction aServiceFunction() {\n  return new Promise(\n    function (resolve, reject) {\n      // ... multiple things that may throw your\n      //     custom \"expected\" errors\n    })\n    .catch(ServiceStateError, function (error) {\n      // ... your \"common way\" of handling\n      //     ServiceStateError\n      // ... then propagate\n    })\n    .catch(ServiceError, function (error) {\n      // ... your \"common generc way\" of handling\n      //     ServiceError\n      // ... then propagate\n    })\n    .catch(function (error) {\n      // ... the \"catch all\"\n      // ... then propagate\n    });\n}\n```\n\nWith JavaScript, I felt quite stifled when I was limited to:\n\n1) Do selective/custom handling based on matching messages from `throw new Error('..')`.\n1) Return/propagate [JSend](https://labs.omniti.com/labs/jsend)-like responses to function \"callers\"/\"users\".\n1) ... or whatever error possible passing/handling could be done, throughout functions and callers/users.\n\nWith **`error-extender`** with help from [syntactic-sugar](https://en.wikipedia.org/wiki/Syntactic_sugar) from [`bluebird`](https://www.npmjs.com/package/bluebird), you can improve _(or even standardize)_ your way of propagating/handling errors throughout your application.\ncallers.\n\n## License\n\n### MIT License\n\n#### Copyright (c) 2018 Joseph Baking\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\n**THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpbaking%2Ferror-extender","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpbaking%2Ferror-extender","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpbaking%2Ferror-extender/lists"}