make-error-cause
Advanced tools
Comparing version 1.2.2 to 2.0.0
@@ -1,18 +0,7 @@ | ||
import makeError = require('make-error'); | ||
declare function makeErrorCause(value: string | Function): makeErrorCause.Constructor<makeErrorCause.BaseError>; | ||
declare function makeErrorCause<T extends Error>(value: string | Function, _super: { | ||
new (...args: any[]): T; | ||
}): makeErrorCause.Constructor<T>; | ||
declare namespace makeErrorCause { | ||
class BaseError extends makeError.BaseError { | ||
cause: Error; | ||
constructor(message: string, cause?: Error); | ||
toString(): string; | ||
} | ||
interface Constructor<T> { | ||
new (message: string, cause?: Error): T; | ||
super_: any; | ||
prototype: T; | ||
} | ||
import * as makeError from 'make-error'; | ||
export declare class BaseError extends makeError.BaseError { | ||
cause: Error | undefined; | ||
constructor(message: string, cause?: Error | undefined); | ||
inspect(): string | undefined; | ||
} | ||
export = makeErrorCause; | ||
export declare function fullStack(error: Error | BaseError): string | undefined; |
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var makeError = require('make-error'); | ||
function makeErrorCause(value, _super) { | ||
if (_super === void 0) { _super = makeErrorCause.BaseError; } | ||
return makeError(value, _super); | ||
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 __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var makeError = require("make-error"); | ||
var BaseError = /** @class */ (function (_super) { | ||
__extends(BaseError, _super); | ||
function BaseError(message, cause) { | ||
var _this = _super.call(this, message) || this; | ||
_this.cause = cause; | ||
return _this; | ||
} | ||
BaseError.prototype.inspect = function () { | ||
return fullStack(this); | ||
}; | ||
return BaseError; | ||
}(makeError.BaseError)); | ||
exports.BaseError = BaseError; | ||
function fullStack(error) { | ||
var err = error.cause; | ||
var fullStack = error.stack; | ||
while (err) { | ||
fullStack = err.stack + "\n\nDuring the above error, another error occurred:\n\n" + fullStack; | ||
err = err.cause; | ||
} | ||
return fullStack; | ||
} | ||
var makeErrorCause; | ||
(function (makeErrorCause) { | ||
var BaseError = (function (_super) { | ||
__extends(BaseError, _super); | ||
function BaseError(message, cause) { | ||
_super.call(this, message); | ||
this.cause = cause; | ||
} | ||
BaseError.prototype.toString = function () { | ||
return _super.prototype.toString.call(this) + (this.cause ? "\nCaused by: " + this.cause.toString() : ''); | ||
}; | ||
return BaseError; | ||
}(makeError.BaseError)); | ||
makeErrorCause.BaseError = BaseError; | ||
})(makeErrorCause || (makeErrorCause = {})); | ||
module.exports = makeErrorCause; | ||
exports.fullStack = fullStack; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
var test = require('blue-tape'); | ||
var makeErrorCause = require('./index'); | ||
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 __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var test = require("blue-tape"); | ||
var util_1 = require("util"); | ||
var index_1 = require("./index"); | ||
var SEP_TEXT = '\n\nDuring the above error, another error occurred:\n\n'; | ||
test('make error cause', function (t) { | ||
var TestError = makeErrorCause('TestError'); | ||
var SubTestError = makeErrorCause('SubTestError', TestError); | ||
var TestError = /** @class */ (function (_super) { | ||
__extends(TestError, _super); | ||
function TestError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return TestError; | ||
}(index_1.BaseError)); | ||
var SubTestError = /** @class */ (function (_super) { | ||
__extends(SubTestError, _super); | ||
function SubTestError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return SubTestError; | ||
}(TestError)); | ||
t.test('render the cause', function (t) { | ||
var cause = new Error('boom!'); | ||
var error = new TestError('something bad', cause); | ||
var again = new SubTestError('more bad', error); | ||
t.equal(error.cause, cause); | ||
t.equal(error.toString(), 'TestError: something bad\nCaused by: Error: boom!'); | ||
t.ok(error instanceof Error); | ||
t.ok(error instanceof makeErrorCause.BaseError); | ||
t.ok(error instanceof TestError); | ||
t.equal(again.cause, error); | ||
t.equal(again.toString(), 'SubTestError: more bad\nCaused by: TestError: something bad\nCaused by: Error: boom!'); | ||
t.ok(again instanceof Error); | ||
t.ok(again instanceof makeErrorCause.BaseError); | ||
t.ok(again instanceof TestError); | ||
t.ok(again instanceof SubTestError); | ||
var testError = new TestError('test boom!', cause); | ||
var subTestError = new SubTestError('sub test boom!', testError); | ||
t.equal(index_1.fullStack(cause), cause.stack); | ||
t.ok(cause instanceof Error); | ||
t.equal(testError.cause, cause); | ||
t.equal(index_1.fullStack(testError), "" + cause.stack + SEP_TEXT + testError.stack); | ||
t.equal(util_1.inspect(testError), index_1.fullStack(testError)); | ||
t.ok(testError instanceof Error); | ||
t.ok(testError instanceof index_1.BaseError); | ||
t.ok(testError instanceof TestError); | ||
t.equal(subTestError.cause, testError); | ||
t.equal(index_1.fullStack(subTestError), "" + cause.stack + SEP_TEXT + testError.stack + SEP_TEXT + subTestError.stack); | ||
t.equal(util_1.inspect(subTestError), index_1.fullStack(subTestError)); | ||
t.ok(subTestError instanceof Error); | ||
t.ok(subTestError instanceof index_1.BaseError); | ||
t.ok(subTestError instanceof TestError); | ||
t.ok(subTestError instanceof SubTestError); | ||
t.end(); | ||
@@ -23,0 +52,0 @@ }); |
{ | ||
"name": "make-error-cause", | ||
"version": "1.2.2", | ||
"version": "2.0.0", | ||
"description": "Make your own nested error types!", | ||
@@ -11,8 +11,8 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"lint": "tslint \"src/**/*.ts\"", | ||
"lint": "tslint \"src/**/*.ts\" --project tsconfig.json", | ||
"build": "rm -rf dist/ && tsc", | ||
"test-spec": "ts-node node_modules/blue-tape/bin/blue-tape.js \"src/**/*.spec.ts\" | tap-spec", | ||
"test-spec": "ts-node --type-check node_modules/blue-tape/bin/blue-tape.js \"src/**/*.spec.ts\" | tap-spec", | ||
"test-cov": "ts-node node_modules/istanbul/lib/cli.js cover -e .ts --print none -x \"*.d.ts\" -x \"*.spec.ts\" blue-tape -- \"src/**/*.spec.ts\" | tap-spec", | ||
"test": "npm run lint && npm run test-cov", | ||
"prepublish": "typings install && npm run build" | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -42,14 +42,15 @@ "repository": { | ||
"devDependencies": { | ||
"@types/blue-tape": "^0.1.31", | ||
"@types/node": "^9.6.1", | ||
"blue-tape": "^1.0.0", | ||
"istanbul": "1.0.0-alpha.2", | ||
"tap-spec": "^4.1.1", | ||
"ts-node": "^1.1.0", | ||
"tslint": "^3.10.2", | ||
"tslint-config-standard": "^1.0.0", | ||
"typescript": "^2.0.3", | ||
"typings": "^1.3.1" | ||
"ts-node": "^5.0.1", | ||
"tslint": "^5.9.1", | ||
"tslint-config-standard": "^7.0.0", | ||
"typescript": "^2.7.2" | ||
}, | ||
"dependencies": { | ||
"make-error": "^1.2.0" | ||
"make-error": "^1.3.4" | ||
} | ||
} |
@@ -8,15 +8,15 @@ # Make Error Cause | ||
> Make your own nested error types! | ||
> Make your own nested errors. | ||
## Features | ||
* Compatible with Node and browsers | ||
* Compatible with node.js and browsers | ||
* Works with `instanceof` | ||
* Use `error.stack` and `error.name` | ||
* Output full cause with `toString` | ||
* Extends [make-error](https://github.com/julien-f/js-make-error) | ||
* Output full stack trace with `fullStack(err)` | ||
* Automatic full stack traces with node.js (via [`inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)) | ||
* Extends [`make-error`](https://github.com/JsCommunity/make-error) | ||
## Installation | ||
```sh | ||
``` | ||
npm install make-error-cause --save | ||
@@ -27,13 +27,18 @@ ``` | ||
Usages from [`make-error`](https://github.com/julien-f/js-make-error#usage). The only difference is that errors accept a second argument known as the error "cause". The cause is used to wrap original errors with more intuitive feedback - for instance, wrapping a raw database error in a HTTP error. | ||
```js | ||
const CustomError = makeErrorCause('CustomError') | ||
import { BaseError, fullStack } from 'make-error-cause' | ||
const cause = new Error('boom!') | ||
const error = new CustomError('something bad', cause) | ||
class CustomError extends BaseError { | ||
constructor (message, cause) { | ||
super(message, cause) | ||
} | ||
} | ||
error.toString() //=> "CustomError: something bad\nCaused by: boom!" | ||
error.stack // Works! | ||
error.cause.stack // Handy! | ||
const error = new Error('Boom!') | ||
const customError = new CustomError('Another boom!', error) | ||
console.log(fullStack(error)) // Works with any error. | ||
console.log(fullStack(customError)) // Extended stack trace contains error causes. | ||
console.log(customError instanceof Error) //=> true | ||
``` | ||
@@ -45,2 +50,7 @@ | ||
Other references: | ||
* [Java](https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html) | ||
* [Python](https://www.python.org/dev/peps/pep-3134/) | ||
## License | ||
@@ -47,0 +57,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12554
97
65
9
Updatedmake-error@^1.3.4