mirror of
https://github.com/fastapi/full-stack-fastapi-template.git
synced 2026-11-03 20:36:27 +00:00
30 lines
726 B
TypeScript
30 lines
726 B
TypeScript
import { AxiosError } from "axios"
|
|
|
|
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
|
|
}
|
|
return "Something went wrong."
|
|
}
|
|
|
|
export const handleError = function (this: (msg: string) => void, err: Error) {
|
|
const errorMessage = extractErrorMessage(err)
|
|
this(errorMessage)
|
|
}
|
|
|
|
export const getInitials = (name: string): string => {
|
|
return name
|
|
.split(" ")
|
|
.slice(0, 2)
|
|
.map((word) => word[0])
|
|
.join("")
|
|
.toUpperCase()
|
|
}
|