Files
zyt/admin/src/utils/tuicall-error.ts
T
2026-03-23 18:02:16 +08:00

57 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function collectErrorText(err: unknown): string {
const parts: string[] = []
let cur: unknown = err
let depth = 0
while (cur != null && depth < 6) {
if (typeof cur === 'string') {
parts.push(cur)
break
}
if (typeof cur === 'object') {
const o = cur as Record<string, unknown>
if (typeof o.message === 'string') parts.push(o.message)
if (typeof o.name === 'string') parts.push(o.name)
if (typeof o.code !== 'undefined') parts.push(String(o.code))
cur = o.cause
} else {
parts.push(String(cur))
break
}
depth++
}
return parts.join(' ').toLowerCase()
}
/** 腾讯云通话套餐/能力类错误常见码(如 wasm 日志 error_code:-1001 */
export const TUICALL_PACKAGE_ERROR_CODE = -1001
/** 是否为「套餐不含该能力 / 需续费」类错误(如 TUICallEngineError: The package you purchased does not support this ability */
export function isTUICallPackageAbilityError(err: unknown): boolean {
if (err && typeof err === 'object') {
const o = err as Record<string, unknown>
const code = o.code ?? o.error_code
if (code === TUICALL_PACKAGE_ERROR_CODE || code === String(TUICALL_PACKAGE_ERROR_CODE)) {
return true
}
}
const text = collectErrorText(err)
return (
text.includes('does not support this ability') ||
text.includes('package you purchased') ||
(text.includes('-1001') && text.includes('does not support'))
)
}
/** 用户可见短文案 */
export function getTUICallPackageArrearsMessage(): string {
return '套餐欠费或未开通该能力,请联系管理员在腾讯云续费或升级套餐。'
}
/** 发起通话等失败时:套餐类错误用友好文案,否则返回原始信息 */
export function formatTUICallUserError(err: unknown, defaultMessage = '通话操作失败'): string {
if (isTUICallPackageAbilityError(err)) return getTUICallPackageArrearsMessage()
if (err instanceof Error) return err.message || defaultMessage
if (typeof err === 'string') return err || defaultMessage
return defaultMessage
}