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

Commit

Permalink
#501 Handle 404 errors at Express backend at at Angular frontend
Browse files Browse the repository at this point in the history
- `/{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
  • Loading branch information
simison committed May 18, 2015
1 parent 8190ee4 commit ba1a447
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
17 changes: 11 additions & 6 deletions modules/core/client/config/core.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
// Setting up route
angular.module('core').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// Redirect to home view when route not found
$urlRouterProvider.otherwise('/');

// Redirect to 404 when route not found
$urlRouterProvider.otherwise('not-found');

// Home state routing
$stateProvider.
state('home', {
url: '/',
templateUrl: 'modules/core/views/home.client.view.html'
});
state('home', {
url: '/',
templateUrl: 'modules/core/views/home.client.view.html'
}).
state('not-found', {
url: '/not-found',
templateUrl: 'modules/core/views/404.client.view.html'
});
}
]);
6 changes: 6 additions & 0 deletions modules/core/client/views/404.client.view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Page Not Found</h1>
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
Page Not Found
</div>
25 changes: 20 additions & 5 deletions modules/core/server/controllers/core.server.controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

/**
* Render the main applicaion page
* Render the main application page
*/
exports.renderIndex = function(req, res) {
res.render('modules/core/server/views/index', {
Expand All @@ -19,10 +19,25 @@ exports.renderServerError = function(req, res) {
};

/**
* Render the server not found page
* Render the server not found responses
*/
exports.renderNotFound = function(req, res) {
res.status(404).render('modules/core/server/views/404', {
url: req.originalUrl
});
res.status(404);

// Respond with html page
if (req.accepts('html')) {
res.render('modules/core/server/views/404', {
url: req.originalUrl
});
return;
}

// Respond with json to API calls
if (req.accepts('json')) {
res.json({ error: 'Path not found' });
return;
}

// Default to plain-text
res.type('txt').send('Path not found');
};
6 changes: 4 additions & 2 deletions modules/core/server/views/404.server.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

0 comments on commit ba1a447

Please sign in to comment.