Add global error handler

pull/282/head
amio 2019-05-14 10:44:05 +08:00
rodzic 85cb44f375
commit 86b2594c12
2 zmienionych plików z 21 dodań i 11 usunięć

Wyświetl plik

@ -2,7 +2,7 @@ import got from '../libs/got'
import { millify, stars, version, versionColor } from '../libs/utils'
import { badgenServe } from '../libs/badgen-serve'
export const help = ``
export const title = 'Mozilla Add-on'
export const examples = [
'/amo/v/markdown-viewer-chrome',

Wyświetl plik

@ -4,24 +4,34 @@ import http from 'http'
import serve404 from './libs/serve-404'
const sendError = (req, res, error) => {
res.statusCode = 500
res.end(error.message)
}
const badgeHandlers = fs.readdirSync(path.join(__dirname, 'endpoints'))
.filter(name => !name.startsWith('_'))
.map(name => name.replace(/\.ts$/, ''))
module.exports = http.createServer(async (req, res) => {
const handler = badgeHandlers.find(h => req.url!.startsWith(`/${h}`))
const server = http.createServer(async (req, res) => {
const handlerName = badgeHandlers.find(h => req.url!.startsWith(`/${h}`))
if (handler) {
return import(path.join(__dirname, 'endpoints', handler)).then(h => {
h.default(req, res)
})
} else {
return serve404(req, res)
try {
if (handlerName) {
const handlerPath = path.join(__dirname, 'endpoints', handlerName)
const { default: handler } = await import(handlerPath)
return handler(req, res)
}
} catch (error) {
console.error(error)
return sendError(req, res, error)
}
return serve404(req, res)
})
if (require.main === module) {
module.exports.listen(3000)
server.listen(3000)
}
process.on('unhandledRejection', err => console.error(err.message, err.stack))
export default server