Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
idranme committed Dec 24, 2024
1 parent ef30303 commit 27271f8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/onebot11/action/BaseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class BaseAction<PayloadType, ReturnDataType> {
return OB11Response.ok(resData)
} catch (e) {
this.ctx.logger.error('发生错误', e)
return OB11Response.error((e as Error)?.toString() || (e as Error)?.stack?.toString() || '未知错误,可能操作超时', 200)
return OB11Response.error((e as Error)?.toString() ?? String(e), 200)
}
}

Expand All @@ -41,7 +41,7 @@ abstract class BaseAction<PayloadType, ReturnDataType> {
return OB11Response.ok(resData, echo)
} catch (e) {
this.ctx.logger.error('发生错误', e)
return OB11Response.error((e as Error)?.stack?.toString() || String(e), 1200, echo)
return OB11Response.error((e as Error)?.toString() ?? String(e), 1200, echo)
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/onebot11/connect/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ class OB11Http {
this.ctx.logger.info('收到 HTTP 请求', req.url, payload)
const action = this.config.actionMap.get(req.path.replaceAll('/', ''))
if (action) {
try {
res.json(await action.handle(payload))
} catch (e) {
res.json(OB11Response.error((e as Error).stack!.toString(), 200))
}
res.json(await action.handle(payload))
} else {
res.status(404).json(OB11Response.error('API 不存在', 404))
}
Expand Down
17 changes: 4 additions & 13 deletions src/onebot11/connect/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,8 @@ class OB11WebSocket {
if (!action) {
return this.reply(socket, OB11Response.error('不支持的api ' + receive.action, 1404, receive.echo))
}
try {
const handleResult = await action.websocketHandle(receive.params, receive.echo)
handleResult.echo = receive.echo
this.reply(socket, handleResult)
} catch (e) {
this.reply(socket, OB11Response.error(`api处理出错:${(e as Error).stack}`, 1200, receive.echo))
}
const handleResult = await action.websocketHandle(receive.params, receive.echo)
this.reply(socket, handleResult)
}

private connect(socket: WebSocket, req: IncomingMessage) {
Expand Down Expand Up @@ -224,12 +219,8 @@ class OB11WebSocketReverse {
if (!action) {
return this.reply(this.wsClient!, OB11Response.error('不支持的api ' + receive.action, 1404, receive.echo))
}
try {
const handleResult = await action.websocketHandle(receive.params, receive.echo)
this.reply(this.wsClient!, handleResult)
} catch (e) {
this.reply(this.wsClient!, OB11Response.error(`api处理出错:${e}`, 1200, receive.echo))
}
const handleResult = await action.websocketHandle(receive.params, receive.echo)
this.reply(this.wsClient!, handleResult)
}

private tryConnect() {
Expand Down

0 comments on commit 27271f8

Please sign in to comment.