extended-exceptions
Advanced tools
Comparing version 0.9.0 to 0.10.0
{ | ||
"name": "extended-exceptions.js", | ||
"version": "0.9.0", | ||
"description": "Easy custom js exceptions for node & browser", | ||
"version": "0.10.0", | ||
"description": "Easy custom js exceptions for node & browser, AMD.", | ||
"keywords": [ | ||
@@ -13,3 +13,5 @@ "exceptions", | ||
"homepage": "https://github.com/Offirmo/extended-exceptions.js", | ||
"main": "extended_exceptions.js" | ||
"main": "extended_exceptions.js", | ||
"dependencies": { | ||
} | ||
} |
@@ -41,16 +41,22 @@ /* New Errors to be thrown, | ||
// contrary to Error, this error will be correctly derivable | ||
function ExtendedError(message) { | ||
function ExtendedError(message_or_error) { | ||
// Note : This is ONE way to derive from error. | ||
// I find it convenient but I don't pretend it's the best | ||
//console.log("ExtendedError constructor..."); | ||
if(typeof message === 'undefined') | ||
message = ""; //<default message>"; | ||
var err, message; | ||
// we must create an error to have an up-to-date stacktrace | ||
var err = new Error(message); | ||
if(message_or_error instanceof Error) { | ||
// we are wrapping an existing error | ||
err = message_or_error; | ||
message = err.message; | ||
} | ||
else { | ||
// we must create an error to have an up-to-date stacktrace | ||
message = message_or_error; | ||
err = new Error(message); | ||
} | ||
// import everything neded into 'this' object | ||
// so it has the same prototype as an Error object | ||
// we must copy manually due to Error specificity | ||
// Import everything neded into 'this' object | ||
// so it has the same prototype as an Error object. | ||
// We must copy manually due to Error object specificity. | ||
this.message = message; // assign directly from param. If we assign from err.message, FF sometimes fails to copy for unknown reasons | ||
@@ -61,6 +67,4 @@ this.stack = err.stack; | ||
this.name = "ExtendedError"; | ||
//console.log("ExtendedError constructor done, message = " + this.message); | ||
} | ||
ExtendedError.prototype = Object.create(error_instance.prototype); // no, we can't directly access Error.prototype | ||
ExtendedError.prototype = Object.create(error_instance.prototype); // since we can't directly access Error.prototype | ||
ExtendedError.prototype.constructor = ExtendedError; | ||
@@ -67,0 +71,0 @@ |
{ | ||
"name": "extended-exceptions", | ||
"version": "0.9.0", | ||
"description": "Easy custom js exceptions for node & browser", | ||
"keywords": [ | ||
"exceptions", | ||
"browser", | ||
"server" | ||
], | ||
"license": "public domain", | ||
"author": "Offirmo <offirmo.net@gmail.com> (http://offirmo.net)", | ||
"main": "extended_exceptions.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Offirmo/extended-exceptions.js.git" | ||
}, | ||
"dependencies": {}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Offirmo/extended-exceptions.js/issues" | ||
}, | ||
"homepage": "https://github.com/Offirmo/extended-exceptions.js", | ||
"scripts": { | ||
"test": "cd test_runner && npm install && make" | ||
} | ||
"name": "extended-exceptions", | ||
"version": "0.10.0", | ||
"description": "Easy custom js exceptions for node & browser, AMD.", | ||
"keywords": "exceptions, browser, server", | ||
"license": "public domain", | ||
"author": "Offirmo <offirmo.net@gmail.com> (http://offirmo.net)", | ||
"homepage": "https://github.com/Offirmo/extended-exceptions.js", | ||
"main": "extended_exceptions.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Offirmo/extended-exceptions.js.git" | ||
}, | ||
"dependencies": { | ||
"amdefine": "~0.1" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Offirmo/extended-exceptions.js/issues" | ||
}, | ||
"scripts": { | ||
"test": "npm install && cd test_runner && make" | ||
} | ||
} |
121
README.md
extended-exceptions.js | ||
====================== | ||
[![Build Status](https://travis-ci.org/Offirmo/extended-exceptions.js.png?branch=master)](https://travis-ci.org/Offirmo/extended-exceptions.js) | ||
[![NPM version](https://badge.fury.io/js/extended-exceptions.png)](http://badge.fury.io/js/extended-exceptions) | ||
[![Bower version](https://badge.fury.io/bo/extended-exceptions.js.png)](http://badge.fury.io/bo/extended-exceptions.js) | ||
[![Project status](http://img.shields.io/badge/project_status-highly_experimental-red.png)](http://offirmo.net/classifying-open-source-projects-status/) | ||
[![license](http://img.shields.io/badge/license-public_domain-brightgreen.png)](http://unlicense.org/) | ||
[![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) | ||
[![Code Climate](https://codeclimate.com/github/Offirmo/extended-exceptions.js.png)](https://codeclimate.com/github/Offirmo/extended-exceptions.js) | ||
[![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) | ||
[![Gittip](http://img.shields.io/gittip/Offirmo.png)](https://www.gittip.com/Offirmo/) | ||
Introduction | ||
------------ | ||
Allow easy declaration of custom exceptions. | ||
Allow easy declaration of custom javascript errors/exceptions. | ||
Also defines common exceptions for Javascript that are missing in the standard : RuntimeError, NotImplementedError, InvalidArgument, OutOfRange, etc. | ||
Also provides common exceptions for Javascript that are missing in the standard : RuntimeError, NotImplementedError, | ||
InvalidArgument, OutOfRange, etc. (inspiration taken from the C++11 standard) | ||
@@ -15,9 +27,36 @@ Works in node.js + browser, AMD. | ||
Includes special workarounds for Firefox "Error" object which has sometimes a strange behaviour. | ||
License : public domain (http://unlicense.org/) | ||
Interesting read : http://dailyjs.com/2014/01/30/exception-error/ | ||
``` | ||
Predefined exceptions | ||
--------------------- | ||
Note : we keep the "error" naming scheme of standard javascript. | ||
* Error (standard) | ||
* ExtendedError | ||
* LogicError | ||
* InvalidArgument | ||
* LengthError | ||
* OutOfRange | ||
* RuntimeError | ||
* NotImplementedError | ||
* UnknownEnumValueError | ||
* IllegalStateError | ||
* InvariantNotMetError | ||
Usage | ||
----- | ||
Throwing an exception : | ||
```javascript | ||
if (typeof define !== 'function') { var define = require('amdefine')(module); } // node only | ||
```javascript | ||
define( | ||
@@ -30,3 +69,5 @@ [ | ||
throw new EE.RuntimeError("Oups !"); | ||
// Feature 1 - use one of the predefined errors | ||
throw new EE.NotImplementedError("TODO !"); | ||
throw new EE.InvalidArgument("Please !"); | ||
``` | ||
@@ -44,7 +85,17 @@ | ||
// inheriting from RuntimeError | ||
var CustomError = EE.create_custom_error("CustomError", EE.RuntimeError); | ||
throw new CustomError("Oups !"); | ||
// Feature 2 - create a brand new error | ||
// inheriting from (for example) RuntimeError (could be just Error) | ||
var CustomJsonSerializationError = EE.create_custom_error("CustomError", EE.RuntimeError); | ||
// Alternative 1 - use it directly | ||
throw new CustomJsonSerializationError("Oups !"); | ||
// Alternative 2 - use it to cast an existing error | ||
try { | ||
JSON.parse(stuff); | ||
} | ||
catch(e) { | ||
throw new CustomJsonSerializationError(e); // new error will have same stack and message | ||
} | ||
... | ||
@@ -58,26 +109,46 @@ | ||
Catching and testing exceptions : | ||
- exactly as you expect it. | ||
Works in chai exactly as you expect it : | ||
```javascript | ||
if (typeof define !== 'function') { var define = require('amdefine')(module); } // node only | ||
Predefined exceptions | ||
--------------------- | ||
define( | ||
[ | ||
'chai', | ||
'<class under test>', | ||
'extended-exceptions', | ||
'mocha' | ||
], | ||
function(chai, CUT, EE) { | ||
"use strict"; | ||
Note : we keep the "error" naming scheme of standard javascript. | ||
var expect = chai.expect; | ||
chai.should(); | ||
* Error (standard) | ||
* ExtendedError | ||
* LogicError | ||
* InvalidArgument | ||
* LengthError | ||
* OutOfRange | ||
* RuntimeError | ||
* NotImplementedError | ||
* UnknownEnumValueError | ||
* IllegalStateError | ||
* InvariantNotMetError | ||
describe('Nice stuff', function() { | ||
describe('processing', function() { | ||
it('should detect bad state', function() { | ||
var out = CUT.make_new(); | ||
var tempfn = function() { out.dostuff(); }; | ||
tempfn.should.throw(EE.IllegalStateError, "Not initialized !"); // works fine | ||
}); | ||
}); // describe feature | ||
}); // describe CUT | ||
}); // requirejs module | ||
Installation | ||
------------ | ||
Bower : `bower install extended-exceptions.js` | ||
Npm : `npm install extended-exceptions` | ||
Unit tests | ||
---------- | ||
in the 'spec' folder. See also readme.txt in the 'test_runner' folder. | ||
Browser : open `test_runner/mocha.html` | ||
Node : `npm test` |
@@ -17,3 +17,3 @@ if (typeof define !== 'function') { var define = require('amdefine')(module); } | ||
describe('ExtendedError (base custom error)', function() { | ||
describe('ExtendedError (base)', function() { | ||
@@ -43,2 +43,17 @@ /*it('test', function() { | ||
it('should allow wrapping/retyping of an existing Error', function() { | ||
var base_err = new Error("Test error"); | ||
var out = new EE.ExtendedError(base_err); | ||
out.name.should.equals("ExtendedError"); | ||
out.message.should.equals("Test error"); | ||
out.stack.should.not.be.empty; | ||
expect( out.stack ).to.equals(base_err.stack); | ||
}); | ||
it('should work along chai expectations', function() { | ||
var tempfn = function() {throw new EE.IllegalStateError("Not started !"); }; | ||
tempfn.should.throw(EE.IllegalStateError, "Not started !"); | ||
}); | ||
}); | ||
@@ -67,3 +82,3 @@ | ||
// and the chai assertion should work (sadly, it doesn't always in FF) | ||
// and the chai assertion should work (even in FF ;) | ||
var tempfn = function() { | ||
@@ -75,56 +90,66 @@ throw new CustomErrorClass("test_extended_error 3"); | ||
describe('LogicError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.LogicError, "LogicError", EE.ExtendedError ); | ||
}); | ||
}); // describe feature | ||
describe('InvalidArgument', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.InvalidArgument, "InvalidArgument", EE.LogicError ); | ||
}); | ||
}); // describe feature | ||
describe('Custom errors creation', function() { | ||
it('should allow easy creation of a custom error which works', function() { | ||
var CustomError = EE.create_custom_error("CustomError", EE.RuntimeError); | ||
describe('LengthError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.LengthError, "LengthError", EE.LogicError ); | ||
test_extended_error(CustomError, "CustomError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('OutOfRange', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.OutOfRange, "OutOfRange", EE.LogicError ); | ||
}); | ||
}); // describe feature | ||
describe('RuntimeError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.RuntimeError, "RuntimeError", EE.ExtendedError ); | ||
}); | ||
}); // describe feature | ||
describe('predefined error', function() { | ||
describe('LogicError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.LogicError, "LogicError", EE.ExtendedError ); | ||
}); | ||
}); // describe feature | ||
describe('NotImplementedError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.NotImplementedError, "NotImplementedError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('InvalidArgument', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.InvalidArgument, "InvalidArgument", EE.LogicError ); | ||
}); | ||
}); // describe feature | ||
describe('UnknownEnumValueError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.UnknownEnumValueError, "UnknownEnumValueError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('LengthError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.LengthError, "LengthError", EE.LogicError ); | ||
}); | ||
}); // describe feature | ||
describe('IllegalStateError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.IllegalStateError, "IllegalStateError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('OutOfRange', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.OutOfRange, "OutOfRange", EE.LogicError ); | ||
}); | ||
}); // describe feature | ||
describe('custom errors creation', function() { | ||
it('should allow easy creation of a custom error which works', function() { | ||
var CustomError = EE.create_custom_error("CustomError", EE.RuntimeError); | ||
describe('RuntimeError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.RuntimeError, "RuntimeError", EE.ExtendedError ); | ||
}); | ||
}); // describe feature | ||
test_extended_error(CustomError, "CustomError", EE.RuntimeError ); | ||
}); | ||
describe('NotImplementedError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.NotImplementedError, "NotImplementedError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('UnknownEnumValueError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.UnknownEnumValueError, "UnknownEnumValueError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('IllegalStateError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.IllegalStateError, "IllegalStateError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
describe('InvariantNotMetError', function() { | ||
it('should work', function() { | ||
test_extended_error(EE.InvariantNotMetError, "InvariantNotMetError", EE.RuntimeError ); | ||
}); | ||
}); // describe feature | ||
}); // describe feature | ||
@@ -131,0 +156,0 @@ }); |
{ | ||
"name": "extended-exceptions-test-runner", | ||
"version": "1.0.0", | ||
"version": "99.99.99", | ||
"dependencies": { | ||
"underscore": "latest", | ||
"chai": "latest", | ||
@@ -8,0 +6,0 @@ "mocha": "latest", |
{ | ||
"name": "extended-exceptions-test-runner", | ||
"description": "node test runner for extended-exceptions.js", | ||
"version": "1.0.0", | ||
"description": "node test runner for extended-exceptions", | ||
"version": "99.99.99", | ||
"private": true, | ||
"dependencies": { | ||
"underscore": "latest", | ||
"amdefine": "latest", | ||
@@ -10,0 +8,0 @@ "chai": "latest", |
"use strict"; | ||
//console.log("hello from tests init !"); | ||
// this files does nothing and just serve as an entry point for node unit tests |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20750
306
151
1
+ Addedamdefine@~0.1
+ Addedamdefine@0.1.1(transitive)