mirror of
https://github.com/comatory/fb2iCal
synced 2025-06-05 22:09:25 +02:00
improve error handling
This commit is contained in:
26
lib/index.js
26
lib/index.js
@ -5,6 +5,7 @@ const path = require('path')
|
||||
const crawl = require('./crawler')
|
||||
const parseHTML = require('./parser')
|
||||
const generateICS = require('./ics')
|
||||
const { genericErrorHandler, checkFBURL } = require('./middlewares')
|
||||
|
||||
const port = process.env.PORT
|
||||
const app = express()
|
||||
@ -21,18 +22,16 @@ app.get('*', (req, res) => {
|
||||
res.status(400).render('404')
|
||||
})
|
||||
|
||||
app.get('/error', (req, res) => {
|
||||
res
|
||||
.status(500)
|
||||
.render('error', { error: req.error || '' })
|
||||
})
|
||||
|
||||
app.use('/download', checkFBURL)
|
||||
app.post('/download', async (req, res) => {
|
||||
const { url } = req.body
|
||||
|
||||
if (!/facebook/.test(url)) {
|
||||
return res
|
||||
.status(500)
|
||||
.render(
|
||||
'error',
|
||||
{ error: 'Not Facebook URL!' }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const html = await crawl(url)
|
||||
const data = parseHTML(html)
|
||||
@ -44,15 +43,12 @@ app.post('/download', async (req, res) => {
|
||||
.send(200, new Buffer(ics, 'utf8'))
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return res
|
||||
.status(500)
|
||||
.render('error', { error: err.toString() })
|
||||
return next(err)
|
||||
}
|
||||
|
||||
return res.render('download', { url })
|
||||
})
|
||||
|
||||
app.use(genericErrorHandler)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`App running on port ${port}`)
|
||||
})
|
||||
|
Reference in New Issue
Block a user