diff --git a/handlers/errorHandlers.js b/handlers/errorHandlers.js new file mode 100644 index 0000000..993b2a5 --- /dev/null +++ b/handlers/errorHandlers.js @@ -0,0 +1,12 @@ +/* + Catch Errors Handler + With async/await, you need some way to catch errors + Instead of using try{} catch(e) {} in each controller, we wrap the function in + catchErrors(), catch and errors they throw, and pass it along to our express middleware with next() +*/ + +exports.catchErrors = (fn) => { + return function (req, res, next) { + return fn(req, res, next).catch(next) + } +} diff --git a/routes/categories.js b/routes/categories.js index 703f6a4..c718d83 100644 --- a/routes/categories.js +++ b/routes/categories.js @@ -1,9 +1,10 @@ const express = require('express') +const { catchErrors } = require('../handlers/errorHandlers') const router = express.Router() -/* GET categories listing. */ -router.get('/', function (req, res, next) { +/* GET courses listing. */ +router.get('/', catchErrors(async function (req, res, next) { res.render('categories', { title: 'Categories' }) -}) +})) module.exports = router diff --git a/routes/courses.js b/routes/courses.js index 150d021..d3a4463 100644 --- a/routes/courses.js +++ b/routes/courses.js @@ -1,49 +1,41 @@ const express = require('express') const {getCourses, getCourse, getCategories, getCoursesByCategory} = require('./../services/contentful') - +const { catchErrors } = require('../handlers/errorHandlers') const router = express.Router() /* GET courses listing. */ -router.get('/', async function (req, res, next) { +router.get('/', catchErrors(async function (req, res, next) { // we get all the entries with the content type `course` let courses = [] let categories = [] - try { - courses = await getCourses(req.query.locale, req.query.api) - categories = await getCategories(req.query.locale, req.query.api) - } catch (e) { - console.log('Error ', e) - } + courses = await getCourses(req.query.locale, req.query.api) + categories = await getCategories(req.query.locale, req.query.api) res.render('courses', { title: `All Courses (${courses.length})`, categories, courses }) -}) +})) -/* GET courses listing by category. */ -router.get('/categories/:category', async function (req, res, next) { - // we get all the entries with the content type `course` filtered by a category +/* GET courses listing. */ +router.get('/categories/:category', catchErrors(async function (req, res, next) { + // we get all the entries with the content type `course` let courses = [] let categories = [] let activeCategory = '' - try { - courses = await getCoursesByCategory(req.params.category, req.query.locale, req.query.api) - categories = await getCategories() - activeCategory = categories.find((category) => category.sys.id === req.params.category) - } catch (e) { - console.log('Error ', e) - } + courses = await getCoursesByCategory(req.params.category, req.query.locale, req.query.api) + categories = await getCategories() + activeCategory = categories.find((category) => category.sys.id === req.params.category) res.render('courses', { title: `${activeCategory.fields.title} (${courses.length})`, categories, courses }) -}) +})) /* GET course detail. */ -router.get('/:slug', async function (req, res, next) { +router.get('/:slug', catchErrors(async function (req, res, next) { let course = await getCourse(req.params.slug, req.query.locale, req.query.api) const lessons = course.fields.lessons const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug) const lesson = lessons[lessonIndex] res.render('course', {title: course.fields.title, course, lesson, lessons, lessonIndex}) -}) +})) /* GET course lesson detail. */ -router.get('/:cslug/lessons/:lslug', async function (req, res, next) { +router.get('/:cslug/lessons/:lslug', catchErrors(async function (req, res, next) { let course = await getCourse(req.params.cslug, req.query.locale, req.query.api) const lessons = course.fields.lessons const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug) @@ -56,6 +48,6 @@ router.get('/:cslug/lessons/:lslug', async function (req, res, next) { lessons, nextLesson }) -}) +})) module.exports = router diff --git a/routes/index.js b/routes/index.js index e38428a..3e02c39 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,11 +1,12 @@ const express = require('express') -const {getLandingPage} = require('../services/contentful') +const { getLandingPage } = require('../services/contentful') +const { catchErrors } = require('../handlers/errorHandlers') const router = express.Router() /* GET home page. */ -router.get('/', async function (req, res, next) { +router.get('/', catchErrors(async function (req, res, next) { const landingPage = await getLandingPage(req.query.locale, req.query.api) res.render('index', { title: 'Contentful University', landingPage }) -}) +})) module.exports = router diff --git a/views/mixins/_lesson.pug b/views/mixins/_lesson.pug index 649928c..2db5e88 100644 --- a/views/mixins/_lesson.pug +++ b/views/mixins/_lesson.pug @@ -26,3 +26,4 @@ mixin lesson(lesson, course, nextLesson) .lesson_footer if nextLesson a.lesson__cta.cta(href=`/courses/${course.fields.slug}/lessons/${nextLesson.fields.slug}${queryString}`) View next lesson + diff --git a/views/mixins/_lessonModuleImage.pug b/views/mixins/_lessonModuleImage.pug index f14daa3..db3c413 100644 --- a/views/mixins/_lessonModuleImage.pug +++ b/views/mixins/_lessonModuleImage.pug @@ -1,6 +1,11 @@ mixin lessonModuleImage(module) .lesson-module.lesson-module-image +<<<<<<< Updated upstream if module.fields.file && module.fields.file.url img.lesson-module-image__image(src=module.fields.file.url alt=module.fields.title) else h3 ⚠️ Image missing +======= + img.lesson-module-image__image(src=module.fields.image.fields.file.url alt=module.fields.image.fields.title) + div.lesson-module-image__title #{module.fields.title} +>>>>>>> Stashed changes