fix(auth): 统一认证响应与异常处理
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
|
||||||
|
import { ApiResponseError, unwrapApiResponse } from '@/utils/api-response'
|
||||||
import { request } from '@/utils/request'
|
import { request } from '@/utils/request'
|
||||||
import type {
|
import type {
|
||||||
AuthenticatedUser,
|
AuthenticatedUser,
|
||||||
@@ -9,33 +10,7 @@ import type {
|
|||||||
} from '@/types/auth'
|
} from '@/types/auth'
|
||||||
import type { ApiResponse } from '@/types/common'
|
import type { ApiResponse } from '@/types/common'
|
||||||
|
|
||||||
export class ApiResponseError extends Error {
|
export { ApiResponseError } from '@/utils/api-response'
|
||||||
readonly code: number | string
|
|
||||||
readonly traceId?: string
|
|
||||||
|
|
||||||
constructor(response: ApiResponse<unknown>) {
|
|
||||||
super(response.message || '接口请求失败')
|
|
||||||
this.name = 'ApiResponseError'
|
|
||||||
this.code = response.code
|
|
||||||
this.traceId = response.traceId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isSuccessCode(code: number | string) {
|
|
||||||
return code === 0 || code === '0'
|
|
||||||
}
|
|
||||||
|
|
||||||
function unwrapApiResponse<T>(response: ApiResponse<T>): T {
|
|
||||||
if (!isSuccessCode(response.code)) {
|
|
||||||
throw new ApiResponseError(response)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.data === null) {
|
|
||||||
throw new Error(response.message || '接口未返回业务数据')
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.data
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizePermissions(value: unknown): string[] {
|
function normalizePermissions(value: unknown): string[] {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
|
|||||||
37
clinical-web/src/utils/api-response.ts
Normal file
37
clinical-web/src/utils/api-response.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import type { ApiResponse } from '@/types/common'
|
||||||
|
|
||||||
|
export class ApiResponseError extends Error {
|
||||||
|
readonly code: number | string
|
||||||
|
readonly traceId?: string
|
||||||
|
|
||||||
|
constructor(response: ApiResponse<unknown>) {
|
||||||
|
super(response.message || '接口请求失败')
|
||||||
|
this.name = 'ApiResponseError'
|
||||||
|
this.code = response.code
|
||||||
|
this.traceId = response.traceId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isSuccessCode(code: number | string) {
|
||||||
|
return code === 0 || code === '0'
|
||||||
|
}
|
||||||
|
|
||||||
|
export function unwrapApiResponse<T>(response: ApiResponse<T>): T {
|
||||||
|
if (!isSuccessCode(response.code)) {
|
||||||
|
throw new ApiResponseError(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.data === null) {
|
||||||
|
throw new Error(response.message || '接口未返回业务数据')
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data
|
||||||
|
}
|
||||||
|
|
||||||
|
export function unwrapNullableApiResponse<T>(response: ApiResponse<T | null>): T | null {
|
||||||
|
if (!isSuccessCode(response.code)) {
|
||||||
|
throw new ApiResponseError(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user