Enhance API client configuration with timeout and error handling improvements
This commit updates the API client configuration to include a 60-second timeout for requests, allows for a maximum of 5 redirects, and modifies the status validation to handle all status codes. Additionally, it improves error handling by checking for network errors and preventing automatic logout on timeouts or network issues, enhancing the robustness of the API interactions.
This commit is contained in:
@@ -5,6 +5,13 @@ export const backendBaseUrl = import.meta.env.VITE_BACKEND || 'http://localhost:
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: `${backendBaseUrl}/api`,
|
||||
timeout: 60000, // 60 Sekunden Timeout für Requests (länger für langsame Verbindungen)
|
||||
// Erlaube längere Verbindungen
|
||||
maxRedirects: 5,
|
||||
validateStatus: function (status) {
|
||||
// Behandle alle Status-Codes als gültig, damit wir sie selbst behandeln können
|
||||
return status >= 200 && status < 600;
|
||||
},
|
||||
});
|
||||
|
||||
apiClient.interceptors.request.use(config => {
|
||||
@@ -25,6 +32,16 @@ apiClient.interceptors.request.use(config => {
|
||||
apiClient.interceptors.response.use(
|
||||
response => response,
|
||||
error => {
|
||||
// Prüfe auf Netzwerkfehler (keine Response = Verbindungsproblem)
|
||||
if (!error.response) {
|
||||
// Wenn es ein Timeout oder Netzwerkfehler ist, nicht automatisch ausloggen
|
||||
if (error.code === 'ECONNABORTED' || error.code === 'ERR_NETWORK' || error.message === 'Network Error') {
|
||||
console.warn('[API Client] Netzwerkfehler oder Timeout:', error.message);
|
||||
// Wirft den Fehler weiter, aber ohne automatisches Logout
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
if (error.response && error.response.status === 401) {
|
||||
// Automatisch ausloggen und zur Login-Seite weiterleiten
|
||||
store.dispatch('logout');
|
||||
|
||||
Reference in New Issue
Block a user