fix(errors): dedicated 404 errors

This commit is contained in:
Benedikt Rötsch
2018-02-14 10:25:01 +01:00
committed by Benedikt Rötsch
parent 9ca77e4e9c
commit 4cdfea0b3e
5 changed files with 40 additions and 21 deletions

4
app.js
View File

@@ -37,7 +37,7 @@ app.use(express.static(path.join(__dirname, 'public')))
// Force all requests on production to be served over https // Force all requests on production to be served over https
app.use(function (req, res, next) { app.use(function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https' && process.env.NODE_ENV === 'production') { if (req.headers['x-forwarded-proto'] !== 'https' && process.env.NODE_ENV === 'production') {
var secureUrl = 'https://' + req.hostname + req.originalUrl const secureUrl = 'https://' + req.hostname + req.originalUrl
res.redirect(302, secureUrl) res.redirect(302, secureUrl)
} }
next() next()
@@ -132,7 +132,7 @@ app.use('/', routes)
// Catch 404 and forward to error handler // Catch 404 and forward to error handler
app.use(function (request, response, next) { app.use(function (request, response, next) {
var err = new Error('Not Found') const err = new Error(translate('error404Route', response.currentLocale))
err.status = 404 err.status = 404
next(err) next(err)
}) })

View File

@@ -87,7 +87,7 @@
"cpaAccessTokenLabel": "Content Preview API Access Token", "cpaAccessTokenLabel": "Content Preview API Access Token",
"contentDeliveryApiHelpText": "Schauen Sie sich veröffentlichten Inhalt mit diesem Token an.", "contentDeliveryApiHelpText": "Schauen Sie sich veröffentlichten Inhalt mit diesem Token an.",
"contentPreviewApiHelpText": "Schauen Sie sich veröffentlichten Inhalt mit diesem Token an. (z.B. Inhalt im Zustand “Entwurf”).", "contentPreviewApiHelpText": "Schauen Sie sich veröffentlichten Inhalt mit diesem Token an. (z.B. Inhalt im Zustand “Entwurf”).",
"enableEditorialFeaturesLabel": "Editoriale Funktionen aktivieren.", "enableEditorialFeaturesLabel": "Editoriale Funktionen aktivieren",
"enableEditorialFeaturesHelpText": "Aktivieren, um Bearbeitung und weitere kontextabhängige Helfer zu aktivieren. Damit dies funktioniert, müssen sie Zugang zu dem Space haben.", "enableEditorialFeaturesHelpText": "Aktivieren, um Bearbeitung und weitere kontextabhängige Helfer zu aktivieren. Damit dies funktioniert, müssen sie Zugang zu dem Space haben.",
"saveSettingsButtonLabel": "Einstellungen Speichern", "saveSettingsButtonLabel": "Einstellungen Speichern",
"fieldIsRequiredLabel": "Diese Feld ist notwendig.", "fieldIsRequiredLabel": "Diese Feld ist notwendig.",
@@ -98,7 +98,6 @@
"intermediateLabel": "Fortgeschrittener", "intermediateLabel": "Fortgeschrittener",
"advancedLabel": "Experte", "advancedLabel": "Experte",
"editInTheWebAppLabel": "In der Contentful web app bearbeiten", "editInTheWebAppLabel": "In der Contentful web app bearbeiten",
"error404Route": "Eine nicht unterstützte HTTP Route",
"currentLocaleLabel": "Deutsch (Deutschland)", "currentLocaleLabel": "Deutsch (Deutschland)",
"hostedLabel": "Gehostet", "hostedLabel": "Gehostet",
"comingSoonLabel": "Bald verfügbar", "comingSoonLabel": "Bald verfügbar",
@@ -114,5 +113,11 @@
"loadedFromLocalFileLabel": "Geladen von der lokalen Datei", "loadedFromLocalFileLabel": "Geladen von der lokalen Datei",
"usingServerCredentialsLabel": "Die Beispielanwendung verwendet derzeit serverseitig gespeicherte Anmeldeinformationen zum Herstellen einer Verbindung mit einem Contentful-Space.", "usingServerCredentialsLabel": "Die Beispielanwendung verwendet derzeit serverseitig gespeicherte Anmeldeinformationen zum Herstellen einer Verbindung mit einem Contentful-Space.",
"usingSessionCredentialsLabel": "Die Beispielanwendung verwendet derzeit Anmeldeinformationen von der Anwendungssitzung, um eine Verbindung zu einem Contentful-Space herzustellen.", "usingSessionCredentialsLabel": "Die Beispielanwendung verwendet derzeit Anmeldeinformationen von der Anwendungssitzung, um eine Verbindung zu einem Contentful-Space herzustellen.",
"applicationCredentialsLabel": "Anmeldeinformationen für die Anwendungssitzung" "applicationCredentialsLabel": "Anmeldeinformationen für die Anwendungssitzung",
"notFoundErrorLabel": "Diese Seite kann nicht gefunden werden. Stellen Sie sicher, dass sie existiert und veröffentlicht ist.",
"error404Route": "Vergewissern Sie sich, dass sie eine unterstützte Adresse aufrufen.",
"error404Lesson": "Vergewissern Sie sich, dass die Lektion veröffentlicht ist und nicht gelöscht wurde.",
"error404Course": "Vergewissern Sie sich, dass der Kurs veröffentlicht ist und nicht gelöscht wurde.",
"error404Category": "Vergewissern Sie sich, dass die Kategorie veröffentlicht ist und nicht gelöscht wurde.",
"errorHighlightedCourse": "⚠️ Dieser Kurs wurde nicht veröffentlicht oder existiert nicht"
} }

View File

@@ -98,7 +98,6 @@
"intermediateLabel": "Intermediate", "intermediateLabel": "Intermediate",
"advancedLabel": "Advanced", "advancedLabel": "Advanced",
"editInTheWebAppLabel": "Edit in the Contentful web app", "editInTheWebAppLabel": "Edit in the Contentful web app",
"error404Route": "Verify that you are trying to reach a supported route",
"currentLocaleLabel": "English (United States)", "currentLocaleLabel": "English (United States)",
"hostedLabel": "Hosted", "hostedLabel": "Hosted",
"comingSoonLabel": "Coming soon", "comingSoonLabel": "Coming soon",
@@ -110,9 +109,15 @@
"resetCredentialsLabel": "Reset credentials to default", "resetCredentialsLabel": "Reset credentials to default",
"resetAboveLabel": "You can reset to the default credentials above.", "resetAboveLabel": "You can reset to the default credentials above.",
"closeLabel": "Close", "closeLabel": "Close",
"overrideConfigLabel": "This configuration can be overriden through the form below or by using query string parameters.", "overrideConfigLabel": "This configuration can be overridden through the form below or by using query string parameters.",
"loadedFromLocalFileLabel": "Loaded from local file", "loadedFromLocalFileLabel": "Loaded from local file",
"usingServerCredentialsLabel": "The example app is currently using server side stored credentials to connect to a Contentful space.", "usingServerCredentialsLabel": "The example app is currently using server side stored credentials to connect to a Contentful space.",
"usingSessionCredentialsLabel": "The example app is currently using application session stored credentials to connect to a Contentful space.", "usingSessionCredentialsLabel": "The example app is currently using application session stored credentials to connect to a Contentful space.",
"applicationCredentialsLabel": "Application session credentials" "applicationCredentialsLabel": "Application session credentials",
"notFoundErrorLabel": "The page you are trying to access could not be found. Make sure it is existing and published.",
"error404Route": "Make sure that you are trying to reach a supported address.",
"error404Lesson": "Make sure that the lesson has not been deleted or unpublished.",
"error404Course": "Make sure that the course has not been deleted or unpublished.",
"error404Category": "Make sure that the category has not been deleted or unpublished.",
"errorHighlightedCourse": "⚠️ The course is not published or does not exist"
} }

View File

@@ -52,7 +52,15 @@ module.exports.getCourses = async (request, response, next) => {
* @returns {undefined} * @returns {undefined}
*/ */
module.exports.getCourse = async (request, response, next) => { module.exports.getCourse = async (request, response, next) => {
let course = await getCourse(request.params.slug, response.locals.currentLocale.code, response.locals.currentApi.id) let course
try {
course = await getCourse(request.params.slug, response.locals.currentLocale.code, response.locals.currentApi.id)
} catch (err) {
if (err.status === 404) {
err.message = translate('error404Course', response.currentLocale)
}
throw err
}
// Get lessons // Get lessons
const lessons = course.fields.lessons const lessons = course.fields.lessons
@@ -86,18 +94,18 @@ module.exports.getCourse = async (request, response, next) => {
* @returns {undefined} * @returns {undefined}
*/ */
module.exports.getCoursesByCategory = async (request, response, next) => { module.exports.getCoursesByCategory = async (request, response, next) => {
// We get all the entries with the content type `course` filtered by a category const categories = await getCategories(response.locals.currentLocale.code, response.locals.currentApi.id)
let courses = [] const activeCategory = categories.find((category) => category.fields.slug === request.params.category)
let categories = []
let activeCategory = '' if (!activeCategory) {
try { const error = new Error(translate('error404Category', response.currentLocale))
categories = await getCategories(response.locals.currentLocale.code, response.locals.currentApi.id) error.status = 404
activeCategory = categories.find((category) => category.fields.slug === request.params.category) throw error
courses = await getCoursesByCategory(activeCategory.sys.id, response.locals.currentLocale.code, response.locals.currentApi.id)
} catch (e) {
console.log('Error ', e)
} }
// We get all the entries with the content type `course` filtered by a category
const courses = await getCoursesByCategory(activeCategory.sys.id, response.locals.currentLocale.code, response.locals.currentApi.id)
// Enhance the breadcrumbs with the active category // Enhance the breadcrumbs with the active category
enhanceBreadcrumb(request, activeCategory) enhanceBreadcrumb(request, activeCategory)
@@ -120,7 +128,7 @@ module.exports.getLesson = async (request, response, next) => {
let {lesson, nextLesson} = getNextLesson(lessons, request.params.lslug) let {lesson, nextLesson} = getNextLesson(lessons, request.params.lslug)
if (!lesson) { if (!lesson) {
const error = new Error('Lesson does not exist') const error = new Error(translate('error404Lesson', response.currentLocale))
error.status = 404 error.status = 404
throw error throw error
} }

View File

@@ -6,11 +6,12 @@ block content
.layout-centered .layout-centered
.error .error
h1 #{translate('somethingWentWrongLabel', currentLocale.code)} (#{error.status}) h1 #{translate('somethingWentWrongLabel', currentLocale.code)} (#{error.status})
p #{error.message}
h2 #{translate('tryLabel', currentLocale.code)}: h2 #{translate('tryLabel', currentLocale.code)}:
ul ul
case error.status case error.status
when 404 when 404
li #{translate('error404Route', currentLocale.code)} li #{translate('notFoundErrorLabel', currentLocale.code)}
li #{translate('draftOrPublishedErrorLabel', currentLocale.code)} li #{translate('draftOrPublishedErrorLabel', currentLocale.code)}
when 400 when 400
li #{translate('contentModelChangedErrorLabel', currentLocale.code)} li #{translate('contentModelChangedErrorLabel', currentLocale.code)}