{"id":26647237,"url":"https://github.com/offirmo/extended-exceptions.js","last_synced_at":"2025-06-11T13:04:39.386Z","repository":{"id":11186325,"uuid":"13565229","full_name":"Offirmo/extended-exceptions.js","owner":"Offirmo","description":"Allow easy declaration of custom javascript errors. Also provides useful errors that are missing from the standard : RuntimeError, InvalidArgument, OutOfRange, NotImplementedError, etc. Works in node.js + browser, AMD","archived":false,"fork":false,"pushed_at":"2014-06-24T15:35:09.000Z","size":300,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T17:06:10.854Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Offirmo.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}},"created_at":"2013-10-14T15:33:24.000Z","updated_at":"2020-01-08T16:07:45.000Z","dependencies_parsed_at":"2022-08-30T14:51:22.458Z","dependency_job_id":null,"html_url":"https://github.com/Offirmo/extended-exceptions.js","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Fextended-exceptions.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Fextended-exceptions.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Fextended-exceptions.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Offirmo%2Fextended-exceptions.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Offirmo","download_url":"https://codeload.github.com/Offirmo/extended-exceptions.js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245372366,"owners_count":20604490,"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":"2025-03-24T23:51:27.976Z","updated_at":"2025-03-24T23:51:28.970Z","avatar_url":"https://github.com/Offirmo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"extended-exceptions.js\n======================\n\n[![Build Status](https://travis-ci.org/Offirmo/extended-exceptions.js.png?branch=master)](https://travis-ci.org/Offirmo/extended-exceptions.js)\n[![NPM version](https://badge.fury.io/js/extended-exceptions.png)](http://badge.fury.io/js/extended-exceptions)\n[![Bower version](https://badge.fury.io/bo/extended-exceptions.js.png)](http://badge.fury.io/bo/extended-exceptions.js)\n[![Dependency Status](https://gemnasium.com/Offirmo/extended-exceptions.js.png)](https://gemnasium.com/Offirmo/extended-exceptions.js)\n[![Project status](http://img.shields.io/badge/project_status-stable_and_active-brightgreen.png)](http://offirmo.net/classifying-open-source-projects-status/)\n[![license](http://img.shields.io/badge/license-public_domain-brightgreen.png)](http://unlicense.org/)\n[![graph status](https://sourcegraph.com/api/repos/github.com/Offirmo/extended-exceptions.js/badges/status.png)](https://sourcegraph.com/github.com/Offirmo/extended-exceptions.js)\n[![Code Climate](https://codeclimate.com/github/Offirmo/extended-exceptions.js.png)](https://codeclimate.com/github/Offirmo/extended-exceptions.js)\n[![Total views](https://sourcegraph.com/api/repos/github.com/Offirmo/extended-exceptions.js/counters/views.png)](https://sourcegraph.com/github.com/Offirmo/extended-exceptions.js)\n[![Gittip](http://img.shields.io/gittip/Offirmo.png)](https://www.gittip.com/Offirmo/)\n\nIntroduction\n------------\n\nAllow easy declaration of custom javascript errors/exceptions.\n\nAlso provides common exceptions for Javascript that are missing in the standard : RuntimeError, NotImplementedError,\nInvalidArgument, OutOfRange, etc. (inspiration taken from the C++11 standard)\n\nWorks in node.js + browser, AMD.\n\nFull testsuite. No dependencies.\n\nIncludes special workarounds for Firefox \"Error\" object which has sometimes a strange behaviour.\n\nLicense : public domain (http://unlicense.org/)\n\nInteresting reads :\n- http://dailyjs.com/2014/01/30/exception-error/\n- http://en.cppreference.com/w/cpp/error/exception\n\n\nPredefined errors\n-----------------\n\nNote : we keep the \"error\" naming scheme of standard javascript.\n\n```\nError (standard)  \u003c-- javascript root exception\n+ EvalError       \u003c-- Vanilla javascript exceptions\n+ RangeError      \u003c-- ...\n+ ReferenceError  \u003c-- ...\n+ SyntaxError     \u003c-- ...\n+ TypeError       \u003c-- ...\n+ URIError        \u003c-- ...\n+ ExtendedError   \u003c-- Here start our custom exceptions\n  + LogicError\n    + InvalidArgument\n    + LengthError\n    + OutOfRange\n    + InvariantNotMet\n  + RuntimeError\n    + NotImplemented\n    + UnknownEnumValue\n    + IllegalState\n```\n\n\nUsage\n-----\n\n```javascript\nif (typeof define !== 'function') { var define = require('amdefine')(module); } // node only\n\ndefine(\n[\n\t'extended-exceptions'\n],\nfunction(EE) {\n\t\"use strict\";\n\n\t// Feature 1 - use one of the predefined errors\n\tthrow new EE.NotImplemented(\"TODO !\");\n\tthrow new EE.InvalidArgument(\"Please !\");\n```\n\nDefining our own exceptions inheriting from an existing one :\n\n```javascript\ndefine(\n[\n\t'extended-exceptions'\n],\nfunction(EE) {\n\t\"use strict\";\n\n\t// Feature 2 - create a brand new error\n\t// inheriting from (for example) RuntimeError (could be just Error)\n\tvar CustomJsonSerializationError = EE.create_custom_error(\"CustomError\", EE.RuntimeError);\n\n\t// Alternative 1 - use it directly\n\tthrow new CustomJsonSerializationError(\"Oups !\");\n\n\t// Alternative 2 - use it to cast an existing error\n\ttry {\n\t\tJSON.parse(stuff);\n\t}\n\tcatch(e) {\n\t\tthrow new CustomJsonSerializationError(e); // new error will have same stack and message\n\t}\n\n\t...\n\t\n\tconsole.log( caught_error.name );\n\tconsole.log( caught_error.message );\n\tconsole.log( caught_error.stack );\n```\n\nCatching and testing exceptions :\n- exactly as you expect it.\n\n\nWorks in chai exactly as you expect it :\n```javascript\nif (typeof define !== 'function') { var define = require('amdefine')(module); } // node only\n\ndefine(\n[\n\t'chai',\n\t'\u003cclass under test\u003e',\n\t'extended-exceptions',\n\t'mocha'\n],\nfunction(chai, CUT, EE) {\n\t\"use strict\";\n\n\tvar expect = chai.expect;\n\tchai.should();\n\n\tdescribe('Nice stuff', function() {\n\n\t\tdescribe('processing', function() {\n\n\t\t\tit('should detect bad state', function() {\n\t\t\t\tvar out = CUT.make_new();\n\n\t\t\t\tvar tempfn = function() { out.dostuff(); };\n\t\t\t\ttempfn.should.throw(EE.IllegalStateError, \"Not initialized !\"); // works fine\n\t\t\t});\n\t\t}); // describe feature\n\t}); // describe CUT\n}); // requirejs module\n```\n\nInstallation\n------------\n\nBower : `bower install extended-exceptions.js`\nNpm : `npm install extended-exceptions`\n\nUnit tests\n----------\n\nBrowser : open `test_runner/mocha.html`\nNode : `npm test`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foffirmo%2Fextended-exceptions.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foffirmo%2Fextended-exceptions.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foffirmo%2Fextended-exceptions.js/lists"}