mirror of
https://github.com/fastapi/full-stack-fastapi-template.git
synced 2026-11-03 20:36:27 +00:00
Migrate client config and error handling to client-axios
This commit is contained in:
+10
-12
@@ -1,22 +1,20 @@
|
||||
import { AxiosError } from "axios"
|
||||
import type { ApiError } from "./client"
|
||||
|
||||
function extractErrorMessage(err: ApiError): string {
|
||||
function extractErrorMessage(err: Error): string {
|
||||
if (err instanceof AxiosError) {
|
||||
const errDetail = (err.response?.data as any)?.detail
|
||||
if (Array.isArray(errDetail) && errDetail.length > 0) {
|
||||
return errDetail[0].msg
|
||||
}
|
||||
if (typeof errDetail === "string") {
|
||||
return errDetail
|
||||
}
|
||||
return err.message
|
||||
}
|
||||
|
||||
const errDetail = (err.body as any)?.detail
|
||||
if (Array.isArray(errDetail) && errDetail.length > 0) {
|
||||
return errDetail[0].msg
|
||||
}
|
||||
return errDetail || "Something went wrong."
|
||||
return "Something went wrong."
|
||||
}
|
||||
|
||||
export const handleError = function (
|
||||
this: (msg: string) => void,
|
||||
err: ApiError,
|
||||
) {
|
||||
export const handleError = function (this: (msg: string) => void, err: Error) {
|
||||
const errorMessage = extractErrorMessage(err)
|
||||
this(errorMessage)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user