Skip to content

Commit

Permalink
Modified all instances of parseResponse to now have error handling.
Browse files Browse the repository at this point in the history
- When an error occurs, instead of throwing ot the window object we now handle it appropriately.

#2506
  • Loading branch information
NTaylorMullen committed Nov 5, 2013
1 parent b5b44e3 commit e7677d3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 21 deletions.
25 changes: 19 additions & 6 deletions src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,15 @@
};

$(connection).triggerHandler(events.onStarting);

var url = connection.url + "/negotiate",
onFailed = function (error, connection) {
$(connection).triggerHandler(events.onError, [error.responseText]);
deferred.reject("SignalR: Error during negotiation request: " + error.responseText);
// Stop the connection if negotiate failed
connection.stop();
};

var url = connection.url + "/negotiate";
url = signalR.transports._logic.prepareQueryString(connection, url);

connection.log("Negotiating with '" + url + "'.");
Expand All @@ -545,19 +552,25 @@
error: function (error, statusText) {
// We don't want to cause any errors if we're aborting our own negotiate request.
if (statusText !== connection._.negotiateAbortText) {
$(connection).triggerHandler(events.onError, [error.responseText]);
deferred.reject("SignalR: Error during negotiation request: " + error.responseText);
// Stop the connection if negotiate failed
connection.stop();
onFailed(error, connection);
} else {
// This rejection will noop if the deferred has already been resolved or rejected.
deferred.reject("Stopped the connection while negotiating.");
}
},
success: function (result) {
var res = connection._parseResponse(result),
var res,
keepAliveData = connection._.keepAliveData;

try {
res = connection._parseResponse(result);
}
catch (error) {
onFailed(error, connection);
return;
}

keepAliveData = connection._.keepAliveData;
connection.appRelativeUrl = res.Url;
connection.id = res.ConnectionId;
connection.token = res.ConnectionToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,30 @@
data: {},
dataType: connection.ajaxDataType,
success: function (result) {
var data = connection._parseResponse(result);
var data;

try {
data = connection._parseResponse(result);
}
catch (error) {
deferral.reject("Failed to parse ping server response, stopping the connection: " + result);
return;
}

if (data.Response === "pong") {
deferral.resolve();
}
else {
deferral.reject("SignalR: Invalid ping response when pinging server: " + (data.responseText || data.statusText));
deferral.reject("SignalR: Invalid ping response when pinging server: " + data.Response);
}
},
error: function (data) {
if (data.status === 401 || data.status === 403) {
deferral.reject("Failed to ping server. Server responded with a " + data.status + " status code, stopping the connection.");
error: function (error) {
if (error.status === 401 || error.status === 403) {
deferral.reject("Failed to ping server. Server responded with a " + error.status + " status code, stopping the connection.");
connection.stop();
}
else {
deferral.reject("SignalR: Error pinging server: " + (data.responseText || data.statusText));
deferral.reject("SignalR: Error pinging server: " + (error.responseText || error.statusText));
}
}
}));
Expand Down Expand Up @@ -190,9 +198,13 @@
},

ajaxSend: function (connection, data) {
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token);
url = transportLogic.prepareQueryString(connection, url);
var url = connection.url + "/send" + "?transport=" + connection.transport.name + "&connectionToken=" + window.encodeURIComponent(connection.token),
onFail = function (error, connection) {
$(connection).triggerHandler(events.onError, [error]);
};

url = transportLogic.prepareQueryString(connection, url);

return $.ajax(
$.extend({}, $.signalR.ajaxDefaults, {
xhrFields: { withCredentials: connection.withCredentials },
Expand All @@ -204,18 +216,29 @@
data: data
},
success: function (result) {
var res;

if (result) {
$(connection).triggerHandler(events.onReceived, [connection._parseResponse(result)]);
try {
res = connection._parseResponse(result);
}
catch (error) {
onFail(error, connection);
return;
}

$(connection).triggerHandler(events.onReceived, [res]);
}
},
error: function (errData, textStatus) {
error: function (error, textStatus) {
if (textStatus === "abort" || textStatus === "parsererror") {
// The parsererror happens for sends that don't return any data, and hence
// don't write the jsonp callback to the response. This is harder to fix on the server
// so just hack around it on the client for now.
return;
}
$(connection).triggerHandler(events.onError, [errData, data]);

onFail(error, connection);
}
}));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@
dataType: connection.ajaxDataType,
contentType: connection.contentType,
success: function (result) {
var minData = connection._parseResponse(result),
delay = 0,
var delay = 0,
minData,
data,
shouldReconnect;

Expand All @@ -124,6 +124,13 @@
fireReconnected();
}

try {
minData = connection._parseResponse(result);
}
catch (error) {
$(connection).triggerHandler(events.onError, ["SignalR: failed at parsing response: " + result + ". With error: " + error.message]);
}

fireConnect();

if (minData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,22 @@
}, false);

connection.eventSource.addEventListener("message", function (e) {
var res;

// process messages
if (e.data === "initialized") {
return;
}

transportLogic.processMessages(connection, connection._parseResponse(e.data));
try {
res = connection._parseResponse(e.data);
}
catch (error) {
$(connection).triggerHandler(events.onError, ["SignalR: failed at parsing response: " + e.data + ". With error: " + error.message]);
return;
}

transportLogic.processMessages(connection, res, onSuccess);
}, false);

connection.eventSource.addEventListener("error", function (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,17 @@
};

connection.socket.onmessage = function (event) {
var data = connection._parseResponse(event.data),
var data,
$connection = $(connection);

try {
data = connection._parseResponse(event.data);
}
catch (error) {
$connection.triggerHandler(events.onError, ["SignalR: failed at parsing response: " + event.data + ". With error: " + error.message]);
return;
}

if (onSuccess) {
onSuccess();
onSuccess = null;
Expand Down

0 comments on commit e7677d3

Please sign in to comment.