-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59cc856
commit 1008d1f
Showing
3 changed files
with
108 additions
and
117 deletions.
There are no files selected for viewing
57 changes: 31 additions & 26 deletions
57
src/javascripts/ng-admin/Main/component/provider/HttpErrorService.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
export default function httpErrorService($state, $translate, notification) { | ||
|
||
function httpErrorService($state, $translate, notification) { | ||
return { | ||
handleError: function(event, toState, toParams, fromState, fromParams, error){ | ||
handleError: function(event, toState, toParams, fromState, fromParams, error) { | ||
switch (error.status) { | ||
case 404: | ||
this.handle404Error(event, error); | ||
break; | ||
case 403: | ||
this.handle403Error(error); | ||
break; | ||
default: | ||
this.handleDefaultError(error); | ||
break; | ||
case 404: | ||
this.handle404Error(event, error); | ||
break; | ||
case 403: | ||
this.handle403Error(error); | ||
break; | ||
default: | ||
this.handleDefaultError(error); | ||
break; | ||
} | ||
}, | ||
handle404Error: function(event,error){ | ||
$state.go('ma-404'); | ||
event.preventDefault(); | ||
}, | ||
handle403Error: function(error){ | ||
$translate('STATE_FORBIDDEN_ERROR', { message: error.data.message }).then(this.displayError); | ||
throw error; | ||
}, | ||
handleDefaultError: function(error){ | ||
$translate('STATE_CHANGE_ERROR', { message: error.message }).then(this.displayError); | ||
}, | ||
|
||
handle404Error: function(event,error) { | ||
event.preventDefault(); | ||
$state.go('ma-404'); | ||
}, | ||
|
||
handle403Error: function(error) { | ||
$translate('STATE_FORBIDDEN_ERROR', { message: error.data.message }).then(this.displayError); | ||
throw error; | ||
}, | ||
|
||
handleDefaultError: function(error) { | ||
$translate('STATE_CHANGE_ERROR', { message: error.message }).then(this.displayError); | ||
throw error; | ||
}, | ||
displayError:text => notification.log(text, { addnCls: 'humane-flatty-error' }) | ||
}, | ||
|
||
displayError: text => notification.log(text, { addnCls: 'humane-flatty-error' }), | ||
} | ||
} | ||
|
||
httpErrorService.$inject =['$state', '$translate', 'notification']; | ||
httpErrorService.$inject = ['$state', '$translate', 'notification']; | ||
|
||
export default httpErrorService; |
91 changes: 0 additions & 91 deletions
91
src/javascripts/test/unit/Main/component/provider/HttpErrorServiceTest.spec
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
src/javascripts/test/unit/Main/component/provider/HttpErrorServiceTest.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const HttpErrorService = require('../../../../../ng-admin/Main/component/provider/HttpErrorService'); | ||
|
||
fdescribe('Http Error Service', () => { | ||
let $state; | ||
let $translate; | ||
let httpErrorService; | ||
let notification; | ||
|
||
angular.module('test_httpErrorService', []) | ||
.service('$state', () => ({ | ||
go: jasmine.createSpy('$state.go'), | ||
})) | ||
.service('$translate', () => ( | ||
jasmine.createSpy('$translate').and.returnValue({ then: cb => cb('translated') }) | ||
)) | ||
.service('notification', () => ({ | ||
log: jasmine.createSpy('notification.log'), | ||
})) | ||
.service('HttpErrorService', HttpErrorService); | ||
|
||
beforeEach(angular.mock.module('test_httpErrorService')); | ||
|
||
beforeEach(inject(function (_$state_, _$translate_, _HttpErrorService_, _notification_) { | ||
$state = _$state_; | ||
$translate = _$translate_; | ||
httpErrorService = _HttpErrorService_; | ||
notification = _notification_; | ||
})); | ||
|
||
it('should redirect to `ma-404` state when receiving a 404 error', () => { | ||
const event = { | ||
preventDefault: jasmine.createSpy(), | ||
}; | ||
|
||
const error = { status: 404 }; | ||
httpErrorService.handleError(event, '', '', '', '', error); | ||
|
||
expect(event.preventDefault).toHaveBeenCalled(); | ||
expect($state.go).toHaveBeenCalledWith('ma-404'); | ||
}); | ||
|
||
it('should display a translated forbidden error notification in case of 403', () => { | ||
const error = { | ||
status: 403, | ||
data: { | ||
message: 'Forbidden access', | ||
}, | ||
}; | ||
|
||
try { | ||
httpErrorService.handleError('', '', '', '', '', error); | ||
expect(true).toBe(false); | ||
} catch (e) { | ||
// should throw an exception in case of 403 | ||
} | ||
|
||
expect($translate).toHaveBeenCalledWith('STATE_FORBIDDEN_ERROR', { message: 'Forbidden access' }); | ||
expect(notification.log).toHaveBeenCalledWith('translated', { addnCls: 'humane-flatty-error' }); | ||
}); | ||
|
||
it('should display generic translated error notification if neither 404 nor 403', () => { | ||
const error = { | ||
status: 500, | ||
message: 'Unknown error', | ||
}; | ||
|
||
try { | ||
httpErrorService.handleError('', '', '', '', '', error); | ||
expect(true).toBe(false); | ||
} catch (e) { | ||
// should throw an exception in case of 403 | ||
} | ||
|
||
expect($translate).toHaveBeenCalledWith('STATE_CHANGE_ERROR', { message: 'Unknown error' }); | ||
expect(notification.log).toHaveBeenCalledWith('translated', { addnCls: 'humane-flatty-error' }); | ||
}); | ||
}); |