better error handling

This commit is contained in:
jomo 2014-10-30 00:21:16 +01:00
parent a07ca210d6
commit d84d55f533
4 changed files with 60 additions and 34 deletions

View File

@ -36,29 +36,38 @@ exp.uuid_valid = function(uuid) {
// error, status, image buffer // error, status, image buffer
// //
// the status gives information about how the image was received // the status gives information about how the image was received
// -1: profile requested, but it was not found // -1: error
// 1: found on disk // 1: found on disk
// 2: profile requested/found, skin downloaded from mojang servers // 2: profile requested/found, skin downloaded from mojang servers
// 3: profile requested/found, but it has no skin // 3: profile requested/found, but it has no skin
exp.get_avatar = function(uuid, size, callback) { exp.get_avatar = function(uuid, size, callback) {
var filepath = config.skins_dir + uuid + ".png"; var filepath = config.skins_dir + uuid + ".png";
if (fs.existsSync(filepath)) { if (fs.existsSync(filepath)) {
skins.resize_img(filepath, size, function(result) { skins.resize_img(filepath, size, function(err, result) {
callback(null, 1, result); callback(err, 1, result);
}); });
} else { } else {
networking.get_profile(uuid, function(err, profile) { networking.get_profile(uuid, function(err, profile) {
if (err) { if (err) {
callback(err, -1, profile); callback(err, -1, profile);
return;
} }
var skinurl = exp.skin_url(profile); var skinurl = exp.skin_url(profile);
if (skinurl) { if (skinurl) {
networking.skin_file(skinurl, filepath, function() { networking.skin_file(skinurl, filepath, function(err) {
if (err) {
callback(err, -1, null);
} else {
console.log('got skin'); console.log('got skin');
skins.resize_img(filepath, size, function(result) { skins.resize_img(filepath, size, function(err, result) {
if (err) {
callback(err, -1, null);
} else {
callback(null, 2, result); callback(null, 2, result);
}
}); });
}
}); });
} else { } else {
// profile found, but has no skin // profile found, but has no skin

View File

@ -40,12 +40,12 @@ exp.skin_file = function(url, outname, callback) {
timeout: config.http_timeout // ms timeout: config.http_timeout // ms
}, function (error, response, body) { }, function (error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
skins.extract_face(body, outname, function() { skins.extract_face(body, outname, function(err) {
callback(); callback(err);
}); });
} else { } else {
if (error) { if (error) {
console.error(error); console.error("Error downloading '" + url + "': " + error);
} else if (response.statusCode == 404) { } else if (response.statusCode == 404) {
console.warn("Texture not found: " + url); console.warn("Texture not found: " + url);
} else if (response.statusCode == 429) { } else if (response.statusCode == 429) {

View File

@ -4,15 +4,22 @@ var exp = {};
// extracts the face from an image +buffer+ // extracts the face from an image +buffer+
// save it to a file called +outname+ // save it to a file called +outname+
// callback has an error parameter which can be null
exp.extract_face = function(buffer, outname, callback) { exp.extract_face = function(buffer, outname, callback) {
lwip.open(buffer, "png", function(err, image) { lwip.open(buffer, "png", function(err, image) {
if (err) throw err; if (err) {
callback(err);
} else {
image.batch() image.batch()
.crop(8, 8, 15, 15) .crop(8, 8, 15, 15)
.writeFile(outname, function(err) { .writeFile(outname, function(err) {
if (err) throw err; if (err) {
callback(); callback(err);
} else {
callback(null);
}
}); });
}
}); });
}; };
@ -20,12 +27,15 @@ exp.extract_face = function(buffer, outname, callback) {
// +callback+ is a buffer of the resized image // +callback+ is a buffer of the resized image
exp.resize_img = function(inname, size, callback) { exp.resize_img = function(inname, size, callback) {
lwip.open(inname, function(err, image) { lwip.open(inname, function(err, image) {
if (err) throw err; if (err) {
callback(err, null);
} else {
image.batch() image.batch()
.resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur .resize(size, size, "nearest-neighbor") // nearest-neighbor doesn't blur
.toBuffer('png', function(err, buffer) { .toBuffer('png', function(err, buffer) {
callback(buffer); callback(null, buffer);
}); });
}
}); });
}; };

View File

@ -23,9 +23,11 @@ router.get('/:uuid/:size?', function(req, res) {
return; return;
} }
try {
helpers.get_avatar(uuid, size, function(err, status, image) { helpers.get_avatar(uuid, size, function(err, status, image) {
if (err) { if (err) {
throw err; console.error(err);
handle_404(def);
} else if (status == 1 || status == 2) { } else if (status == 1 || status == 2) {
var time = new Date() - start; var time = new Date() - start;
sendimage(200, time, image); sendimage(200, time, image);
@ -33,6 +35,11 @@ router.get('/:uuid/:size?', function(req, res) {
handle_404(def); handle_404(def);
} }
}); });
} catch(e) {
console.error("Error!");
console.error(e);
res.status(500).send("500 Internal server error");
}
function handle_404(def) { function handle_404(def) {
if (def == "alex" || def == "steve") { if (def == "alex" || def == "steve") {