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) {
setLoading(false);
if (error?.response?.status === 409) {
alert("این زمان همین لحظه توسط کاربر دیگری رزرو شد. لطفاً ساعت دیگری انتخاب کنید.");
setStep(0);
return;
}
-1
View File
@@ -63,7 +63,6 @@ function Paying({ setStep, appointmentId, expiresAt, doctor, selectedSlot, selec
}
} catch (error) {
console.error("Payment error:", error);
alert("خطا در پرداخت");
setLoading(false);
}
};
@@ -103,7 +103,6 @@ function SendReq({
.catch(() => {
setLoading(false);
setIsError(true);
toast.error("دریافت اطلاعات کاربر ناموفق بود. دوباره تلاش کنید.");
});
};
+26 -10
View File
@@ -1,5 +1,17 @@
import axios from "axios";
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;
@@ -33,16 +45,20 @@ api.interceptors.response.use(
return response.data;
},
(error) => {
if (error?.response?.status === 401 && typeof window !== "undefined") {
const hostname = window.location.hostname;
const domain = hostname.includes("localhost")
? undefined
: `.${hostname.split(".").slice(-2).join(".")}`;
const opts = { path: "/", ...(domain && { domain }) };
["access_token", "refresh_token", "uuid", "userInfo"].forEach((key) => {
Cookies.remove(key, opts);
});
window.location.href = "/login";
if (typeof window !== "undefined") {
if (error?.response?.status === 401) {
const hostname = window.location.hostname;
const domain = hostname.includes("localhost")
? undefined
: `.${hostname.split(".").slice(-2).join(".")}`;
const opts = { path: "/", ...(domain && { domain }) };
["access_token", "refresh_token", "uuid", "userInfo"].forEach((key) => {
Cookies.remove(key, opts);
});
window.location.href = "/login";
} else if (!error?.config?.skipErrorToast) {
toast.error(extractErrorMessage(error));
}
}
return Promise.reject(error);
}