typed-error
Advanced tools
Comparing version 0.1.0 to 1.0.0
{ | ||
"name": "typed-error", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "A class designed to enable easily extending the built-in javascript Error, allowing typed errors.", | ||
"main": "src/typed-error.js", | ||
"types": "src/typed-error.ts", | ||
"scripts": { | ||
"test": "node node_modules/coffee-script/bin/coffee -c . && node node_modules/mocha/bin/mocha" | ||
"prepublish": "require-npm4-to-publish", | ||
"prepublishOnly": "npm test", | ||
"pretest": "tsc", | ||
"test": "mocha --compilers ts:ts-node/register && mocha --compilers coffee:coffee-script/register" | ||
}, | ||
@@ -14,7 +18,13 @@ "repository": { | ||
"devDependencies": { | ||
"bluebird": "~2.2.2", | ||
"@types/bluebird": "^3.0.36", | ||
"@types/mocha": "^2.2.33", | ||
"@types/node": "^6.0.48", | ||
"bluebird": "^3.4.6", | ||
"chai": "~1.9.0", | ||
"chai-as-promised": "~4.1.1", | ||
"coffee-script": "~1.7.1", | ||
"mocha": "~1.21.3" | ||
"coffee-script": "~1.12.0", | ||
"mocha": "~1.21.3", | ||
"require-npm4-to-publish": "^1.0.0", | ||
"ts-node": "^1.7.2", | ||
"typescript": "^2.1.4" | ||
}, | ||
@@ -21,0 +31,0 @@ "author": "", |
This module allows you to easily extend the built-in Error type for typed error checking | ||
```coffee-script | ||
For typescript: | ||
```typescript | ||
import TypedError = require('typed-error') | ||
class MyError extends TypedError {} | ||
try { | ||
throw new MyError() | ||
} catch(e) { | ||
console.log(e instanceof MyError) // true | ||
console.log(e.name) // 'MyError' | ||
console.log(e.constructor.name) // 'MyError' | ||
console.log(e.stack) // <stack trace> | ||
if(e instanceof MyError) { | ||
console.log('Do custom handling') | ||
} else { | ||
console.log('Another type of error') | ||
} | ||
// Or | ||
switch(e.name) { | ||
case 'MyError': | ||
console.log('Do custom handling') | ||
break; | ||
default: | ||
console.log('Another type of error') | ||
} | ||
// Or | ||
switch(e.constructor.name) { | ||
case 'MyError': | ||
console.log('Do custom handling') | ||
break; | ||
default: | ||
console.log('Another type of error') | ||
} | ||
} | ||
``` | ||
And with bluebird: | ||
```typescript | ||
import TypedError = require('../src/typed-error') | ||
import * as Promise from 'bluebird' | ||
class MyError extends TypedError {} | ||
Promise.try(() => { | ||
throw new MyError() | ||
}) | ||
.catch(MyError, (e) => { | ||
console.log('Do custom handling') | ||
}) | ||
.catch(() => { | ||
console.log('Another type of error') | ||
}) | ||
// Or | ||
const MyErrorName = (e: Error) => e.name === 'MyError' | ||
Promise.try(() => { | ||
throw new MyError() | ||
}) | ||
.catch(MyErrorName, (e) => { | ||
console.log('Do custom handling') | ||
}) | ||
.catch(() => { | ||
console.log('Another type of error') | ||
}) | ||
// Or | ||
const MyErrorConstructorName = (e: Error) => e.constructor.name === 'MyError' | ||
Promise.try(() => { | ||
throw new MyError() | ||
}) | ||
.catch(MyErrorConstructorName, (e) => { | ||
console.log('Do custom handling') | ||
}) | ||
.catch(() => { | ||
console.log('Another type of error') | ||
}) | ||
``` | ||
For coffeescript: | ||
```coffeescript | ||
TypedError = require 'typed-error' | ||
@@ -37,3 +120,3 @@ | ||
And with bluebird: | ||
```coffee-script | ||
```coffeescript | ||
Promise = require 'bluebird' | ||
@@ -59,3 +142,2 @@ TypedError = require 'typed-error' | ||
console.log('Another type of error') | ||
``` | ||
@@ -62,0 +144,0 @@ # Or |
@@ -1,44 +0,64 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
var __hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
(function(root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
return define(function() { | ||
return (root.TypedError = factory()); | ||
}); | ||
} else if (typeof exports === 'object') { | ||
return module.exports = factory(); | ||
} else { | ||
return root.TypedError = factory(); | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
})(this, function() { | ||
var TypedError; | ||
TypedError = (function(_super) { | ||
__extends(TypedError, _super); | ||
function TypedError(message) { | ||
var err; | ||
if (message instanceof Error) { | ||
err = message; | ||
} else { | ||
err = new Error(message); | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
var BaseError = (function () { | ||
function BaseError() { | ||
Error.apply(this, arguments); | ||
} | ||
err.name = this.constructor.name; | ||
this.name = err.name; | ||
this.message = err.message; | ||
if (Error.captureStackTrace != null) { | ||
Error.captureStackTrace(this, this.constructor); | ||
} else if (err.stack != null) { | ||
this.stack = err.stack; | ||
return BaseError; | ||
}()); | ||
BaseError.prototype = Object.create(Error.prototype); | ||
var getStackTrace; | ||
if (Error.captureStackTrace != null) { | ||
var captureStackTrace_1 = Error.captureStackTrace; | ||
getStackTrace = function (e) { | ||
captureStackTrace_1(e, e.constructor); | ||
}; | ||
} | ||
else { | ||
getStackTrace = function (e, err) { | ||
if (!(err instanceof Error)) { | ||
err = new Error(err); | ||
} | ||
if (err.stack != null) { | ||
e.stack = err.stack; | ||
} | ||
}; | ||
} | ||
var TypedError = (function (_super) { | ||
__extends(TypedError, _super); | ||
function TypedError(err) { | ||
if (err === void 0) { err = ''; } | ||
var _this = _super.call(this) || this; | ||
if (err instanceof Error) { | ||
_this.message = err.message; | ||
} | ||
else { | ||
_this.message = err; | ||
} | ||
_this.name = _this.constructor.name; | ||
getStackTrace(_this, err); | ||
return _this; | ||
} | ||
} | ||
return TypedError; | ||
})(Error); | ||
return TypedError; | ||
}(BaseError)); | ||
return TypedError; | ||
}); | ||
}).call(this); | ||
}); | ||
//# sourceMappingURL=typed-error.js.map |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12494
13
193
0
150
11
1