feat(api): surface backend error messages globally via toast

Show the backend's Persian error (errors[0].message, e.g. rate-limit
'درخواست‌های زیاد') as a toast for any failed request.* call, from the
axios response interceptor — so failures are no longer silent. 401 still
logs out/redirects without a toast; callers can opt out with
config.skipErrorToast. Drop now-redundant per-caller alerts/toasts in
the booking submit, payment, and OTP userinfo paths.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-06-15 18:25:11 +03:30
co-authored by Claude Opus 4.8
parent 08f5829a22
commit 9912ae06ca
4 changed files with 26 additions and 13 deletions
@@ -127,7 +127,6 @@ function SubmitData({ setStep, data, prevData, setErrors, doctor, isForAnother,
} catch (error) { } catch (error) {
setLoading(false); setLoading(false);
if (error?.response?.status === 409) { if (error?.response?.status === 409) {
alert("این زمان همین لحظه توسط کاربر دیگری رزرو شد. لطفاً ساعت دیگری انتخاب کنید.");
setStep(0); setStep(0);
return; return;
} }
-1
View File
@@ -63,7 +63,6 @@ function Paying({ setStep, appointmentId, expiresAt, doctor, selectedSlot, selec
} }
} catch (error) { } catch (error) {
console.error("Payment error:", error); console.error("Payment error:", error);
alert("خطا در پرداخت");
setLoading(false); setLoading(false);
} }
}; };
@@ -103,7 +103,6 @@ function SendReq({
.catch(() => { .catch(() => {
setLoading(false); setLoading(false);
setIsError(true); setIsError(true);
toast.error("دریافت اطلاعات کاربر ناموفق بود. دوباره تلاش کنید.");
}); });
}; };
+26 -10
View File
@@ -1,5 +1,17 @@
import axios from "axios"; import axios from "axios";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { toast } from "react-toastify";
function extractErrorMessage(error) {
const errors = error?.response?.data?.errors;
if (Array.isArray(errors) && errors.length > 0 && errors[0]?.message) {
return errors[0].message;
}
if (error?.response?.data?.message) {
return error.response.data.message;
}
return "خطایی رخ داد. لطفاً دوباره تلاش کنید.";
}
const BASE_URL = process.env.NEXT_PUBLIC_API_URL; const BASE_URL = process.env.NEXT_PUBLIC_API_URL;
@@ -33,16 +45,20 @@ api.interceptors.response.use(
return response.data; return response.data;
}, },
(error) => { (error) => {
if (error?.response?.status === 401 && typeof window !== "undefined") { if (typeof window !== "undefined") {
const hostname = window.location.hostname; if (error?.response?.status === 401) {
const domain = hostname.includes("localhost") const hostname = window.location.hostname;
? undefined const domain = hostname.includes("localhost")
: `.${hostname.split(".").slice(-2).join(".")}`; ? undefined
const opts = { path: "/", ...(domain && { domain }) }; : `.${hostname.split(".").slice(-2).join(".")}`;
["access_token", "refresh_token", "uuid", "userInfo"].forEach((key) => { const opts = { path: "/", ...(domain && { domain }) };
Cookies.remove(key, opts); ["access_token", "refresh_token", "uuid", "userInfo"].forEach((key) => {
}); Cookies.remove(key, opts);
window.location.href = "/login"; });
window.location.href = "/login";
} else if (!error?.config?.skipErrorToast) {
toast.error(extractErrorMessage(error));
}
} }
return Promise.reject(error); return Promise.reject(error);
} }