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

Commit ba1a447

Browse files
committed
#501 Handle 404 errors at Express backend at at Angular frontend
- `/{api|modules|lib}/*` returns error page when path doesn’t exist (from Express). - `/*` always returns index (from Express), but if `$state` doesn’t exist, Angular redirects to `/not-found` (no 404 status in that case though!) - If `Accept: application/json` header is present without `Accept: text/html`, return error as json. Hence looking at non existing /api/* paths with browser would show html error, but querying them with script would return json. - Slightly prettier 404 error Test: ```bash curl http://localhost:3000/api/notfound -4 -H "Accept: application/json" ``` => json error. ```bash curl http://localhost:3000/api/notfound -4 -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0 .8" ``` => html error (imitates Chrome’s Accept header). Starting point was @dotch’s PL: #503 And `req.accepts()` idea came from http://stackoverflow.com/a/9802006
1 parent 8190ee4 commit ba1a447

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
// Setting up route
44
angular.module('core').config(['$stateProvider', '$urlRouterProvider',
55
function($stateProvider, $urlRouterProvider) {
6-
// Redirect to home view when route not found
7-
$urlRouterProvider.otherwise('/');
6+
7+
// Redirect to 404 when route not found
8+
$urlRouterProvider.otherwise('not-found');
89

910
// Home state routing
1011
$stateProvider.
11-
state('home', {
12-
url: '/',
13-
templateUrl: 'modules/core/views/home.client.view.html'
14-
});
12+
state('home', {
13+
url: '/',
14+
templateUrl: 'modules/core/views/home.client.view.html'
15+
}).
16+
state('not-found', {
17+
url: '/not-found',
18+
templateUrl: 'modules/core/views/404.client.view.html'
19+
});
1520
}
1621
]);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Page Not Found</h1>
2+
<div class="alert alert-danger" role="alert">
3+
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
4+
<span class="sr-only">Error:</span>
5+
Page Not Found
6+
</div>

modules/core/server/controllers/core.server.controller.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
/**
4-
* Render the main applicaion page
4+
* Render the main application page
55
*/
66
exports.renderIndex = function(req, res) {
77
res.render('modules/core/server/views/index', {
@@ -19,10 +19,25 @@ exports.renderServerError = function(req, res) {
1919
};
2020

2121
/**
22-
* Render the server not found page
22+
* Render the server not found responses
2323
*/
2424
exports.renderNotFound = function(req, res) {
25-
res.status(404).render('modules/core/server/views/404', {
26-
url: req.originalUrl
27-
});
25+
res.status(404);
26+
27+
// Respond with html page
28+
if (req.accepts('html')) {
29+
res.render('modules/core/server/views/404', {
30+
url: req.originalUrl
31+
});
32+
return;
33+
}
34+
35+
// Respond with json to API calls
36+
if (req.accepts('json')) {
37+
res.json({ error: 'Path not found' });
38+
return;
39+
}
40+
41+
// Default to plain-text
42+
res.type('txt').send('Path not found');
2843
};

modules/core/server/views/404.server.view.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
{% block content %}
44
<h1>Page Not Found</h1>
5-
<pre>
5+
<div class="alert alert-danger" role="alert">
6+
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
7+
<span class="sr-only">Error:</span>
68
{{url}} is not a valid path.
7-
</pre>
9+
</div>
810
{% endblock %}

0 commit comments

Comments
 (0)