From 0719c777d69c4e9f46c84e5acb9d1c628a800186 Mon Sep 17 00:00:00 2001 From: Embbnux Ji Date: Wed, 26 Jun 2024 17:54:52 +0800 Subject: [PATCH] misc: support express error handle --- .github/workflows/ci.yaml | 4 +- src/apps/bot.ts | 106 ++++++++++++++++++++------------------ 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f9fc61e..d89b833 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,9 +10,9 @@ jobs: uses: actions/checkout@v2 with: persist-credentials: false - - name: Use Node.js 14.x + - name: Use Node.js 16.x uses: actions/setup-node@v1 with: - node-version: 14.x + node-version: 16.x - run: yarn - run: yarn build diff --git a/src/apps/bot.ts b/src/apps/bot.ts index 4d4311f..1b76efa 100644 --- a/src/apps/bot.ts +++ b/src/apps/bot.ts @@ -8,65 +8,73 @@ const createApp = (handle: Function, conf: BotConfig) => { app.use(express.json()) app.use(express.urlencoded({ extended: true })) const {Bot} = conf.models || {} - app.all('/oauth', async (req, res) => { - const bot = (await (Bot as any).init({ - code: req.query.code, - token: req.body, - creator_extension_id: req.query.creator_extension_id, - creator_account_id: req.query.creator_account_id, - })) as BotType; - await bot.setupWebHook(); // this might take a while, depends on when the bot user is ready - await handle({type: 'BotAdded', bot}); - res.status(200); - res.send('ok'); // return string to fix issue for serverless-offline + app.all('/oauth', async (req, res, next) => { + try { + const bot = (await (Bot as any).init({ + code: req.query.code, + token: req.body, + creator_extension_id: req.query.creator_extension_id, + creator_account_id: req.query.creator_account_id, + })) as BotType; + await bot.setupWebHook(); // this might take a while, depends on when the bot user is ready + await handle({type: 'BotAdded', bot}); + res.status(200); + res.send('ok'); // return string to fix issue for serverless-offline + } catch (e) { + next(e); + } }); - app.post('/webhook', async (req, res) => { + app.post('/webhook', async (req, res, next) => { const message = req.body; if (process.env.LOG_LEVEL === 'DEBUG') { console.log('WebHook payload:', JSON.stringify(message, null, 2)); } - const body = message.body; - if (body) { - switch (body.eventType) { - case 'Delete': { - const deleteBot = await botDeleted(Bot, message); - await handle({type: 'BotRemoved', bot: deleteBot}); - break; - } - case 'PostAdded': { - const result = await postAdded(Bot, message, conf); - if (result) { - await handle({type: 'Message4Bot', ...result}); + try { + const body = message.body; + if (body) { + switch (body.eventType) { + case 'Delete': { + const deleteBot = await botDeleted(Bot, message); + await handle({type: 'BotRemoved', bot: deleteBot}); + break; } - break; - } - case 'GroupLeft': - const info = await groupLeft(message); - await handle({ - type: 'BotGroupLeft', - ...info - }); - break; - case 'GroupJoined': { - const botId = message.ownerId; - const joinGroupBot = await Bot.findByPk(botId); - const groupId = message.body.id; - const groupType = message.body.type; - await handle({ - type: 'BotJoinGroup', - bot: joinGroupBot, - group: {id: groupId, type: groupType }, - }); - break; + case 'PostAdded': { + const result = await postAdded(Bot, message, conf); + if (result) { + await handle({type: 'Message4Bot', ...result}); + } + break; + } + case 'GroupLeft': + const info = await groupLeft(message); + await handle({ + type: 'BotGroupLeft', + ...info + }); + break; + case 'GroupJoined': { + const botId = message.ownerId; + const joinGroupBot = await Bot.findByPk(botId); + const groupId = message.body.id; + const groupType = message.body.type; + await handle({ + type: 'BotJoinGroup', + bot: joinGroupBot, + group: {id: groupId, type: groupType }, + }); + break; + } + default: + break; } - default: - break; + await handle({type: body.eventType, message}); } - await handle({type: body.eventType, message}); + res.header('Validation-Token', req.header('Validation-Token')); + res.json({ result: 'ok' }); + } catch (e) { + next(e); } - res.header('Validation-Token', req.header('Validation-Token')); - res.json({ result: 'ok' }); }); return app;