Bug 991654 - Updated client to use new server error format. r=Standard8

--HG--
extra : transplant_source : %E9Jpme%FB%C0%29Un%01J%10%EFd%00%19%BD%F4%E3
This commit is contained in:
Nicolas Perriault 2014-05-29 21:20:11 +01:00
parent e7abb90eb0
commit 5bc0bda475
2 changed files with 38 additions and 14 deletions

View File

@ -56,16 +56,28 @@ loop.shared.Client = (function($) {
*
* @param {Function} cb Callback(err)
* @param jqXHR See jQuery docs
* @param testStatus See jQuery docs
* @param textStatus See jQuery docs
* @param errorThrown See jQuery docs
*/
_failureHandler: function(cb, jqXHR, testStatus, errorThrown) {
var error = "Unknown error.";
if (jqXHR && jqXHR.responseJSON && jqXHR.responseJSON.error) {
error = jqXHR.responseJSON.error;
_failureHandler: function(cb, jqXHR, textStatus, errorThrown) {
var error = "Unknown error.",
jsonRes = jqXHR && jqXHR.responseJSON || {};
// Received error response format:
// { "status": "errors",
// "errors": [{
// "location": "url",
// "name": "token",
// "description": "invalid token"
// }]}
if (jsonRes.status === "errors" && Array.isArray(jsonRes.errors)) {
error = "Details: " + jsonRes.errors.map(function(err) {
return Object.keys(err).map(function(field) {
return field + ": " + err[field];
}).join(", ");
}).join("; ");
}
var message = "HTTP error " + jqXHR.status + ": " +
errorThrown + "; " + error;
var message = "HTTP " + jqXHR.status + " " + errorThrown +
"; " + error;
console.error(message);
cb(new Error(message));
},

View File

@ -9,7 +9,19 @@ var expect = chai.expect;
describe("loop.shared.Client", function() {
"use strict";
var sandbox, fakeXHR, requests = [], callback;
var sandbox,
fakeXHR,
requests = [],
callback;
var fakeErrorRes = JSON.stringify({
status: "errors",
errors: [{
location: "url",
name: "token",
description: "invalid token"
}]
});
beforeEach(function() {
sandbox = sinon.sandbox.create();
@ -68,9 +80,9 @@ describe("loop.shared.Client", function() {
expect(requests).to.have.length.of(1);
requests[0].respond(400, {"Content-Type": "application/json"},
'{"error": "my error"}');
fakeErrorRes);
sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
return /400.*my error/.test(err.message);
return /400.*invalid token/.test(err.message);
}));
});
@ -115,9 +127,9 @@ describe("loop.shared.Client", function() {
client.requestCallsInfo(42, callback);
requests[0].respond(400, {"Content-Type": "application/json"},
'{"error": "my error"}');
fakeErrorRes);
sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
return /400.*my error/.test(err.message);
return /400.*invalid token/.test(err.message);
}));
});
@ -172,9 +184,9 @@ describe("loop.shared.Client", function() {
client.requestCallInfo("fake", callback);
requests[0].respond(400, {"Content-Type": "application/json"},
'{"error": "my error"}');
fakeErrorRes);
sinon.assert.calledWithMatch(callback, sinon.match(function(err) {
return /400.*my error/.test(err.message);
return /400.*invalid token/.test(err.message);
}));
});