pax_global_header00006660000000000000000000000064123474133320014514gustar00rootroot0000000000000052 comment=fbee48fed75bd8e039b5a3f3a7e45f62b6d3b28d js-standard-error-1.1.0/000077500000000000000000000000001234741333200150545ustar00rootroot00000000000000js-standard-error-1.1.0/.gitignore000066400000000000000000000000261234741333200170420ustar00rootroot00000000000000/node_modules/ /*.tgz js-standard-error-1.1.0/.npmignore000066400000000000000000000000071234741333200170500ustar00rootroot00000000000000/*.tgz js-standard-error-1.1.0/CHANGELOG.md000066400000000000000000000024211234741333200166640ustar00rootroot00000000000000## 1.1.0 (Jun 15, 2014) - Allows overwriting the `stack` property if given in the object: ```javascript new StandardError({stack: stackOverflow}) ``` - Fixes stack's first line when name given in the object: ```javascript new StandardError({name: "UnknownError"}) ``` - Allows overriding `name` explicitly through the subclass's prototype. Previously StandardError got the name only from the constructor function's `name` property or from the passed-in object. ```javascript function ChildError(msg, props) { StandardError.apply(this, arguments) } ChildError.prototype = Object.create(StandardError.prototype, { constructor: {value: ChildError, configurable: true, writeable: true} }) ChildError.prototype.name = "FallacyError" ``` - Sets `StandardError.prototype.name` explicitly for cases where the code is minified. If you don't minify your code (like when using it on the server side) you don't need to set `name` explicitly on your subclasses and can depend on StandardError.js finding it out from the constructor function. - Uses `Error.captureStackTrace` only if available, so it'll work in non-V8 JavaScript engines. ## 1.0.0 (May 1, 2014) - First standard release. There are now [15 competing standards](https://xkcd.com/927/)! js-standard-error-1.1.0/LICENSE000066400000000000000000000016571234741333200160720ustar00rootroot00000000000000StandardError.js Copyright (C) 2014– Andri Möll This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version. Additional permission under the GNU Affero GPL version 3 section 7: If you modify this Program, or any covered work, by linking or combining it with other code, such other code is not for that reason alone subject to any of the requirements of the GNU Affero GPL version 3. In summary: - You can use this program for no cost. - You can use this program for both personal and commercial reasons. - You do not have to share your own program's code which uses this program. - You have to share modifications (e.g bug-fixes) you've made to this program. For the full copy of the GNU Affero General Public License see: http://www.gnu.org/licenses. js-standard-error-1.1.0/Makefile000066400000000000000000000011711234741333200165140ustar00rootroot00000000000000NODE_OPTS = TEST_OPTS = love: @echo "Feel like makin' love." test: @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R dot $(TEST_OPTS) spec: @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R spec $(TEST_OPTS) autotest: @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R dot --watch $(TEST_OPTS) autospec: @node $(NODE_OPTS) ./node_modules/.bin/_mocha -R spec --watch $(TEST_OPTS) pack: npm pack publish: npm publish tag: git tag "v$$(node -e 'console.log(require("./package").version)')" clean: rm -f *.tgz npm prune --production .PHONY: love .PHONY: test spec autotest autospec .PHONY: pack publish tag .PHONY: clean js-standard-error-1.1.0/README.md000066400000000000000000000106331234741333200163360ustar00rootroot00000000000000StandardError.js ================ [![NPM version][npm-badge]](http://badge.fury.io/js/standard-error) [npm-badge]: https://badge.fury.io/js/standard-error.png StandardError.js is a tiny JavaScript library that simplifies creating subclasses of `Error` for **custom error classes** with the correct `name` and `stack` property. Saves you from writing a few lines of boilerplate. ### Tour - Create **custom error classes** and add new behavior to them while keeping the standard `Error` behavior in tact. - Add **extra properties** to the error by just passing in an object. - StandardError.js sets the error's **stack trace correctly**, even if your error class **subclasses/inherits** from StandardError. Just inheriting from `Error` with `Object.create` breaks the stack trace. - Every `StandardError` instance is also an instance of `Error`. - Serializes all expected properties when passing it to `JSON.stringify`. Did you know that the default `Error` object serializes to an empty object (`{}`)? - Works both in Node.js and browsers and sets the stack trace via `Error.captureStackTrace` where available. Installing ---------- ``` npm install standard-error ``` Using ----- Just require StandardError.js and either use it directly or inherit from it for your custom error class. ### Throwing StandardError Like `Error`, `StandardError` takes a message argument, but in addition to that, you may give it an object with other properties to be set: ```javascript var StandardError = require("standard-error") throw new StandardError("Not Found", {code: 404}) ``` The thrown instance of `StandardError` will then have both the `message` and the `code` property. It'll also also have a `name` property set to `"StandardError"`. You can skip the explicit `message` argument and give everything as an object of properties: ```javascript new StandardError({message: "Not Found", code: 404}) ``` **Note**: All properties besides `stack` will be enumerable for easier serialization with `JSON.stringify`. That includes the `name` property which will be set from the constructor's name (defaults to `"StandardError"`). ### Subclassing and inheriting from StandardError The real benefit of StandardError.js comes from subclassing it to create new error classes and adding custom behavior to them. Let's create an `HttpError` that we can instantiate with the HTTP status code (`new HttpError(404)`) and have it set the message automatically based on that: ```javascript var Http = require("http") var StandardError = require("standard-error") function HttpError(code, msg) { StandardError.call(this, msg || Http.STATUS_CODES[code], {code: code}) } HttpError.prototype = Object.create(StandardError.prototype, { constructor: {value: HttpError, configurable: true, writable: true} }) ``` **Note** that you must set the `constructor` property like in the above example. First, that's the proper way to subclass in JavaScript and second, StandardError.js depends on that to know which functions to skip in the stack trace. #### Name StandardError.js finds out the name (`err.name`) of your subclassed error from its constructor function. However, if you minify your code, you can also set or change it explicitly: ```javascript ChildError.prototype.name = "FallacyError" ``` ### Adding behavior to your subclass of StandardError Now that you've inherited, you can, for example, customize stringifying by overwriting `toString` on your subclass. To get `new HttpError(404)` to print itself as `404 Not Found`: ```javascript HttpError.prototype.toString = function() { return this.code + " " + this.message } ``` License ------- StandardError.js is released under a *Lesser GNU Affero General Public License*, which in summary means: - You **can** use this program for **no cost**. - You **can** use this program for **both personal and commercial reasons**. - You **do not have to share your own program's code** which uses this program. - You **have to share modifications** (e.g. bug-fixes) you've made to this program. For more convoluted language, see the `LICENSE` file. About ----- **[Andri Möll](http://themoll.com)** typed this and the code. [Monday Calendar](https://mondayapp.com) supported the engineering work. If you find StandardError.js needs improving, please don't hesitate to type to me now at [andri@dot.ee][email] or [create an issue online][issues]. [email]: mailto:andri@dot.ee [issues]: https://github.com/moll/js-standard-error/issues js-standard-error-1.1.0/index.js000066400000000000000000000016531234741333200165260ustar00rootroot00000000000000var has = Object.hasOwnProperty var proto = Object.getPrototypeOf var trace = Error.captureStackTrace module.exports = StandardError function StandardError(msg, props) { // Let all properties be enumerable for easier serialization. if (msg && typeof msg == "object") props = msg, msg = undefined else this.message = msg // Name has to be an own property (or on the prototype a single step up) for // the stack to be printed with the correct name. if (props) for (var key in props) this[key] = props[key] if (!has.call(this, "name")) this.name = has.call(proto(this), "name")? this.name : this.constructor.name if (trace && !("stack" in this)) trace(this, this.constructor) } StandardError.prototype = Object.create(Error.prototype, { constructor: {value: StandardError, configurable: true, writable: true} }) // Set name explicitly for when the code gets minified. StandardError.prototype.name = "StandardError" js-standard-error-1.1.0/package.json000066400000000000000000000015301234741333200173410ustar00rootroot00000000000000{ "name": "standard-error", "version": "1.1.0", "description": "Tiny library that simplifies subclassing and inheriting from Error while keeping the correct name and stack. Also supports constructing from an object of properties. Saves you from boilerplate.", "keywords": ["error", "exception"], "homepage": "https://github.com/moll/js-standard-error", "bugs": "https://github.com/moll/js-standard-error/issues", "author": { "name": "Andri Möll", "email": "andri@dot.ee", "url": "http://themoll.com" }, "repository": { "type": "git", "url": "git://github.com/moll/js-standard-error.git" }, "licenses": [{ "type": "LAGPL", "url": "https://github.com/moll/js-standard-error/blob/master/LICENSE" }], "main": "index.js", "scripts": {"test": "make test"}, "devDependencies": { "mocha": ">= 1.18.0 < 2", "must": "< 1" } } js-standard-error-1.1.0/test/000077500000000000000000000000001234741333200160335ustar00rootroot00000000000000js-standard-error-1.1.0/test/index_test.js000066400000000000000000000075071234741333200205500ustar00rootroot00000000000000var StandardError = require("..") function ChildError(msg, props) { StandardError.apply(this, arguments) } ChildError.prototype = Object.create(StandardError.prototype, { constructor: {value: ChildError, configurable: true, writeable: true} }) describe("StandardError", function() { describe("new", function() { it("must be an instance of StandardError", function() { new StandardError().must.be.an.instanceof(StandardError) }) it("must set message", function() { new StandardError("Problem").must.have.own("message", "Problem") }) it("must set properties", function() { var err = new StandardError({message: "Problem", code: 500}) err.must.have.own("message", "Problem") err.must.have.own("code", 500) }) it("must set message and properties", function() { var err = new StandardError("Problem", {code: 500}) err.must.have.own("message", "Problem") err.must.have.own("code", 500) }) it("must set message from object given both", function() { var err = new StandardError("Problem", {message: "OK"}) err.must.have.own("message", "OK") }) it("must set name", function() { new StandardError().must.have.own("name", "StandardError") }) it("must set name given emtpy object", function() { new StandardError({}).must.have.own("name", "StandardError") }) it("must set name from constructor", function() { new ChildError().must.have.own("name", "ChildError") }) it("must set name from prototype", function() { function ChildError(msg, props) { StandardError.apply(this, arguments) } ChildError.prototype = Object.create(StandardError.prototype) ChildError.prototype.name = "FallacyError" new ChildError().must.have.own("name", "FallacyError") }) it("must set name from object", function() { var err = new StandardError("Problem", {name: "FallacyError"}) err.must.have.own("name", "FallacyError") }) it("must set name from object when subclassed", function() { var err = new ChildError("Problem", {name: "FallacyError"}) err.must.have.own("name", "FallacyError") }) it("must set stack", function() { var err = new StandardError() err.must.have.own("stack") var stack = err.stack.split(/\n\s*/) stack[0].must.equal("StandardError") stack[1].must.include("index_test.js") }) it("must set stack when subclassed", function() { var err = new ChildError() err.must.have.own("stack") var stack = err.stack.split(/\n\s*/) stack[0].must.equal("ChildError") stack[1].must.include("index_test.js") stack[2].must.not.include("index_test.js") }) it("must set stack given name from object", function() { var err = new StandardError({name: "FallacyError"}) err.must.have.own("stack") err.stack.split(/\n\s*/)[0].must.equal("FallacyError") }) it("must set stack from object", function() { new ChildError({stack: "OMG"}).must.have.own("stack", "OMG") }) it("must set stack from object even if empty", function() { new ChildError({stack: ""}).must.have.own("stack", "") }) }) describe(".prototype.toString", function() { it("must return message", function() { var err = new StandardError("Problem") err.toString().must.equal("StandardError: Problem") }) it("must return set name", function() { var err = new StandardError("Problem") err.name = "OtherError" err.toString().must.equal("OtherError: Problem") }) }) describe("JSON.stringify", function() { it("must serialize enumerable properties", function() { var err = JSON.stringify(new StandardError("Problem", {code: 404})) var obj = {name: "StandardError", message: "Problem", code: 404} JSON.parse(err).must.eql(obj) }) }) }) js-standard-error-1.1.0/test/mocha.opts000066400000000000000000000000511234741333200200250ustar00rootroot00000000000000--recursive --check-leaks --require must