Skip to content

Commit 0719c77

Browse files
committed
misc: support express error handle
1 parent 83a8f3b commit 0719c77

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

.github/workflows/ci.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ jobs:
1010
uses: actions/checkout@v2
1111
with:
1212
persist-credentials: false
13-
- name: Use Node.js 14.x
13+
- name: Use Node.js 16.x
1414
uses: actions/setup-node@v1
1515
with:
16-
node-version: 14.x
16+
node-version: 16.x
1717
- run: yarn
1818
- run: yarn build

src/apps/bot.ts

+57-49
Original file line numberDiff line numberDiff line change
@@ -8,65 +8,73 @@ const createApp = (handle: Function, conf: BotConfig) => {
88
app.use(express.json())
99
app.use(express.urlencoded({ extended: true }))
1010
const {Bot} = conf.models || {}
11-
app.all('/oauth', async (req, res) => {
12-
const bot = (await (Bot as any).init({
13-
code: req.query.code,
14-
token: req.body,
15-
creator_extension_id: req.query.creator_extension_id,
16-
creator_account_id: req.query.creator_account_id,
17-
})) as BotType;
18-
await bot.setupWebHook(); // this might take a while, depends on when the bot user is ready
19-
await handle({type: 'BotAdded', bot});
20-
res.status(200);
21-
res.send('ok'); // return string to fix issue for serverless-offline
11+
app.all('/oauth', async (req, res, next) => {
12+
try {
13+
const bot = (await (Bot as any).init({
14+
code: req.query.code,
15+
token: req.body,
16+
creator_extension_id: req.query.creator_extension_id,
17+
creator_account_id: req.query.creator_account_id,
18+
})) as BotType;
19+
await bot.setupWebHook(); // this might take a while, depends on when the bot user is ready
20+
await handle({type: 'BotAdded', bot});
21+
res.status(200);
22+
res.send('ok'); // return string to fix issue for serverless-offline
23+
} catch (e) {
24+
next(e);
25+
}
2226
});
2327

24-
app.post('/webhook', async (req, res) => {
28+
app.post('/webhook', async (req, res, next) => {
2529
const message = req.body;
2630
if (process.env.LOG_LEVEL === 'DEBUG') {
2731
console.log('WebHook payload:', JSON.stringify(message, null, 2));
2832
}
29-
const body = message.body;
30-
if (body) {
31-
switch (body.eventType) {
32-
case 'Delete': {
33-
const deleteBot = await botDeleted(Bot, message);
34-
await handle({type: 'BotRemoved', bot: deleteBot});
35-
break;
36-
}
37-
case 'PostAdded': {
38-
const result = await postAdded(Bot, message, conf);
39-
if (result) {
40-
await handle({type: 'Message4Bot', ...result});
33+
try {
34+
const body = message.body;
35+
if (body) {
36+
switch (body.eventType) {
37+
case 'Delete': {
38+
const deleteBot = await botDeleted(Bot, message);
39+
await handle({type: 'BotRemoved', bot: deleteBot});
40+
break;
4141
}
42-
break;
43-
}
44-
case 'GroupLeft':
45-
const info = await groupLeft(message);
46-
await handle({
47-
type: 'BotGroupLeft',
48-
...info
49-
});
50-
break;
51-
case 'GroupJoined': {
52-
const botId = message.ownerId;
53-
const joinGroupBot = await Bot.findByPk(botId);
54-
const groupId = message.body.id;
55-
const groupType = message.body.type;
56-
await handle({
57-
type: 'BotJoinGroup',
58-
bot: joinGroupBot,
59-
group: {id: groupId, type: groupType },
60-
});
61-
break;
42+
case 'PostAdded': {
43+
const result = await postAdded(Bot, message, conf);
44+
if (result) {
45+
await handle({type: 'Message4Bot', ...result});
46+
}
47+
break;
48+
}
49+
case 'GroupLeft':
50+
const info = await groupLeft(message);
51+
await handle({
52+
type: 'BotGroupLeft',
53+
...info
54+
});
55+
break;
56+
case 'GroupJoined': {
57+
const botId = message.ownerId;
58+
const joinGroupBot = await Bot.findByPk(botId);
59+
const groupId = message.body.id;
60+
const groupType = message.body.type;
61+
await handle({
62+
type: 'BotJoinGroup',
63+
bot: joinGroupBot,
64+
group: {id: groupId, type: groupType },
65+
});
66+
break;
67+
}
68+
default:
69+
break;
6270
}
63-
default:
64-
break;
71+
await handle({type: body.eventType, message});
6572
}
66-
await handle({type: body.eventType, message});
73+
res.header('Validation-Token', req.header('Validation-Token'));
74+
res.json({ result: 'ok' });
75+
} catch (e) {
76+
next(e);
6777
}
68-
res.header('Validation-Token', req.header('Validation-Token'));
69-
res.json({ result: 'ok' });
7078
});
7179

7280
return app;

0 commit comments

Comments
 (0)