Skip to content

Commit

Permalink
updated to support parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitkapoor committed Jul 31, 2011
1 parent f3340a0 commit 3e9bd13
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,29 @@ mixin(Request.prototype, {

response.on('end', function() {
if (self.options.parser) {
self.options.parser.call(response, body, function(parsedData) {

self._fireEvents(parsedData, response);
});
self.options.parser.call(response, body, function(error, parsedData) {
self._fireEvents(error, parsedData, response);
});
} else {
self._fireEvents(body, response);
self._fireEvents(null, body, response);
}
});
}
},
_respond: function(type, data, response) {
_respond: function(type, error, data, response) {
if (this.options.originalRequest) {
this.options.originalRequest.emit(type, data, response);
} else {
this.emit(type, data, response);
this.emit(type, error, data, response);
}
},
_fireEvents: function(body, response) {
if (parseInt(response.statusCode) >= 400) this._respond('error', body, response);
else this._respond('success', body, response);
_fireEvents: function(error, body, response) {
if (parseInt(response.statusCode) >= 400) this._respond('error', error, body, response);
else this._respond('success', error, body, response);

this._respond(response.statusCode.toString().replace(/\d{2}$/, 'XX'), body, response);
this._respond(response.statusCode.toString(), body, response);
this._respond('complete', body, response);
this._respond(response.statusCode.toString().replace(/\d{2}$/, 'XX'), error, body, response);
this._respond(response.statusCode.toString(), error, body, response);
this._respond('complete', error, body, response);
},
_makeRequest: function() {
var self = this;
Expand Down Expand Up @@ -205,7 +204,11 @@ var parsers = {
callback(data);
},
json: function(data, callback) {
callback(data && JSON.parse(data));
try {
return callback(null, data && JSON.parse(data));
} catch(error){
callback(error, data);
}
}
}

Expand Down

0 comments on commit 3e9bd13

Please sign in to comment.