Skip to content

Commit

Permalink
Merge pull request #23 from UKHomeOffice/feature/error-handling
Browse files Browse the repository at this point in the history
Use the latest middleware from hof.
  • Loading branch information
JoeChapman committed Apr 5, 2016
2 parents 4d7fcb9 + 5dda8b1 commit 74b8119
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ app.use(require('hof').middleware());
// apps
app.use(require('./apps/my_awesome_form/'));

app.use(require('./middleware/not-found')());

// errors
app.use(require('./errors/'));

Expand Down
10 changes: 9 additions & 1 deletion apps/common/translations/en/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
}
},
"errors": {
"404": {
"title": "Page not found",
"description": "This page does not exist"
},
"default": {
"title": "Sorry, something went wrong",
"message": "A server exception occurred. Please try again."
},
"session": {
"title": "Sorry, you'll have to start again",
"message": "You haven't entered any details for 20 minutes, so we have cleared your information to keep it secure."
"message": "You haven't entered any details for 30 minutes, so we have cleared your information to keep it secure."
},
"cookies-required": {
"title": "Cookies are required to use this service",
"message": "Cookies are required in order to use the BRP service.<br /><br /> Please <a href=\"http://www.aboutcookies.org/how-to-control-cookies/\" rel=\"external\">enable cookies</a> and try again. Find out <a href=\"/cookies\">how to we use cookies</a>."
}
}
}
8 changes: 8 additions & 0 deletions apps/common/translations/src/en/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"session": {
"title": "Sorry, you'll have to start again",
"message": "You haven't entered any details for 20 minutes, so we have cleared your information to keep it secure."
},
"404": {
"title": "Page not found",
"description": "This page does not exist"
},
"cookies-required": {
"title": "Cookies are required to use this service",
"message": "Cookies are required in order to use the BRP service.<br /><br /> Please <a href=\"http://www.aboutcookies.org/how-to-control-cookies/\" rel=\"external\">enable cookies</a> and try again. Find out <a href=\"/cookies\">how to we use cookies</a>."
}
}
11 changes: 11 additions & 0 deletions apps/common/views/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{<layout}}

{{$header}}
{{content.title}}
{{/header}}

{{$content}}
<p>{{description}}</p>
{{/content}}

{{/layout}}
2 changes: 1 addition & 1 deletion apps/common/views/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{/header}}

{{$content}}
<p>{{content.message}}</p>
<p>{{{content.message}}}</p>
<a href="/{{startLink}}" class="button" role="button">Start again</a>
{{#showStack}}
<pre>
Expand Down
8 changes: 7 additions & 1 deletion errors/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

var path = require('path');
var hof = require('hof');
var i18n = hof.i18n({
Expand All @@ -17,14 +18,19 @@ module.exports = function errorHandler(err, req, res) {
content.message = i18n.translate('errors.session.message');
}

if (err.code === 'NO_COOKIES') {
err.status = 403;
content.title = i18n.translate('errors.cookies-required.title');
content.message = i18n.translate('errors.cookies-required.message');
}

err.template = 'error';
content.title = content.title || i18n.translate('errors.default.title');
content.message = content.message || i18n.translate('errors.default.message');

res.statusCode = err.status || 500;

logger.error(err.message || err.error, err);

res.render(err.template, {
error: err,
content: content,
Expand Down
19 changes: 19 additions & 0 deletions middleware/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var path = require('path');
var hof = require('hof');
var i18n = hof.i18n({
path: path.resolve(__dirname, '../apps/common/translations/__lng__/__ns__.json')
});
var logger = require('../lib/logger');

module.exports = function notFound() {
return function notFoundHandler(req, res) {
logger.warn('Cannot find:', req.url);

res.status(404).render('404', {
title: i18n.translate('errors.404.title'),
description: i18n.translate('errors.404.description')
});
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"express": "^4.12.4",
"express-partial-templates": "^0.1.0",
"express-session": "^1.11.3",
"hof": "4.0.0",
"hof": "5.0.0",
"hogan-express-strict": "^0.5.4",
"hogan.js": "^3.0.2",
"jquery": "^2.1.4",
Expand Down

0 comments on commit 74b8119

Please sign in to comment.