mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
various networking.js improvements
- cleaned up some messy if/else code, replaced with nicely readable switch/case - catch JSON.parse errors
This commit is contained in:
parent
ccc7314ea0
commit
3a61e15abf
@ -69,7 +69,7 @@ exp.get_from_options = function(rid, url, options, callback) {
|
|||||||
},
|
},
|
||||||
timeout: config.server.http_timeout,
|
timeout: config.server.http_timeout,
|
||||||
followRedirect: false,
|
followRedirect: false,
|
||||||
encoding: (options.encoding || null),
|
encoding: options.encoding || null,
|
||||||
}, function(error, response, body) {
|
}, function(error, response, body) {
|
||||||
// log url + code + description
|
// log url + code + description
|
||||||
var code = response && response.statusCode;
|
var code = response && response.statusCode;
|
||||||
@ -80,24 +80,34 @@ exp.get_from_options = function(rid, url, options, callback) {
|
|||||||
logfunc(rid, url, code, http_code[code]);
|
logfunc(rid, url, code, http_code[code]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 200 or 301 depending on content type
|
|
||||||
if (!error && (code === 200 || code === 301)) {
|
switch (code) {
|
||||||
// response received successfully
|
case 200:
|
||||||
callback(body, response, null);
|
case 301:
|
||||||
} else if (error) {
|
case 302: // never seen, but mojang might use it in future
|
||||||
callback(body || null, response, error);
|
case 307: // never seen, but mojang might use it in future
|
||||||
} else if (code === 404 || code === 204) {
|
case 308: // never seen, but mojang might use it in future
|
||||||
// page does not exist
|
// these are okay
|
||||||
callback(null, response, null);
|
break;
|
||||||
} else if (code === 429) {
|
case 404:
|
||||||
// Too Many Requests exception - code 429
|
case 204:
|
||||||
// cause error so the image will not be cached
|
// we don't want to cache this
|
||||||
callback(body || null, response, (error || "TooManyRequests"));
|
body = null;
|
||||||
} else {
|
break;
|
||||||
// Probably 500 or the likes
|
case 429:
|
||||||
logging.error(rid, "Unexpected response:", code, body);
|
// this shouldn't usually happen, but occasionally does
|
||||||
callback(body || null, response, error);
|
// forcing error so it's not cached
|
||||||
|
error = error || "TooManyRequestsException";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (!error) {
|
||||||
|
// Probably 500 or the likes
|
||||||
|
logging.error(rid, "Unexpected response:", code, body);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callback(body, response, error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -144,7 +154,18 @@ exp.get_profile = function(rid, uuid, callback) {
|
|||||||
callback(null, null);
|
callback(null, null);
|
||||||
} else {
|
} else {
|
||||||
exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
|
exp.get_from_options(rid, session_url + uuid, { encoding: "utf8" }, function(body, response, err) {
|
||||||
callback(err || null, (body !== null ? JSON.parse(body) : null));
|
try {
|
||||||
|
body = body ? JSON.parse(body) : null;
|
||||||
|
callback(err || null, body);
|
||||||
|
} catch(e) {
|
||||||
|
if (e instanceof SyntaxError) {
|
||||||
|
logging.warn(rid, "Failed to parse JSON", e);
|
||||||
|
logging.debug(rid, body);
|
||||||
|
callback(err || null, null);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1013,7 +1013,7 @@ describe("Crafatar", function() {
|
|||||||
it("uuid should be rate limited", function(done) {
|
it("uuid should be rate limited", function(done) {
|
||||||
networking.get_profile(rid, id, function() {
|
networking.get_profile(rid, id, function() {
|
||||||
networking.get_profile(rid, id, function(err, profile) {
|
networking.get_profile(rid, id, function(err, profile) {
|
||||||
assert.strictEqual(err, "TooManyRequests");
|
assert.strictEqual(err, "TooManyRequestsException");
|
||||||
assert.strictEqual(profile.error, "TooManyRequestsException");
|
assert.strictEqual(profile.error, "TooManyRequestsException");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user