feat: Improve error handling in ContactPage and update Axios headers management
This commit is contained in:
@@ -2,6 +2,7 @@ import { useState } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Helmet } from 'react-helmet-async';
|
import { Helmet } from 'react-helmet-async';
|
||||||
import { Mail, Send, CheckCircle, AlertCircle, Loader2 } from 'lucide-react';
|
import { Mail, Send, CheckCircle, AlertCircle, Loader2 } from 'lucide-react';
|
||||||
|
import { isAxiosError } from 'axios';
|
||||||
import SEOHead from '@/components/seo/SEOHead';
|
import SEOHead from '@/components/seo/SEOHead';
|
||||||
import { generateWebPage, getSiteOrigin } from '@/utils/seo';
|
import { generateWebPage, getSiteOrigin } from '@/utils/seo';
|
||||||
import { getApiClient } from '@/services/api';
|
import { getApiClient } from '@/services/api';
|
||||||
@@ -40,10 +41,12 @@ export default function ContactPage() {
|
|||||||
});
|
});
|
||||||
setSubmitted(true);
|
setSubmitted(true);
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
if (err instanceof Error) {
|
if (isAxiosError(err) && err.response?.data?.error) {
|
||||||
|
setError(err.response.data.error);
|
||||||
|
} else if (err instanceof Error) {
|
||||||
setError(err.message);
|
setError(err.message);
|
||||||
} else {
|
} else {
|
||||||
setError(err.response.data.error);
|
setError(String(err));
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ function shouldAttachCsrfToken(config: InternalAxiosRequestConfig): boolean {
|
|||||||
|
|
||||||
function setRequestHeader(config: InternalAxiosRequestConfig, key: string, value: string) {
|
function setRequestHeader(config: InternalAxiosRequestConfig, key: string, value: string) {
|
||||||
if (!config.headers) {
|
if (!config.headers) {
|
||||||
config.headers = {};
|
config.headers = new axios.AxiosHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof (config.headers as { set?: (header: string, headerValue: string) => void }).set === 'function') {
|
if (typeof (config.headers as { set?: (header: string, headerValue: string) => void }).set === 'function') {
|
||||||
|
|||||||
Reference in New Issue
Block a user