Skip to content

Commit b7bfda4

Browse files
committed
add jobsHelper errorHandler and propagate it to api errorHandler
1 parent 8913962 commit b7bfda4

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/helper/jobsHelper.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ angular
2727
.service('jobsHelper', function($q, Jobs) {
2828
'use strict';
2929
var self = this;
30-
30+
var errorHandler;
3131

3232
/**
3333
* @ngdoc function
@@ -123,7 +123,34 @@ angular
123123
return self.listen(jobId)
124124
.then(function() {
125125
return asyncResult;
126+
})
127+
.catch(function(err) {
128+
if(errorHandler) {
129+
errorHandler(err);
130+
}
131+
throw err;
126132
});
127133
});
128134
};
135+
136+
/**
137+
* @ngdoc function
138+
* @methodOf predicsis.jsSDK.helpers.jobsHelper
139+
* @name setErrorHandler
140+
* @description set error handler (errors occuring in a job)
141+
*
142+
* Usage example:
143+
* <pre>
144+
* return jobsHelper
145+
* .setErrorHandler(function(error) {
146+
* // do something with error
147+
* // ...
148+
* });
149+
* </pre>
150+
*
151+
* @param {Function} callback called when an error occurs during a Job
152+
*/
153+
self.setErrorHandler = function(cb) {
154+
errorHandler = cb;
155+
};
129156
});

lib/predicsis-jsSDK.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ angular.module('predicsis.jsSDK.helpers', []);
44
angular
55
.module('predicsis.jsSDK', ['predicsis.jsSDK.models', 'predicsis.jsSDK.helpers', 'restangular'])
66
.provider('predicsisAPI', function () {
7-
var errorHandler = function(response) { throw Error(response); }
7+
'use strict';
8+
var errorHandler = function(response) { throw Error(response); };
89
var baseURL = 'https://api.predicsis.com';
910
var oauthToken = 'no-token-defined';
1011

@@ -18,13 +19,15 @@ angular
1819
this.setErrorHandler = function(handler) { errorHandler = handler; };
1920

2021
this.$get = function(Restangular,
21-
Datasets, Dictionaries, Jobs, Modalities, Models, OauthTokens, OauthApplications, PreparationRules, Projects, Reports, UserSettings, Sources, Uploads, Users, Variables,
22+
Datasets, Dictionaries, Jobs, Modalities, Models, OauthTokens, OauthApplications,
23+
PreparationRules, Projects, Reports, UserSettings, Sources, Uploads, Users, Variables,
2224
datasetHelper, jobsHelper, modelHelper, projectsHelper, s3FileHelper) {
2325
var self = this;
2426

2527
Restangular.setBaseUrl(this.getBaseUrl());
2628
Restangular.setDefaultHeaders({ accept: 'application/json', Authorization: 'Bearer ' + this.getOauthToken() });
2729
Restangular.setErrorInterceptor(function(response) { errorHandler(response); });
30+
jobsHelper.setErrorHandler(function(err) { errorHandler(err); });
2831
Restangular.addResponseInterceptor(function(data, operation, what, url, response) {
2932
//operation is one of 'getList', 'post', 'get', 'patch'
3033
if (['getList', 'post', 'get', 'patch'].indexOf(operation) > -1) {

0 commit comments

Comments
 (0)