Comparing version 5.1.1 to 5.2.0
{ | ||
"name": "error", | ||
"version": "5.1.1", | ||
"version": "5.2.0", | ||
"description": "Custom errors", | ||
@@ -21,2 +21,3 @@ "keywords": [], | ||
"camelize": "^1.0.0", | ||
"is-error": "^2.0.0", | ||
"string-template": "~0.2.0", | ||
@@ -26,3 +27,4 @@ "xtend": "~4.0.0" | ||
"devDependencies": { | ||
"tape": "~3.0.0" | ||
"lint-trap": "^1.0.1", | ||
"tape": "^3.5.0" | ||
}, | ||
@@ -36,5 +38,4 @@ "licenses": [ | ||
"scripts": { | ||
"test": "node ./test.js", | ||
"start": "node ./index.js", | ||
"watch": "nodemon -w ./index.js index.js", | ||
"lint": "lint-trap .", | ||
"test": "npm run lint && node test/index.js", | ||
"travis-test": "istanbul cover ./test/index.js && ((cat coverage/lcov.info | coveralls) || exit 0)", | ||
@@ -41,0 +42,0 @@ "cover": "istanbul cover --report none --print detail ./test/index.js", |
@@ -15,23 +15,2 @@ # error | ||
## Example | ||
```js | ||
var ValidationError = require("error/validation") | ||
var OptionError = require("error/option") | ||
var error = ValidationError([{ | ||
message: "Please enter required field", | ||
attribute: "name" | ||
}, { | ||
message: "Password must be at least 10 characters", | ||
attribute: "password" | ||
}]) | ||
console.log("error.errors", error.errors) | ||
var error = OptionError("Something went wrong", metaData) | ||
console.log("error.option", error.option) | ||
``` | ||
## Typed Error | ||
@@ -43,20 +22,53 @@ | ||
var ServerError = TypedError({ | ||
type: 'server.5xx.error', | ||
message: '{title} server error, status={statusCode}' | ||
}) | ||
type: 'server.5xx', | ||
message: '{title} server error, status={statusCode}', | ||
title: null, | ||
statusCode: null | ||
}); | ||
var ClientError = TypedError({ | ||
type: 'client.4xx.error', | ||
message: '{title} client error, status={statusCode}' | ||
}) | ||
type: 'client.4xx', | ||
message: '{title} client error, status={statusCode}', | ||
title: null, | ||
statusCode: null | ||
}); | ||
var error = ServerError({ | ||
title:'some title', | ||
statusCode: 500 | ||
}) | ||
title:'some title', | ||
statusCode: 500 | ||
}); | ||
var error2 = ClientError({ | ||
title: 'some title', | ||
statusCode: 404 | ||
}) | ||
title: 'some title', | ||
statusCode: 404 | ||
}); | ||
``` | ||
## Wrapped Errors | ||
```js | ||
var net = require('net'); | ||
var WrappedError = require('error/wrapped'); | ||
var ServerListenError = WrappedError({ | ||
message: 'server: {origMessage}', | ||
type: 'server.listen-failed', | ||
requestedPort: null, | ||
host: null | ||
}); | ||
var server = net.createServer(); | ||
server.on('error', function onError(err) { | ||
if (err.code === 'EADDRINUSE') { | ||
throw ServerListenError(err, { | ||
requestedPort: 3000, | ||
host: null | ||
}); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
server.listen(3000); | ||
``` | ||
## Installation | ||
@@ -63,0 +75,0 @@ |
37
typed.js
@@ -1,20 +0,22 @@ | ||
var camelize = require("camelize") | ||
var template = require("string-template") | ||
var extend = require("xtend/mutable") | ||
'use strict'; | ||
module.exports = TypedError | ||
var camelize = require('camelize'); | ||
var template = require('string-template'); | ||
var extend = require('xtend/mutable'); | ||
module.exports = TypedError; | ||
function TypedError(args) { | ||
if (!args) { | ||
throw new Error("args is required"); | ||
throw new Error('args is required'); | ||
} | ||
if (!args.type) { | ||
throw new Error("args.type is required"); | ||
throw new Error('args.type is required'); | ||
} | ||
var message = args.message | ||
var message = args.message; | ||
if (args.type && !args.name) { | ||
var errorName = camelize(args.type) + "Error" | ||
args.name = errorName[0].toUpperCase() + errorName.substr(1) | ||
var errorName = camelize(args.type) + 'Error'; | ||
args.name = errorName[0].toUpperCase() + errorName.substr(1); | ||
} | ||
@@ -28,5 +30,5 @@ | ||
function createError(opts) { | ||
var result = new Error() | ||
var result = new Error(); | ||
Object.defineProperty(result, "type", { | ||
Object.defineProperty(result, 'type', { | ||
value: result.type, | ||
@@ -36,16 +38,15 @@ enumerable: true, | ||
configurable: true | ||
}) | ||
}); | ||
var options = extend({}, args, opts) | ||
var options = extend({}, args, opts); | ||
extend(result, options) | ||
extend(result, options); | ||
if (opts && opts.message) { | ||
result.message = template(opts.message, options) | ||
result.message = template(opts.message, options); | ||
} else if (message) { | ||
result.message = template(message, options) | ||
result.message = template(message, options); | ||
} | ||
return result | ||
return result; | ||
} | ||
} | ||
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
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
Network access
Supply chain riskThis module accesses the network.
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
12875
14
242
94
4
2
2
+ Addedis-error@^2.0.0
+ Addedis-error@2.2.2(transitive)