Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
perf: ⚡️ 报错过滤有价值信息提示到接口
Browse files Browse the repository at this point in the history
  • Loading branch information
danni-cool committed May 20, 2024
1 parent 9a24f20 commit 7429a1c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/service/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module.exports.formatAndSendMsg = async function ({
}
success = true
} catch (/** @type {any} */ e) {
error = e
error = Utils.filterUseFulHttpError(e)
Utils.logger.error(e)
}

Expand Down
6 changes: 3 additions & 3 deletions src/service/msgSender.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ const handleMsg2Single = async function (body, { bot, messageReceiver }) {
})
msgArr[i].success = success
if (!success) {
msgArr[i].error = error.toString()
msgArr[i].error = error
}
}

Expand All @@ -419,7 +419,7 @@ const handleMsg2Single = async function (body, { bot, messageReceiver }) {
: null
}
} else if (!Array.isArray(payload.data)) {
const { success } = await formatAndSendMsg({
const { success, error } = await formatAndSendMsg({
isRoom,
bot,
msgData: payload.data,
Expand All @@ -435,7 +435,7 @@ const handleMsg2Single = async function (body, { bot, messageReceiver }) {
: {
to,
...(isRoom !== undefined ? { isRoom } : {}),
data: [payload.data]
data: [{ ...payload.data, error }]
}
}
}
Expand Down
35 changes: 26 additions & 9 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,31 @@ const sleep = async (ms) => {
}

/**
* 删除登录缓存文件
* 过滤有价值的发送消息报错信息
* @param {*} error
*/
// const deleteMemoryCard = () => {
// //@ts-expect-errors 必定是 pathlike
// if (fs.existsSync(memoryCardPath)) {
// //@ts-expect-errors 必定是 pathlike
// fs.unlinkSync(memoryCardPath)
// }
// }
const filterUseFulHttpError = (error) => {
let newErrorObj = {}

if (error.tips) {
newErrorObj.tips = error.tips
newErrorObj.code = error.code
newErrorObj.stack = error.stack

if (error.response) {
newErrorObj.response = {}
newErrorObj.response.status = error.response.status
newErrorObj.response.statusText = error.response.statusText
newErrorObj.response.url = error.response.url
newErrorObj.response.method = error.response.method
newErrorObj.response.method = error.response.method
newErrorObj.response.data = error.response.data
}
return newErrorObj
} else {
return error
}
}

module.exports = {
...require('./msg.js'),
Expand All @@ -262,5 +278,6 @@ module.exports = {
parseJsonLikeStr,
tryConvertCnCharToUtf8Char,
sleep,
Defer
Defer,
filterUseFulHttpError
}
7 changes: 5 additions & 2 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type pushMsgUnitTypeOpt = {

type msgData = {
success?: boolean
error?: string
error?: never
} & pushMsgUnitTypeOpt

type toType = string | { alias: string }
Expand Down Expand Up @@ -104,7 +104,10 @@ type msg2SingleRejectReason = {
type failedTaskType = {
to: toType
isRoom?: boolean
data: pushMsgUnitTypeOpt | pushMsgUnitTypeOpt[] | []
data:
| (pushMsgUnitTypeOpt & { error?: never })
| (pushMsgUnitTypeOpt & { error?: never })[]
| []
}

type sendingTaskType = {
Expand Down

0 comments on commit 7429a1c

Please sign in to comment.