From 50d9c114195ce0a6ce7d5bd37e98f48856152e38 Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Tue, 27 Jan 2015 12:28:11 -0500 Subject: [PATCH] Ensure we always catch errors on Rest calls --- .../js/services/job-status-graph-data.js | 27 ++++++++++--------- .../services/job-status-graph-data-test.js | 15 ++++++++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/awx/ui/static/js/services/job-status-graph-data.js b/awx/ui/static/js/services/job-status-graph-data.js index 4bf681102a..1c2f528a3c 100644 --- a/awx/ui/static/js/services/job-status-graph-data.js +++ b/awx/ui/static/js/services/job-status-graph-data.js @@ -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; }); }); }, diff --git a/awx/ui/tests/unit/services/job-status-graph-data-test.js b/awx/ui/tests/unit/services/job-status-graph-data-test.js index e80102af97..fc0f4cd4bd 100644 --- a/awx/ui/tests/unit/services/job-status-graph-data-test.js +++ b/awx/ui/tests/unit/services/job-status-graph-data-test.js @@ -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() {