{"id":17222823,"url":"https://github.com/onury/custom-error-test","last_synced_at":"2025-07-01T09:07:49.740Z","repository":{"id":66781740,"uuid":"78061688","full_name":"onury/custom-error-test","owner":"onury","description":"Compare and test various custom error implementations.","archived":false,"fork":false,"pushed_at":"2021-10-29T13:15:24.000Z","size":164,"stargazers_count":34,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-08T09:05:15.979Z","etag":null,"topics":["custom-error","error","inheritance","javascript"],"latest_commit_sha":null,"homepage":"https://onury.io/custom-error-test","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/onury.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,"zenodo":null}},"created_at":"2017-01-04T23:34:05.000Z","updated_at":"2025-02-10T00:03:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"ec07f302-1fab-4115-afe3-b1ddc17997ba","html_url":"https://github.com/onury/custom-error-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/onury/custom-error-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onury%2Fcustom-error-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onury%2Fcustom-error-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onury%2Fcustom-error-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onury%2Fcustom-error-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onury","download_url":"https://codeload.github.com/onury/custom-error-test/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onury%2Fcustom-error-test/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262933317,"owners_count":23386784,"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":["custom-error","error","inheritance","javascript"],"created_at":"2024-10-15T04:06:26.612Z","updated_at":"2025-07-01T09:07:49.722Z","avatar_url":"https://github.com/onury.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Custom Errors in JavaScript\n\nCustom errors are useful when you want to track code with context-specific information. There are several ways to define a custom error in JavaScript. But since inheritance is not a first-citizen feature in JS (until ES2015); there's also so much discussion on how to implement a proper custom error.\n\nSee this [SO Question][so-question], [this][so-answer] and [this][so-answer-2] answers, that leads to [this discussion][gist-discuss] (with [@mbrowne][mbrowne]) and finally, to this module you're viewing.\n\nThis module tests various implementations for custom errors, widely used by developers; such as methods making use of `Error.call()`, `Object.create()`, `new Error()`, `Object.setPrototypeOf()`, `extends` (ES2015) and `util.inherits` (Node). See `/errors` directory for [included implementations][errors-dir].\n\nWhat we expect from a custom error implementation is:\n- `e.constructor.name` MUST be set to custom error name.\n- `E.prototype` MUST inherit from `Error.prototype`.\n- `e.message` MUST be set.\n- `e` MUST be an instance of `Error`.\n- `e` MUST be an instance of `CustomError`.\n- `e.stack` MUST be set and should have line-tracable info.\n- `e.tostring()` MUST return error name and/or message.\n- `({}).toString.call(e)` SHOULD output `\"error\"`.\n- and some more... \n\n### Test\n\nRun `npm start` which will output test results to console for the current Node.js environment. And will also generate an HTML file, which will display results for the used browser. \n\nBetter, you can [directly view this page][test-page] for testing your current browser.\n\n[![Screenshot](https://raw.github.com/onury/custom-error-test/master/result.png)][test-page]\n_Capture of a test on Chrome 55 browser._\n\n### CustomError: The One\n\nSo; with some research, discussions and these tests I think [**this**][the-one] is the one closest to ES2015's `extends Error`. If you think this is a bit over-kill, [**this**][so-answer] should be quite enough.\n\n### License\nMIT.\n\n[test-page]:https://onury.github.io/custom-error-test/\n[errors-dir]:https://github.com/onury/custom-error-test/tree/master/errors\n[getPrototypeOf]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf\n[setPrototypeOf]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf\n[Error]:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error\n[capturestacktrace_constr]:https://nodejs.org/api/errors.html#errors_error_capturestacktrace_targetobject_constructoropt\n[proto]:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/proto\n[so-question]:http://stackoverflow.com/q/1382107/112731\n[so-answer]:http://stackoverflow.com/a/35881508/112731\n[so-answer-2]:http://stackoverflow.com/a/41338601/112731\n[gist-discuss]:https://gist.github.com/mbrowne/4af54767dcb3d529648f5a8aa11d6348\n[the-one]:https://github.com/onury/custom-error-test/tree/master/the-one/CustomError.js\n[mbrowne]:https://github.com/mbrowne","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonury%2Fcustom-error-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonury%2Fcustom-error-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonury%2Fcustom-error-test/lists"}