Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 6be12f8

Browse files
authored
fix(core): Add custom 400 and 404 error messages (#1547)
* Added 400 and 404 custom error messages * nicer error message views * Sign Up & Sign In error responses Changed the error responses returned from the Sign Up & Sign In API calls to use 422 rather than 400. For insight into why this change was made: #1510 (comment) For reference on why to use 422 over 400: https://www.bennadel.com/blog/2434-http-status-codes-for-invalid-data-400-vs-422.htm
1 parent 8645b24 commit 6be12f8

File tree

7 files changed

+55
-9
lines changed

7 files changed

+55
-9
lines changed

modules/core/client/config/core.client.routes.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,31 @@
3636
.state('not-found', {
3737
url: '/not-found',
3838
templateUrl: 'modules/core/client/views/404.client.view.html',
39+
controller: 'ErrorController',
40+
controllerAs: 'vm',
41+
params: {
42+
message: function($stateParams) {
43+
return $stateParams.message;
44+
}
45+
},
3946
data: {
4047
ignoreState: true,
41-
pageTitle: 'Not-Found'
48+
pageTitle: 'Not Found'
4249
}
4350
})
4451
.state('bad-request', {
4552
url: '/bad-request',
4653
templateUrl: 'modules/core/client/views/400.client.view.html',
54+
controller: 'ErrorController',
55+
controllerAs: 'vm',
56+
params: {
57+
message: function($stateParams) {
58+
return $stateParams.message;
59+
}
60+
},
4761
data: {
4862
ignoreState: true,
49-
pageTitle: 'Bad-Request'
63+
pageTitle: 'Bad Request'
5064
}
5165
})
5266
.state('forbidden', {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(function () {
2+
'use strict';
3+
4+
angular
5+
.module('core')
6+
.controller('ErrorController', ErrorController);
7+
8+
ErrorController.$inject = ['$stateParams'];
9+
10+
function ErrorController($stateParams) {
11+
var vm = this;
12+
vm.errorMessage = null;
13+
14+
// Display custom message if it was set
15+
if ($stateParams.message) vm.errorMessage = $stateParams.message;
16+
}
17+
}());
18+

modules/core/client/services/interceptors/auth-interceptor.client.service.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
function responseError(rejection) {
1818
if (!rejection.config.ignoreAuthModule) {
1919
switch (rejection.status) {
20+
case 400:
21+
$injector.get('$state').go('bad-request', { message: rejection.data.message });
22+
break;
2023
case 401:
2124
// Deauthenticate the global user
2225
Authentication.user = null;
@@ -25,6 +28,9 @@
2528
case 403:
2629
$injector.get('$state').transitionTo('forbidden');
2730
break;
31+
case 404:
32+
$injector.get('$state').go('not-found', { message: rejection.data.message });
33+
break;
2834
}
2935
}
3036
// otherwise, default behaviour
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<h1>Bad Request</h1>
1+
<div class="page-header">
2+
<h1>Bad Request</h1>
3+
</div>
24
<div class="alert alert-danger" role="alert">
35
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
46
<span class="sr-only">Error:</span>
5-
You made a bad request
7+
<span ng-if="vm.errorMessage" ng-bind="vm.errorMessage"></span>
8+
<span ng-if="!vm.errorMessage">You made a bad request</span>
69
</div>

modules/core/client/views/403.client.view.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<h1>Forbidden</h1>
1+
<div class="page-header">
2+
<h1>Forbidden</h1>
3+
</div>
24
<div class="alert alert-danger" role="alert">
35
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
46
<span class="sr-only">Error:</span>
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
<h1>Page Not Found</h1>
1+
<div class="page-header">
2+
<h1>Page Not Found</h1>
3+
</div>
24
<div class="alert alert-danger" role="alert">
35
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
4-
<span class="sr-only">Error:</span> Page Not Found
6+
<span ng-if="vm.errorMessage" ng-bind="vm.errorMessage"></span>
7+
<span ng-if="!vm.errorMessage">Page Not Found</span>
58
</div>

modules/users/server/controllers/users/users.authentication.server.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.signup = function (req, res) {
3030
// Then save the user
3131
user.save(function (err) {
3232
if (err) {
33-
return res.status(400).send({
33+
return res.status(422).send({
3434
message: errorHandler.getErrorMessage(err)
3535
});
3636
} else {
@@ -55,7 +55,7 @@ exports.signup = function (req, res) {
5555
exports.signin = function (req, res, next) {
5656
passport.authenticate('local', function (err, user, info) {
5757
if (err || !user) {
58-
res.status(400).send(info);
58+
res.status(422).send(info);
5959
} else {
6060
// Remove sensitive data before login
6161
user.password = undefined;

0 commit comments

Comments
 (0)