chore: Improve error page (#66)
* chore: Improve error page * fix: Fix middlware order * fix: Remove console.log
This commit is contained in:
49
app.js
49
app.js
@@ -16,6 +16,7 @@ const breadcrumb = require('./lib/breadcrumb')
|
|||||||
const settings = require('./lib/settings')
|
const settings = require('./lib/settings')
|
||||||
const routes = require('./routes/index')
|
const routes = require('./routes/index')
|
||||||
const { getSpace } = require('./services/contentful')
|
const { getSpace } = require('./services/contentful')
|
||||||
|
const { catchErrors } = require('./handlers/errorHandlers')
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
@@ -43,22 +44,18 @@ app.use(function (req, res, next) {
|
|||||||
app.use(settings)
|
app.use(settings)
|
||||||
|
|
||||||
// Make data available for our views to consume
|
// Make data available for our views to consume
|
||||||
app.use(async function (request, response, next) {
|
app.use(catchErrors(async function (request, response, next) {
|
||||||
// Get enabled locales from Contentful
|
// Get enabled locales from Contentful
|
||||||
const space = await getSpace()
|
response.locals.locales = [{code: 'en-US', name: 'U.S. English'}]
|
||||||
response.locals.locales = space.locales
|
response.locals.currentLocale = response.locals.locales[0]
|
||||||
|
// Inject custom helpers
|
||||||
|
response.locals.helpers = helpers
|
||||||
|
|
||||||
const defaultLocale = response.locals.locales
|
// Make query string available in templates to render links properly
|
||||||
.find((locale) => locale.default)
|
const qs = querystring.stringify(request.query)
|
||||||
|
response.locals.queryString = qs ? `?${qs}` : ''
|
||||||
if (request.query.locale) {
|
response.locals.query = request.query
|
||||||
response.locals.currentLocale = space.locales
|
response.locals.currentPath = request.path
|
||||||
.find((locale) => locale.code === request.query.locale)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!response.locals.currentLocale) {
|
|
||||||
response.locals.currentLocale = defaultLocale
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize translations and include them on templates
|
// Initialize translations and include them on templates
|
||||||
initializeTranslations()
|
initializeTranslations()
|
||||||
@@ -79,17 +76,23 @@ app.use(async function (request, response, next) {
|
|||||||
response.locals.currentApi = apis
|
response.locals.currentApi = apis
|
||||||
.find((api) => api.id === (request.query.api || 'cda'))
|
.find((api) => api.id === (request.query.api || 'cda'))
|
||||||
|
|
||||||
// Inject custom helpers
|
const space = await getSpace()
|
||||||
response.locals.helpers = helpers
|
response.locals.locales = space.locales
|
||||||
|
|
||||||
// Make query string available in templates to render links properly
|
const defaultLocale = response.locals.locales
|
||||||
const qs = querystring.stringify(request.query)
|
.find((locale) => locale.default)
|
||||||
response.locals.queryString = qs ? `?${qs}` : ''
|
|
||||||
response.locals.query = request.query
|
if (request.query.locale) {
|
||||||
response.locals.currentPath = request.path
|
response.locals.currentLocale = space.locales
|
||||||
|
.find((locale) => locale.code === request.query.locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.locals.currentLocale) {
|
||||||
|
response.locals.currentLocale = defaultLocale
|
||||||
|
}
|
||||||
|
|
||||||
next()
|
next()
|
||||||
})
|
}))
|
||||||
|
|
||||||
app.use(breadcrumb())
|
app.use(breadcrumb())
|
||||||
|
|
||||||
@@ -108,7 +111,7 @@ app.use(function (request, response, next) {
|
|||||||
app.use(function (err, request, response, next) {
|
app.use(function (err, request, response, next) {
|
||||||
// Set locals, only providing error in development
|
// Set locals, only providing error in development
|
||||||
response.locals.error = request.app.get('env') === 'development' ? err : {}
|
response.locals.error = request.app.get('env') === 'development' ? err : {}
|
||||||
|
response.locals.error.status = err.status || 500
|
||||||
// Render the error page
|
// Render the error page
|
||||||
response.status(err.status || 500)
|
response.status(err.status || 500)
|
||||||
response.render('error')
|
response.render('error')
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
module.exports.catchErrors = (fn) => {
|
module.exports.catchErrors = (fn) => {
|
||||||
return function (request, response, next) {
|
return function (request, response, next) {
|
||||||
return fn(request, response, next).catch((e) => {
|
return fn(request, response, next).catch((e) => {
|
||||||
|
if (e.response) {
|
||||||
|
e.status = e.response.status
|
||||||
|
}
|
||||||
next(e)
|
next(e)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,5 +80,6 @@
|
|||||||
"beginnerLabel": "Anfänger",
|
"beginnerLabel": "Anfänger",
|
||||||
"intermediateLabel": "Fortgeschrittener",
|
"intermediateLabel": "Fortgeschrittener",
|
||||||
"advancedLabel": "Experte",
|
"advancedLabel": "Experte",
|
||||||
"editInTheWebAppLabel": "In der web app bearbeiten"
|
"editInTheWebAppLabel": "In der web app bearbeiten",
|
||||||
|
"error404Route": "Ein nicht unterstützter HTTP Route"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,5 +80,6 @@
|
|||||||
"beginnerLabel": "Beginner",
|
"beginnerLabel": "Beginner",
|
||||||
"intermediateLabel": "Intermediate",
|
"intermediateLabel": "Intermediate",
|
||||||
"advancedLabel": "Advanced",
|
"advancedLabel": "Advanced",
|
||||||
"editInTheWebAppLabel": "Edit in the web app"
|
"editInTheWebAppLabel": "Edit in the web app",
|
||||||
|
"error404Route": "a non supported route"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,23 @@ block content
|
|||||||
h1 #{translate('somethingWentWrongLabel', currentLocale.code)} (#{error.status})
|
h1 #{translate('somethingWentWrongLabel', currentLocale.code)} (#{error.status})
|
||||||
h2 #{translate('tryLabel', currentLocale.code)}:
|
h2 #{translate('tryLabel', currentLocale.code)}:
|
||||||
ul
|
ul
|
||||||
li #{translate('contentModelChangedErrorLabel', currentLocale.code)}
|
case error.status
|
||||||
|
when 404
|
||||||
|
li #{translate('error404Route', currentLocale.code)}
|
||||||
li #{translate('draftOrPublishedErrorLabel', currentLocale.code)}
|
li #{translate('draftOrPublishedErrorLabel', currentLocale.code)}
|
||||||
li #{translate('localeContentErrorLabel', currentLocale.code)}
|
when 400
|
||||||
|
li #{translate('contentModelChangedErrorLabel', currentLocale.code)}
|
||||||
|
when 401
|
||||||
li #{translate('verifyCredentialsErrorLabel', currentLocale.code)}
|
li #{translate('verifyCredentialsErrorLabel', currentLocale.code)}
|
||||||
|
li #{translate('localeContentErrorLabel', currentLocale.code)}
|
||||||
|
|
||||||
|
if error.stack
|
||||||
li #{translate('stackTraceErrorLabel', currentLocale.code)}
|
li #{translate('stackTraceErrorLabel', currentLocale.code)}
|
||||||
if error.response
|
if error.response
|
||||||
h2 #{translate('errorLabel', currentLocale.code)}
|
h2 #{translate('errorLabel', currentLocale.code)}
|
||||||
pre.error__stack-trace
|
pre.error__stack-trace
|
||||||
code.shell #{helpers.dump(error.response.data)}
|
code.shell #{helpers.dump(error.response.data)}
|
||||||
|
if error.stack
|
||||||
h2 #{translate('stackTraceLabel', currentLocale.code)}
|
h2 #{translate('stackTraceLabel', currentLocale.code)}
|
||||||
pre.error__stack-trace
|
pre.error__stack-trace
|
||||||
code.shell #{error.stack}
|
code.shell #{error.stack}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
mixin breadcrumb
|
mixin breadcrumb
|
||||||
|
if breadcrumb
|
||||||
nav.breadcrumb
|
nav.breadcrumb
|
||||||
ul
|
ul
|
||||||
each item in breadcrumb
|
each item in breadcrumb
|
||||||
|
|||||||
Reference in New Issue
Block a user