1
This commit is contained in:
+9
-4
@@ -144,6 +144,7 @@ export async function apiRequest<T>(config: AxiosRequestConfig) {
|
||||
export async function apiBlobRequest(config: AxiosRequestConfig) {
|
||||
try {
|
||||
const response = await transport.request<Blob>({ ...config, responseType: 'blob' })
|
||||
await throwBlobApiError(response.data, response.status)
|
||||
return response.data
|
||||
} catch (error) {
|
||||
await throwBlobApiError(error)
|
||||
@@ -154,6 +155,7 @@ export async function apiBlobRequest(config: AxiosRequestConfig) {
|
||||
export async function apiBlobResponseRequest(config: AxiosRequestConfig) {
|
||||
try {
|
||||
const response = await transport.request<Blob>({ ...config, responseType: 'blob' })
|
||||
await throwBlobApiError(response.data, response.status)
|
||||
return { blob: response.data, headers: response.headers }
|
||||
} catch (error) {
|
||||
await throwBlobApiError(error)
|
||||
@@ -161,16 +163,19 @@ export async function apiBlobResponseRequest(config: AxiosRequestConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
async function throwBlobApiError(error: unknown) {
|
||||
if (!axios.isAxiosError(error) || !(error.response?.data instanceof Blob)) return
|
||||
async function throwBlobApiError(error: unknown, responseStatus?: number) {
|
||||
const blob = error instanceof Blob
|
||||
? error
|
||||
: axios.isAxiosError(error) && error.response?.data instanceof Blob ? error.response.data : null
|
||||
if (!blob || !blob.type.toLowerCase().includes('json')) return
|
||||
try {
|
||||
const body = JSON.parse(await error.response.data.text()) as unknown
|
||||
const body = JSON.parse(await blob.text()) as unknown
|
||||
if (!isApiEnvelope(body)) return
|
||||
const fieldErrors = asFieldErrors(body.data)
|
||||
throw new ApiError(
|
||||
apiErrorMessage(body.message, fieldErrors),
|
||||
body.code,
|
||||
error.response.status,
|
||||
responseStatus ?? (axios.isAxiosError(error) ? error.response?.status : undefined),
|
||||
fieldErrors,
|
||||
typeof body.traceId === 'string' ? body.traceId : null,
|
||||
typeof body.requestId === 'string' ? body.requestId : null,
|
||||
|
||||
Reference in New Issue
Block a user