Comparing version 6.2.0 to 6.4.0
{ | ||
"name": "error", | ||
"version": "6.2.0", | ||
"version": "6.4.0", | ||
"description": "Custom errors", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -18,3 +18,6 @@ 'use strict'; | ||
assert.equal(ServerError.type, 'server.5xx.error'); | ||
assert.equal(error.type, 'server.5xx.error'); | ||
assert.equal(error.fullType, 'server.5xx.error'); | ||
assert.equal(error.statusCode, 500); | ||
@@ -38,2 +41,3 @@ assert.equal(error.message, 'some title server error, status=500'); | ||
assert.equal(error2.type, 'client.4xx.error'); | ||
assert.equal(error2.fullType, 'client.4xx.error'); | ||
assert.equal(error2.statusCode, 404); | ||
@@ -40,0 +44,0 @@ assert.equal(error2.message, 'some title client error, status=404'); |
@@ -24,2 +24,4 @@ 'use strict'; | ||
assert.equal(ServerListenError.type, 'server.listen-failed'); | ||
assert.equal(err2.message, 'server: listen EADDRINUSE'); | ||
@@ -72,2 +74,52 @@ assert.equal(err2.requestedPort, 3426); | ||
test('wrapping twice', function t(assert) { | ||
var ReadError = WrappedError({ | ||
type: 'my.read-error', | ||
message: 'read: {causeMessage}' | ||
}); | ||
var DatabaseError = WrappedError({ | ||
type: 'my.database-error', | ||
message: 'db: {causeMessage}' | ||
}); | ||
var BusinessError = WrappedError({ | ||
type: 'my.business-error', | ||
message: 'business: {causeMessage}' | ||
}); | ||
var err = BusinessError( | ||
DatabaseError( | ||
ReadError( | ||
new Error('oops') | ||
) | ||
) | ||
); | ||
assert.ok(err); | ||
assert.equal(err.message, 'business: db: read: oops'); | ||
assert.equal(err.type, 'my.business-error'); | ||
assert.equal(err.fullType, 'my.business-error~!~' + | ||
'my.database-error~!~' + | ||
'my.read-error~!~' + | ||
'error.wrapped-unknown'); | ||
assert.end(); | ||
}); | ||
test('handles bad recursive strings', function t(assert) { | ||
var ReadError = WrappedError({ | ||
type: 'wat.wat', | ||
message: 'read: {causeMessage}' | ||
}); | ||
var err2 = ReadError(new Error('hi {causeMessage}')); | ||
assert.ok(err2); | ||
assert.equal(err2.message, | ||
'read: hi $INVALID_CAUSE_MESSAGE_LITERAL'); | ||
assert.end(); | ||
}); | ||
test('can wrap real IO errors', function t(assert) { | ||
@@ -74,0 +126,0 @@ var ServerListenError = WrappedError({ |
21
typed.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
var extend = require('xtend/mutable'); | ||
var assert = require('assert'); | ||
@@ -12,11 +13,10 @@ var isWordBoundary = /[_.-](\w|$)/g; | ||
function TypedError(args) { | ||
if (!args) { | ||
throw new Error('args is required'); | ||
} | ||
if (!args.type) { | ||
throw new Error('args.type is required'); | ||
} | ||
assert(args, 'TypedError: must specify options'); | ||
assert(args.type, 'TypedError: must specify options.type'); | ||
assert(args.message, 'TypedError: must specify options.message'); | ||
assert(!has(args, 'fullType'), | ||
'TypedError: fullType field is reserved'); | ||
var message = args.message; | ||
if (args.type && !args.name) { | ||
@@ -42,2 +42,5 @@ var errorName = camelCase(args.type) + 'Error'; | ||
if (!opts.fullType) { | ||
opts.fullType = args.type; | ||
} | ||
var options = extend({}, args, opts); | ||
@@ -63,1 +66,5 @@ | ||
} | ||
function has(obj, key) { | ||
return Object.prototype.hasOwnProperty.call(obj, key); | ||
} |
'use strict'; | ||
var extend = require('xtend'); | ||
var extend = require('xtend/mutable'); | ||
var assert = require('assert'); | ||
@@ -10,2 +10,4 @@ | ||
var ERROR_TYPE = '[object Error]'; | ||
var causeMessageRegex = /\{causeMessage\}/g; | ||
var origMessageRegex = /\{origMessage\}/g; | ||
@@ -29,2 +31,4 @@ module.exports = WrappedError; | ||
var createTypedError = TypedError(options); | ||
extend(createError, options); | ||
createError._name = options.name; | ||
@@ -39,6 +43,21 @@ return createError; | ||
var causeMessage = cause.message; | ||
if (causeMessage.indexOf('{causeMessage}') >= 0) { | ||
// recover | ||
causeMessage = causeMessage.replace( | ||
causeMessageRegex, | ||
'$INVALID_CAUSE_MESSAGE_LITERAL' | ||
); | ||
} | ||
if (causeMessage.indexOf('{origMessage}') >= 0) { | ||
causeMessage = causeMessage.replace( | ||
origMessageRegex, | ||
'$INVALID_ORIG_MESSAGE_LITERAL' | ||
); | ||
} | ||
var nodeCause = false; | ||
var errOptions = extend(opts, { | ||
causeMessage: cause.message, | ||
origMessage: cause.message | ||
var errOptions = extend({}, opts, { | ||
causeMessage: causeMessage, | ||
origMessage: causeMessage | ||
}); | ||
@@ -70,3 +89,3 @@ | ||
errOptions.fullType = options.type + '~!~' + | ||
(cause.type || causeType); | ||
(cause.fullType || cause.type || causeType); | ||
@@ -73,0 +92,0 @@ var err = createTypedError(errOptions); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16890
362
13