xtraderClient/docs/composables/useAuthError.md

37 lines
1.1 KiB
Markdown
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.

# useAuthError.ts
**路径**`src/composables/useAuthError.ts`
## 功能用途
统一处理 HTTP 401Unauthorized等权限相关错误根据登录状态返回用户友好的提示文案未登录时提示「请先登录」已登录时提示「权限不足」。
## 核心能力
- `formatAuthError(err, fallback)`:将异常转换为展示文案
- 若错误信息包含 `401``Unauthorized`:根据 `userStore.isLoggedIn` 返回 `error.pleaseLogin``error.insufficientPermission`
- 否则返回原始错误信息或 `fallback`
## 使用方式
```typescript
import { useAuthError } from '@/composables/useAuthError'
const { formatAuthError } = useAuthError()
try {
await someApiCall()
} catch (e) {
errorMessage.value = formatAuthError(e, t('error.requestFailed'))
}
```
## 国际化
依赖 `error.pleaseLogin``error.insufficientPermission`,在各 `locales/*.json``error` 下配置。
## 扩展方式
1. **扩展状态码**:在 `formatAuthError` 中增加对 403 等的判断
2. **统一拦截**:在 `request.ts` 层统一处理 401自动跳转登录或弹 toast