From 4cdfea0b3e392ed56ff19ccf6d463104a5a078b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Wed, 14 Feb 2018 10:25:01 +0100 Subject: [PATCH] fix(errors): dedicated 404 errors --- app.js | 4 ++-- i18n/locales/de-DE.json | 11 ++++++++--- i18n/locales/en-US.json | 11 ++++++++--- routes/courses.js | 32 ++++++++++++++++++++------------ views/error.pug | 3 ++- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/app.js b/app.js index fc4b415..54396b6 100644 --- a/app.js +++ b/app.js @@ -37,7 +37,7 @@ app.use(express.static(path.join(__dirname, 'public'))) // Force all requests on production to be served over https app.use(function (req, res, next) { 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) } next() @@ -132,7 +132,7 @@ app.use('/', routes) // Catch 404 and forward to error handler app.use(function (request, response, next) { - var err = new Error('Not Found') + const err = new Error(translate('error404Route', response.currentLocale)) err.status = 404 next(err) }) diff --git a/i18n/locales/de-DE.json b/i18n/locales/de-DE.json index ca39249..9a0e56d 100644 --- a/i18n/locales/de-DE.json +++ b/i18n/locales/de-DE.json @@ -87,7 +87,7 @@ "cpaAccessTokenLabel": "Content Preview API Access Token", "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”).", - "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.", "saveSettingsButtonLabel": "Einstellungen Speichern", "fieldIsRequiredLabel": "Diese Feld ist notwendig.", @@ -98,7 +98,6 @@ "intermediateLabel": "Fortgeschrittener", "advancedLabel": "Experte", "editInTheWebAppLabel": "In der Contentful web app bearbeiten", - "error404Route": "Eine nicht unterstützte HTTP Route", "currentLocaleLabel": "Deutsch (Deutschland)", "hostedLabel": "Gehostet", "comingSoonLabel": "Bald verfügbar", @@ -114,5 +113,11 @@ "loadedFromLocalFileLabel": "Geladen von der lokalen Datei", "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.", - "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" } \ No newline at end of file diff --git a/i18n/locales/en-US.json b/i18n/locales/en-US.json index a4dcc94..725b643 100644 --- a/i18n/locales/en-US.json +++ b/i18n/locales/en-US.json @@ -98,7 +98,6 @@ "intermediateLabel": "Intermediate", "advancedLabel": "Advanced", "editInTheWebAppLabel": "Edit in the Contentful web app", - "error404Route": "Verify that you are trying to reach a supported route", "currentLocaleLabel": "English (United States)", "hostedLabel": "Hosted", "comingSoonLabel": "Coming soon", @@ -110,9 +109,15 @@ "resetCredentialsLabel": "Reset credentials to default", "resetAboveLabel": "You can reset to the default credentials above.", "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", "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.", - "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" } \ No newline at end of file diff --git a/routes/courses.js b/routes/courses.js index e94b67b..9a48b17 100644 --- a/routes/courses.js +++ b/routes/courses.js @@ -52,7 +52,15 @@ module.exports.getCourses = async (request, response, next) => { * @returns {undefined} */ 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 const lessons = course.fields.lessons @@ -86,18 +94,18 @@ module.exports.getCourse = async (request, response, next) => { * @returns {undefined} */ module.exports.getCoursesByCategory = async (request, response, next) => { - // We get all the entries with the content type `course` filtered by a category - let courses = [] - let categories = [] - let activeCategory = '' - try { - categories = await getCategories(response.locals.currentLocale.code, response.locals.currentApi.id) - activeCategory = categories.find((category) => category.fields.slug === request.params.category) - courses = await getCoursesByCategory(activeCategory.sys.id, response.locals.currentLocale.code, response.locals.currentApi.id) - } catch (e) { - console.log('Error ', e) + const categories = await getCategories(response.locals.currentLocale.code, response.locals.currentApi.id) + const activeCategory = categories.find((category) => category.fields.slug === request.params.category) + + if (!activeCategory) { + const error = new Error(translate('error404Category', response.currentLocale)) + error.status = 404 + throw error } + // 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 enhanceBreadcrumb(request, activeCategory) @@ -120,7 +128,7 @@ module.exports.getLesson = async (request, response, next) => { let {lesson, nextLesson} = getNextLesson(lessons, request.params.lslug) if (!lesson) { - const error = new Error('Lesson does not exist') + const error = new Error(translate('error404Lesson', response.currentLocale)) error.status = 404 throw error } diff --git a/views/error.pug b/views/error.pug index 4bd1e7d..39cef78 100644 --- a/views/error.pug +++ b/views/error.pug @@ -6,11 +6,12 @@ block content .layout-centered .error h1 #{translate('somethingWentWrongLabel', currentLocale.code)} (#{error.status}) + p #{error.message} h2 #{translate('tryLabel', currentLocale.code)}: ul case error.status when 404 - li #{translate('error404Route', currentLocale.code)} + li #{translate('notFoundErrorLabel', currentLocale.code)} li #{translate('draftOrPublishedErrorLabel', currentLocale.code)} when 400 li #{translate('contentModelChangedErrorLabel', currentLocale.code)}