Skip to content

Commit

Permalink
Catching unhandled errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-A committed Dec 22, 2019
1 parent 95b9567 commit 79960d1
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,23 @@ formidable.IncomingForm.prototype.parse = function(req, cb) {

// handling serverless cases
if (Buffer.isBuffer(req.rawBody)) { // firebase
// parse headers
this.writeHeaders(req.headers);
// parse body
this.write(req.rawBody);
try {
// parse headers
this.writeHeaders(req.headers);
// parse body
this.write(req.rawBody);
} catch (err) {
this._error(err);
}
} else if (Buffer.isBuffer(req.body)) { // body parser
// parse headers
this.writeHeaders(req.headers);
// parse body
this.write(req.body);
try {
// parse headers
this.writeHeaders(req.headers);
// parse body
this.write(req.body);
} catch (err) {
this._error(err);
}
} else {
// standard formidable parse prototype (not serverless)...

Expand Down Expand Up @@ -93,7 +101,11 @@ formidable.IncomingForm.prototype.parse = function(req, cb) {
};

// parse headers
this.writeHeaders(req.headers);
try {
this.writeHeaders(req.headers);
} catch (err) {
this._error(err);
}

// start listening for data...

Expand All @@ -103,7 +115,11 @@ formidable.IncomingForm.prototype.parse = function(req, cb) {
});

req.on('data', function (buffer) {
self.write(buffer);
try {
self.write(buffer);
} catch (err) {
self._error(err);
}
});
}
return this;
Expand Down

0 comments on commit 79960d1

Please sign in to comment.