Permalink
Browse files

Just pass necessary information instead of whole response object.

  • Loading branch information...
1 parent f996375 commit 8e53b34458b455594709a0648cb2bacb9a8296d7 @brianloveswords brianloveswords committed Sep 19, 2012
Showing with 33 additions and 12 deletions.
  1. +22 −12 lib/bakery.js
  2. +11 −0 test/bakery.test.js
View
@@ -73,58 +73,68 @@ exports.debake = function debake(image, callback) {
return callback(err);
request(url, function (err, response, body) {
- if (err)
+ if (err) {
+ var err = errors.request(err, url);
return callback(err);
+ }
var status = response.statusCode;
var type = response.headers['content-type']
if (status == 200 && type == 'application/json')
- return exports.parseResponse(response, body, callback);
+ return exports.parseResponse(body, url, callback);
if (status != 200) {
- var err = (errors[status] || errors.generic)(response);
+ var err = (errors[status] || errors.generic)(status, url);
return callback(err);
}
});
});
};
-exports.parseResponse = function parseResponse(response, body, callback) {
+exports.parseResponse = function parseResponse(body, url, callback) {
if (typeof body !== 'string')
return callback(null, body);
var obj;
try { obj = JSON.parse(body) }
catch (err) {
- var err = errors.jsonParse(response, err);
+ var err = errors.jsonParse(err, url);
return callback(err)
}
return callback(null, obj);
};
var errors = {
- generic: function (response) {
- var status = response.statusCode;
+ request: function (original, url) {
+ var msg = util.format('There was an error initiating the request: %s', original.message);
+ var err = new Error(msg);
+ err.code = 'REQUEST_ERROR';
+ err.original = original;
+ err.url = url;
+ return err;
+ },
+
+ generic: function (status, url) {
var msg = util.format('There was a problem retrieving the remote resource (%s)', status);
var err = new Error(msg);
err.code = 'RESOURCE_ERROR';
- err.url = urlutil.format(response.request.uri);
+ err.url = url
err.httpStatusCode = status;
return err;
},
- jsonParse: function (response, original) {
+ jsonParse: function (original, url) {
var err = new Error('Could not parse JSON at endpoint');
err.code = 'JSON_PARSE_ERROR';
- err.url = urlutil.format(response.request.uri);
+ err.url = url
err.original = original;
return err;
},
- 404: function (response) {
+ 404: function (response, url) {
var err = new Error('Could not get resource (404)');
err.code = 'RESOURCE_NOT_FOUND';
- err.url = urlutil.format(response.request.uri);
+ err.url = url
err.httpStatusCode = 404;
return err;
}
View
@@ -159,3 +159,14 @@ test('bakery.debake: 500 should return generic error', function (t) {
});
});
});
+
+test('bakery.debake: error when DNS fails', function (t) {
+ var opts = { image: getImageStream(), url: 'http://this.does.not.exist.bogus'};
+ bakery.bake(opts, function (err, baked) {
+ bakery.debake(baked, function (err, contents) {
+ t.ok(err, 'should have an error');
+ t.same(err.code, 'REQUEST_ERROR', 'should be a request error');
+ t.end();
+ });
+ });
+});

0 comments on commit 8e53b34

Please sign in to comment.