Skip to content

Commit 5bf308c

Browse files
committed
Add index.js
1 parent 01c3ae8 commit 5bf308c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
3+
beautify: function(options) {
4+
5+
var options = options || {};
6+
7+
return function(err, req, res, next) {
8+
//Catch JSON error
9+
if (err instanceof SyntaxError &&
10+
err.status >= 400 && err.status < 500 &&
11+
err.message.indexOf('JSON')) {
12+
options.status ? res.status(options.status) : res.status(400);
13+
options.res ? res.json(options.res) : res.json({
14+
msg: 'Invalid JSON'
15+
});
16+
}
17+
};
18+
}
19+
}

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "bodyparser-json-error",
3+
"version": "0.0.1",
4+
"description": "Beautify Express body-parser JSON syntax error",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Igor-Lopes/bodyparser-json-error.git"
12+
},
13+
"author": "Igor Lopes",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/Igor-Lopes/bodyparser-json-error/issues"
17+
},
18+
"homepage": "https://github.com/Igor-Lopes/bodyparser-json-error#readme"
19+
}

0 commit comments

Comments
 (0)