Skip to content

Commit cbfb714

Browse files
committed
♻️ Refactor error handling in utils
1 parent c30949e commit cbfb714

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

frontend/src/utils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import { AxiosError } from "axios"
12
import type { ApiError } from "./client"
2-
import useCustomToast from "./hooks/useCustomToast"
33

4-
export const handleError = (err: ApiError) => {
5-
const { showErrorToast } = useCustomToast()
4+
function extractErrorMessage(err: ApiError): string {
5+
if (err instanceof AxiosError) {
6+
return err.message
7+
}
8+
69
const errDetail = (err.body as any)?.detail
7-
let errorMessage = errDetail || "Something went wrong."
810
if (Array.isArray(errDetail) && errDetail.length > 0) {
9-
errorMessage = errDetail[0].msg
11+
return errDetail[0].msg
1012
}
11-
showErrorToast(errorMessage)
13+
return errDetail || "Something went wrong."
14+
}
15+
16+
export const handleError = function (this: (msg: string) => void, err: ApiError) {
17+
const errorMessage = extractErrorMessage(err)
18+
this(errorMessage)
1219
}

0 commit comments

Comments
 (0)