Permalink
Browse files

Add a whole bunch more bytes to spell out "error".

  • Loading branch information...
brianloveswords committed Sep 19, 2012
1 parent 8e53b34 commit 88a0d6dd5c35bdb404db2c80fee7d123d50c872e
Showing with 48 additions and 40 deletions.
  1. +39 −40 lib/bakery.js
  2. +9 −0 test/bakery.test.js
View
@@ -56,9 +56,9 @@ exports.getBakedData = function getBakedData(img, callback) {
function endListener() {
if (!found) {
- var err = new Error('Image does not have any baked in data.')
- err.code = IMAGE_UNBAKED;
- return callback(err);
+ var error = new Error('Image does not have any baked in data.');
+ error.code = 'IMAGE_UNBAKED';
+ return callback(error);
}
}
@@ -68,78 +68,77 @@ exports.getBakedData = function getBakedData(img, callback) {
exports.debake = function debake(image, callback) {
- exports.getBakedData(image, function (err, url) {
- if (err)
- return callback(err);
-
- request(url, function (err, response, body) {
- if (err) {
- var err = errors.request(err, url);
- return callback(err);
+ exports.getBakedData(image, function (error, url) {
+ if (error)
+ return callback(error);
+
+ request(url, function (error, response, body) {
+ if (error) {
+ error = errors.request(error, url);
+ return callback(error);
}
var status = response.statusCode;
- var type = response.headers['content-type']
+ var type = response.headers['content-type'];
if (status == 200 && type == 'application/json')
return exports.parseResponse(body, url, callback);
if (status != 200) {
- var err = (errors[status] || errors.generic)(status, url);
- return callback(err);
+ error = (errors[status] || errors.generic)(status, url);
+ return callback(error);
}
});
});
};
exports.parseResponse = function parseResponse(body, url, callback) {
+ var error, obj;
if (typeof body !== 'string')
return callback(null, body);
- var obj;
try { obj = JSON.parse(body) }
- catch (err) {
- var err = errors.jsonParse(err, url);
- return callback(err)
+ catch (original) {
+ error = errors.jsonParse(original, url);
+ return callback(error);
}
return callback(null, obj);
};
var errors = {
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;
+ var error = new Error(msg);
+ error.code = 'REQUEST_ERROR';
+ error.original = original;
+ error.url = url;
+ return error;
},
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 = url
- err.httpStatusCode = status;
- return err;
+ var error = new Error(msg);
+ error.code = 'RESOURCE_ERROR';
+ error.url = url;
+ error.httpStatusCode = status;
+ return error;
},
jsonParse: function (original, url) {
- var err = new Error('Could not parse JSON at endpoint');
- err.code = 'JSON_PARSE_ERROR';
- err.url = url
- err.original = original;
- return err;
+ var error = new Error('Could not parse JSON at endpoint');
+ error.code = 'JSON_PARSE_ERROR';
+ error.url = url;
+ error.original = original;
+ return error;
},
404: function (response, url) {
- var err = new Error('Could not get resource (404)');
- err.code = 'RESOURCE_NOT_FOUND';
- err.url = url
- err.httpStatusCode = 404;
- return err;
+ var error = new Error('Could not get resource (404)');
+ error.code = 'RESOURCE_NOT_FOUND';
+ error.url = url;
+ error.httpStatusCode = 404;
+ return error;
}
-}
-function error404() { }
+};
exports.createChunk = createChunk;
exports.KEYWORD = KEYWORD;
View
@@ -170,3 +170,12 @@ test('bakery.debake: error when DNS fails', function (t) {
});
});
});
+
+test('bakery.debake: error when debaking unbaked image', function (t) {
+ var image = getImageStream();
+ bakery.debake(image, function (err, contents) {
+ t.ok(err, 'should have an error');
+ t.same(err.code, 'IMAGE_UNBAKED', 'should get unbaked image error');
+ t.end();
+ });
+});

0 comments on commit 88a0d6d

Please sign in to comment.