feat(Errors): Add global error handling
This commit is contained in:
committed by
Benedikt Rötsch
parent
17b52868e2
commit
64f59e203d
@@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
exports.catchErrors = (fn) => {
|
exports.catchErrors = (fn) => {
|
||||||
return function (req, res, next) {
|
return function (req, res, next) {
|
||||||
return fn(req, res, next).catch(next)
|
return fn(req, res, next).catch((e) => {
|
||||||
|
next(e)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ router.get('/:slug', catchErrors(async function (req, res, next) {
|
|||||||
res.render('course', {title: course.fields.title, course, lesson, lessons, lessonIndex, visitedLessons})
|
res.render('course', {title: course.fields.title, course, lesson, lessons, lessonIndex, visitedLessons})
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
/* GET course lesson detail. */
|
/* GET course lesson detail. */
|
||||||
router.get('/:cslug/lessons/:lslug', catchErrors(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)
|
let course = await getCourse(req.params.cslug, req.query.locale, req.query.api)
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ exports.initClient = (options) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getCourses = (locale = 'en-US', api = `cda`) => {
|
exports.getCourses = assert((locale = 'en-US', api = `cda`) => {
|
||||||
// to get all the courses we request all the entries
|
// to get all the courses we request all the entries
|
||||||
// with the content_type `course` from Contentful
|
// with the content_type `course` from Contentful
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({content_type: 'course', locale, include: 10})
|
return client.getEntries({content_type: 'course', locale, include: 10})
|
||||||
.then((response) => response.items)
|
.then((response) => response.items)
|
||||||
}
|
}, 'Course')
|
||||||
|
|
||||||
exports.getLandingPage = (locale = 'en-US', api = `cda`) => {
|
exports.getLandingPage = (locale = 'en-US', api = `cda`) => {
|
||||||
// our Home page is fully configureable via Contentful
|
// our Home page is fully configureable via Contentful
|
||||||
@@ -39,26 +39,22 @@ exports.getLandingPage = (locale = 'en-US', api = `cda`) => {
|
|||||||
.then((response) => response.items[0])
|
.then((response) => response.items[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getCourse = (slug, locale = 'en-US', api = `cda`) => {
|
exports.getCourse = assert((slug, locale = 'en-US', api = `cda`) => {
|
||||||
// the SDK supports link resolution only when you call the collection endpoints
|
// the SDK supports link resolution only when you call the collection endpoints
|
||||||
// That's why we are using getEntries with a query instead of getEntry(entryId)
|
// That's why we are using getEntries with a query instead of getEntry(entryId)
|
||||||
// make sure to specify the content_type whenever you want to perform a query
|
// make sure to specify the content_type whenever you want to perform a query
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({content_type: 'course', 'fields.slug': slug, locale, include: 10})
|
return client.getEntries({content_type: 'course', 'fields.slug': slug, locale, include: 10})
|
||||||
.then((response) => response.items[0])
|
.then((response) => response.items[0])
|
||||||
}
|
}, 'Course')
|
||||||
|
|
||||||
exports.getLessons = (courseId, locale = 'en-US', api = `cda`) => {
|
exports.getCategories = assert((locale = 'en-US', api = `cda`) => {
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getCategories = (locale = 'en-US', api = `cda`) => {
|
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({content_type: 'category', locale})
|
return client.getEntries({content_type: 'category', locale})
|
||||||
.then((response) => response.items)
|
.then((response) => response.items)
|
||||||
}
|
}, 'Course')
|
||||||
|
|
||||||
exports.getCoursesByCategory = (category, locale = 'en-US', api = `cda`) => {
|
exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`) => {
|
||||||
const client = api === 'cda' ? cdaClient : cpaClient
|
const client = api === 'cda' ? cdaClient : cpaClient
|
||||||
return client.getEntries({
|
return client.getEntries({
|
||||||
content_type: 'course',
|
content_type: 'course',
|
||||||
@@ -67,5 +63,18 @@ exports.getCoursesByCategory = (category, locale = 'en-US', api = `cda`) => {
|
|||||||
include: 10
|
include: 10
|
||||||
})
|
})
|
||||||
.then((response) => response.items)
|
.then((response) => response.items)
|
||||||
}
|
}, 'Category')
|
||||||
|
|
||||||
|
function assert (fn, context) {
|
||||||
|
return function (req, res, next) {
|
||||||
|
return fn(req, res, next)
|
||||||
|
.then((data) => {
|
||||||
|
if (!data) {
|
||||||
|
var err = new Error(`${context} Not Found`)
|
||||||
|
err.status = 404
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,15 @@ extends layout
|
|||||||
|
|
||||||
block content
|
block content
|
||||||
.layout-centered
|
.layout-centered
|
||||||
h1= message
|
.error
|
||||||
h2= error.status
|
h1 Oops Something went wrong (#{error.status})
|
||||||
pre #{error.stack}
|
h2 Try:
|
||||||
|
ul
|
||||||
|
li Check if the content model has changed
|
||||||
|
li Check the selection has content in draft or published state (for Preview or Delivery)
|
||||||
|
li Check if there's any content for this locale
|
||||||
|
li Verify credentials are correct and up to date
|
||||||
|
li Check the stack trace below
|
||||||
|
|
||||||
|
h2 Stack trace
|
||||||
|
pre.error__stack-trace #{error.stack}
|
||||||
|
|||||||
Reference in New Issue
Block a user