Permalink
Browse files

Handle parse errors more gracefully.

  • Loading branch information...
1 parent 165aeab commit 92a3a46677cc23237506cf258bafd3753650b84d @brianloveswords brianloveswords committed Sep 19, 2012
Showing with 16 additions and 2 deletions.
  1. +2 −2 lib/bakery.js
  2. +14 −0 test/bakery.test.js
View
@@ -113,12 +113,12 @@ var errors = {
return err;
},
- jsonParse: function (response, err) {
- var original = err;
+ jsonParse: function (response, original) {
var err = new Error('Could not parse JSON at endpoint');
err.code = 'JSON_PARSE_ERROR';
err.url = urlutil.format(response.request.uri);
err.original = original;
+ return err;
},
404: function (response) {
View
@@ -106,6 +106,20 @@ test('bakery.debake: should work', function (t) {
});
});
+test('bakery.debake: should get a parse error', function (t) {
+ var opts = {
+ body: "{no json here}",
+ type: 'application/json'
+ };
+ broil(opts, function (baked) {
+ bakery.debake(baked, function (err, contents) {
+ t.ok(err, 'should have an error');
+ t.same(err.code, 'JSON_PARSE_ERROR', 'should be a json parse error');
+ t.end();
+ });
+ });
+});
+
test('bakery.debake: 404 should return error', function (t) {
var opts = { body: 'x', statusCode: 404 };
broil(opts, function (baked) {

0 comments on commit 92a3a46

Please sign in to comment.