mirror of
https://github.com/comatory/fb2iCal
synced 2025-06-05 22:09:25 +02:00
improve error handling
This commit is contained in:
33
lib/middlewares.js
Normal file
33
lib/middlewares.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const FBURLError = () => new Error('Not a valid Facebook URL!')
|
||||
|
||||
const genericErrorHandler = (err, req, res, next) => {
|
||||
console.error(err.stack)
|
||||
res
|
||||
.status(500)
|
||||
.render('error', { error: err.toString() })
|
||||
}
|
||||
|
||||
const checkFBURL = (req, res, next) => {
|
||||
const { url } = req.body
|
||||
|
||||
if (!url) {
|
||||
return next(FBURLError())
|
||||
}
|
||||
|
||||
try {
|
||||
const FBURL = new URL(url)
|
||||
if (!(/facebook/.test(FBURL.hostname))) {
|
||||
return next(FBURLError())
|
||||
}
|
||||
} catch (err) {
|
||||
return next(err)
|
||||
}
|
||||
|
||||
|
||||
return next()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
genericErrorHandler,
|
||||
checkFBURL,
|
||||
}
|
Reference in New Issue
Block a user