Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

js: better error handling for JSONP requests

Use the timeout hack to catch JSONP errors.

Signed-off-by: Pierre-Alexandre Meyer <pierre@ning.com>
  • Loading branch information...
commit 97ba559f4986cb621875802ca2eea85c4e72aebc 1 parent 481a61e
Pierre-Alexandre Meyer pierre authored

Showing 2 changed files with 21 additions and 3 deletions. Show diff stats Hide diff stats

  1. +3 1 js/api.js
  2. +18 2 js/ui.js
4 js/api.js
@@ -34,7 +34,9 @@ function getDataAndCreateOrUpdateGraph(uri, callback, opts) {
34 34 dataType: "jsonp",
35 35 cache : false,
36 36 jsonp : "callback",
37   - jsonpCallback: callback
  37 + jsonpCallback: callback,
  38 + // Hacky error handling for JSONP requests
  39 + timeout : 7000
38 40 }
39 41
40 42 if (!(opts === undefined)) {
20 js/ui.js
@@ -39,9 +39,25 @@ function initializeUI() {
39 39 // Setup the error messages alert for Ajax calls
40 40 $('#errorDiv')
41 41 .hide() // hide it initially
42   - .ajaxError(function(event, request, settings) {
  42 + .ajaxError(function(event, jqXHR, settings) {var message;
  43 + if (jqXHR.status === 0) {
  44 + message = 'Unable to connect to the remote host.';
  45 + } else if (jqXHR.status == 404) {
  46 + message = 'Requested resource not found [404].';
  47 + } else if (jqXHR.status == 500) {
  48 + message = 'Internal Server Error [500].';
  49 + } else if (exception === 'parsererror') {
  50 + message = 'Unable to parse the JSON response.';
  51 + } else if (exception === 'timeout') {
  52 + message = 'Connection timeout.';
  53 + } else if (exception === 'abort') {
  54 + message = 'Ajax request aborted.';
  55 + } else {
  56 + message = 'Uncaught Error. ' + jqXHR.responseText;
  57 + }
  58 +
43 59 $(this).show();
44   - $(this).append("<p>Error requesting " + settings.url + ", error is:" + request.responseText + "<p>");
  60 + $(this).append("<p>Error requesting " + settings.url + ". " + message + "<p>");
45 61 event.preventDefault();
46 62 });
47 63 }

0 comments on commit 97ba559

Please sign in to comment.
Something went wrong with that request. Please try again.