Ensure we always catch errors on Rest calls

This commit is contained in:
Joe Fiorini 2015-01-27 12:28:11 -05:00
parent 64484c4054
commit 50d9c11419
2 changed files with 27 additions and 15 deletions

View File

@ -20,7 +20,21 @@ function JobStatusGraphData(Rest, getBasePath, processErrors, $rootScope, $q) {
function getData(period, jobType) {
var url = getBasePath('dashboard')+'graphs/jobs/?period='+period+'&job_type='+jobType;
Rest.setUrl(url);
return pluck('data', Rest.get());
var result = Rest.get()
.catch(function(response) {
var errorMessage = 'Failed to get: ' + response.url + ' GET returned: ' + response.status;
processErrors(null,
response.data,
response.status,
null, {
hdr: 'Error!',
msg: errorMessage
});
return response;
});
return pluck('data', result);
}
return {
@ -33,17 +47,6 @@ function JobStatusGraphData(Rest, getBasePath, processErrors, $rootScope, $q) {
$broadcast('DataReceived:JobStatusGraph',
result);
return result;
}).catch(function(response) {
var errorMessage = 'Failed to get: ' + response.url + ' GET returned: ' + response.status;
ProcessErrors(null,
response.data,
response.status,
null, {
hdr: 'Error!',
msg: errorMessage
});
return response;
});
});
},

View File

@ -8,6 +8,8 @@ describe('Job Status Graph Data Service', function() {
$on: sinon.spy(),
};
var processErrors = sinon.spy();
var getBasePath = function(path) {
return '/' + path + '/';
}
@ -46,6 +48,7 @@ describe('Job Status Graph Data Service', function() {
$provide.value("$cookieStore", { get: angular.noop });
$provide.value('ProcessErrors', processErrors);
$provide.value('Rest', restStub);
$provide.value('GetBasePath', getBasePath);
}));
@ -77,14 +80,20 @@ describe('Job Status Graph Data Service', function() {
});
it('processes errors through error handler', function() {
var expected = { data: "error", status: "bad" };
var actual = jobStatusGraphData.get();
var expected = { data: "blah", status: "bad" };
var actual = jobStatusGraphData.get().catch(function() {
return processErrors;
});
restStub.fail(expected);
flushPromises();
return expect(actual).to.be.rejectedWith(expected);
return actual.catch(function() {
expect(processErrors).to
.have.been.calledWith(null, expected.data, expected.status);
});
});
it('broadcasts event when data is received', function() {