forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
31 lines (27 loc) · 706 Bytes
/
utils.ts
File metadata and controls
31 lines (27 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { AxiosError } from "axios"
import type { ApiError } from "./client"
function extractErrorMessage(err: ApiError): string {
if (err instanceof AxiosError) {
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."
}
export const handleError = function (
this: (msg: string) => void,
err: ApiError,
) {
const errorMessage = extractErrorMessage(err)
this(errorMessage)
}
export const getInitials = (name: string): string => {
return name
.split(" ")
.slice(0, 2)
.map((word) => word[0])
.join("")
.toUpperCase()
}