Skip to content

Commit

Permalink
Merge pull request #41 from unDemian/master
Browse files Browse the repository at this point in the history
Add onError handler
  • Loading branch information
dlau authored Oct 6, 2016
2 parents 842c843 + 97ba3e8 commit 51e1c98
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ console.log('curl -i http://localhost:3131/users -d "name=test"');
- `text` **{Boolean}** Parse text bodies, default `true`
- `json` **{Boolean}** Parse json bodies, default `true`
- `formidable` **{Object}** Options to pass to the formidable multipart parser
- `onError` **{Function}** Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw
- `strict` **{Boolean}** If enabled, don't parse GET, HEAD, DELETE requests, default `true`

## A note about strict mode
Expand Down
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = requestbody;
*/
function requestbody(opts) {
opts = opts || {};
opts.onError = 'onError' in opts ? opts.onError : false;
opts.patchNode = 'patchNode' in opts ? opts.patchNode : false;
opts.patchKoa = 'patchKoa' in opts ? opts.patchKoa : true;
opts.multipart = 'multipart' in opts ? opts.multipart : false;
Expand All @@ -48,17 +49,26 @@ function requestbody(opts) {
var body = {};
// so don't parse the body in strict mode
if (!opts.strict || ["GET", "HEAD", "DELETE"].indexOf(this.method.toUpperCase()) === -1) {
if (opts.json && this.is('json')) {
body = yield buddy.json(this, {encoding: opts.encoding, limit: opts.jsonLimit});
}
else if (opts.urlencoded && this.is('urlencoded')) {
body = yield buddy.form(this, {encoding: opts.encoding, limit: opts.formLimit});
}
else if (opts.text && this.is('text')) {
body = yield buddy.text(this, {encoding: opts.encoding, limit: opts.textLimit});
}
else if (opts.multipart && this.is('multipart')) {
body = yield formy(this, opts.formidable);
try {
if (opts.json && this.is('json')) {
body = yield buddy.json(this, {encoding: opts.encoding, limit: opts.jsonLimit});
}
else if (opts.urlencoded && this.is('urlencoded')) {
body = yield buddy.form(this, {encoding: opts.encoding, limit: opts.formLimit});
}
else if (opts.text && this.is('text')) {
body = yield buddy.text(this, {encoding: opts.encoding, limit: opts.textLimit});
}
else if (opts.multipart && this.is('multipart')) {
body = yield formy(this, opts.formidable);
}

} catch(parsingError) {
if (typeof(opts.onError) === 'function') {
opts.onError(parsingError, this);
} else {
throw parsingError;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koa-body",
"version": "1.5.0",
"version": "1.6.0",
"description": "A koa body parser middleware. Support multipart, urlencoded and json request bodies.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 51e1c98

Please sign in to comment.