-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1273 from marmelab/http-error-handling
Http error handling
- Loading branch information
Showing
7 changed files
with
159 additions
and
14 deletions.
There are no files selected for viewing
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
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
36 changes: 36 additions & 0 deletions
36
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const HttpErrorService = ($state, $translate, notification) => ({ | ||
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; | ||
} | ||
}, | ||
|
||
handle404Error: function(event) { | ||
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.data.message }).then(this.displayError); | ||
throw error; | ||
}, | ||
|
||
displayError: text => notification.log(text, { addnCls: 'humane-flatty-error' }), | ||
}); | ||
|
||
HttpErrorService.$inject = ['$state', '$translate', 'notification']; | ||
|
||
export default { $get: HttpErrorService }; |
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
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
export default function HttpErrorHandler($rootScope, HttpErrorService) { | ||
$rootScope.$on("$stateChangeError", (event, toState, toParams, fromState, fromParams, error) => { | ||
HttpErrorService.handleError(event, toState, toParams, fromState, fromParams, error); | ||
}); | ||
} | ||
|
||
HttpErrorHandler.$inject = ['$rootScope', 'HttpErrorService']; |
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
const HttpErrorServiceProvider = require('../../../../../ng-admin/Main/component/provider/HttpErrorService'); | ||
|
||
describe('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'), | ||
})) | ||
.provider('HttpErrorService', HttpErrorServiceProvider); | ||
|
||
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, | ||
data: { | ||
message: 'Unknown error', | ||
} | ||
}; | ||
|
||
try { | ||
HttpErrorService.handleError('', '', '', '', '', error); | ||
expect(true).toBe(false); | ||
} catch (e) { | ||
// should throw an exception in case of 500 | ||
} | ||
|
||
expect($translate).toHaveBeenCalledWith('STATE_CHANGE_ERROR', { message: 'Unknown error' }); | ||
expect(notification.log).toHaveBeenCalledWith('translated', { addnCls: 'humane-flatty-error' }); | ||
}); | ||
}); |