Browse Source

various networking.js improvements

- cleaned up some messy if/else code, replaced with nicely readable switch/case
- catch JSON.parse errors
jomo 9 years ago
parent
commit
3a61e15abf
2 changed files with 41 additions and 20 deletions
  1. 40 19
      lib/networking.js
  2. 1 1
      test/test.js

+ 40 - 19
lib/networking.js

@@ -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)) {
-      // response received successfully
-      callback(body, response, null);
-    } else if (error) {
-      callback(body || null, response, error);
-    } else if (code === 404 || code === 204) {
-      // page does not exist
-      callback(null, response, null);
-    } else if (code === 429) {
-      // Too Many Requests exception - code 429
-      // cause error so the image will not be cached
-      callback(body || null, response, (error || "TooManyRequests"));
-    } else {
-      // Probably 500 or the likes
-      logging.error(rid, "Unexpected response:", code, body);
-      callback(body || null, response, error);
+
+    switch (code) {
+      case 200:
+      case 301:
+      case 302: // never seen, but mojang might use it in future
+      case 307: // never seen, but mojang might use it in future
+      case 308: // never seen, but mojang might use it in future
+        // these are okay
+        break;
+      case 404:
+      case 204:
+        // we don't want to cache this
+        body = null;
+        break;
+      case 429:
+        // this shouldn't usually happen, but occasionally does
+        // 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;
+        }
+      }
     });
     });
   }
   }
 };
 };

+ 1 - 1
test/test.js

@@ -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();
               });
               });