feat(handlers): Add generic error handler

This commit is contained in:
Khaled Garbaya
2017-10-04 10:36:04 +02:00
committed by Benedikt Rötsch
parent 01fd3cc9b3
commit 67fc0edf62
6 changed files with 42 additions and 30 deletions

12
handlers/errorHandlers.js Normal file
View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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