From 5fd1470db08ad984386d00ec72e4b82a2c39a8bd Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Thu, 23 Jul 2026 20:34:04 +0200 Subject: [PATCH] Migrate client config and error handling to client-axios --- frontend/src/main.tsx | 16 ++++++++++------ frontend/src/utils.ts | 22 ++++++++++------------ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index 8afe946c..b28a53b8 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -5,21 +5,25 @@ import { QueryClientProvider, } from "@tanstack/react-query" import { createRouter, RouterProvider } from "@tanstack/react-router" +import { AxiosError } from "axios" import { StrictMode } from "react" import ReactDOM from "react-dom/client" -import { ApiError, OpenAPI } from "./client" +import { client } from "./client/client.gen" import { ThemeProvider } from "./components/theme-provider" import { Toaster } from "./components/ui/sonner" import "./index.css" import { routeTree } from "./routeTree.gen" -OpenAPI.BASE = import.meta.env.VITE_API_URL -OpenAPI.TOKEN = async () => { - return localStorage.getItem("access_token") || "" -} +client.setConfig({ + baseURL: import.meta.env.VITE_API_URL, + auth: () => localStorage.getItem("access_token") || "", +}) const handleApiError = (error: Error) => { - if (error instanceof ApiError && [401, 403].includes(error.status)) { + if ( + error instanceof AxiosError && + [401, 403].includes(error.response?.status ?? 0) + ) { localStorage.removeItem("access_token") window.location.href = "/login" } diff --git a/frontend/src/utils.ts b/frontend/src/utils.ts index fa491eb2..967bb471 100644 --- a/frontend/src/utils.ts +++ b/frontend/src/utils.ts @@ -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) }