mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-09-26 17:52:59 +02:00
Reworked http.ts
This commit is contained in:
parent
d613a4659e
commit
cc21a18de2
@ -1,5 +1,5 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import axios from 'axios';
|
import apiClient from '../../../services/apiClient';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { ConvertParameters, defaultParameters } from './useConvertParameters';
|
import { ConvertParameters, defaultParameters } from './useConvertParameters';
|
||||||
import { createFileFromApiResponse } from '../../../utils/fileResponseUtils';
|
import { createFileFromApiResponse } from '../../../utils/fileResponseUtils';
|
||||||
@ -108,7 +108,7 @@ export const convertProcessor = async (
|
|||||||
for (const file of selectedFiles) {
|
for (const file of selectedFiles) {
|
||||||
try {
|
try {
|
||||||
const formData = buildConvertFormData(parameters, [file]);
|
const formData = buildConvertFormData(parameters, [file]);
|
||||||
const response = await axios.post(endpoint, formData, { responseType: 'blob' });
|
const response = await apiClient.post(endpoint, formData, { responseType: 'blob' });
|
||||||
|
|
||||||
const convertedFile = createFileFromResponse(response.data, response.headers, file.name, parameters.toExtension);
|
const convertedFile = createFileFromResponse(response.data, response.headers, file.name, parameters.toExtension);
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ export const convertProcessor = async (
|
|||||||
} else {
|
} else {
|
||||||
// Batch processing for simple cases (image→PDF combine)
|
// Batch processing for simple cases (image→PDF combine)
|
||||||
const formData = buildConvertFormData(parameters, selectedFiles);
|
const formData = buildConvertFormData(parameters, selectedFiles);
|
||||||
const response = await axios.post(endpoint, formData, { responseType: 'blob' });
|
const response = await apiClient.post(endpoint, formData, { responseType: 'blob' });
|
||||||
|
|
||||||
const baseFilename = selectedFiles.length === 1
|
const baseFilename = selectedFiles.length === 1
|
||||||
? selectedFiles[0].name
|
? selectedFiles[0].name
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { useCallback, useRef } from 'react';
|
import { useCallback, useRef } from 'react';
|
||||||
import axios, { CancelTokenSource } from '../../../services/http';
|
import axios, {type CancelTokenSource} from 'axios'; // Real axios for static methods (CancelToken, isCancel)
|
||||||
|
import apiClient from '../../../services/apiClient'; // Our configured instance
|
||||||
import { processResponse, ResponseHandler } from '../../../utils/toolResponseProcessor';
|
import { processResponse, ResponseHandler } from '../../../utils/toolResponseProcessor';
|
||||||
import { isEmptyOutput } from '../../../services/errorUtils';
|
import { isEmptyOutput } from '../../../services/errorUtils';
|
||||||
import type { ProcessingProgress } from './useToolState';
|
import type { ProcessingProgress } from './useToolState';
|
||||||
@ -42,9 +43,9 @@ export const useToolApiCalls = <TParams = void>() => {
|
|||||||
const formData = config.buildFormData(params, file);
|
const formData = config.buildFormData(params, file);
|
||||||
const endpoint = typeof config.endpoint === 'function' ? config.endpoint(params) : config.endpoint;
|
const endpoint = typeof config.endpoint === 'function' ? config.endpoint(params) : config.endpoint;
|
||||||
console.debug('[processFiles] POST', { endpoint, name: file.name });
|
console.debug('[processFiles] POST', { endpoint, name: file.name });
|
||||||
const response = await axios.post(endpoint, formData, {
|
const response = await apiClient.post(endpoint, formData, {
|
||||||
responseType: 'blob',
|
responseType: 'blob',
|
||||||
cancelToken: cancelTokenRef.current.token,
|
cancelToken: cancelTokenRef.current?.token,
|
||||||
});
|
});
|
||||||
console.debug('[processFiles] Response OK', { name: file.name, status: (response as any)?.status });
|
console.debug('[processFiles] Response OK', { name: file.name, status: (response as any)?.status });
|
||||||
|
|
||||||
@ -61,10 +62,10 @@ export const useToolApiCalls = <TParams = void>() => {
|
|||||||
if (empty) {
|
if (empty) {
|
||||||
console.warn('[processFiles] Empty output treated as failure', { name: file.name });
|
console.warn('[processFiles] Empty output treated as failure', { name: file.name });
|
||||||
failedFiles.push(file.name);
|
failedFiles.push(file.name);
|
||||||
try {
|
try {
|
||||||
(markFileError as any)?.((file as any).fileId);
|
(markFileError as any)?.((file as any).fileId);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.debug('markFileError', e);
|
console.debug('markFileError', e);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -80,10 +81,10 @@ export const useToolApiCalls = <TParams = void>() => {
|
|||||||
console.error('[processFiles] Failed', { name: file.name, error });
|
console.error('[processFiles] Failed', { name: file.name, error });
|
||||||
failedFiles.push(file.name);
|
failedFiles.push(file.name);
|
||||||
// mark errored file so UI can highlight
|
// mark errored file so UI can highlight
|
||||||
try {
|
try {
|
||||||
(markFileError as any)?.((file as any).fileId);
|
(markFileError as any)?.((file as any).fileId);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.debug('markFileError', e);
|
console.debug('markFileError', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { useCallback, useRef, useEffect } from 'react';
|
import { useCallback, useRef, useEffect } from 'react';
|
||||||
import axios from '../../../services/http';
|
import apiClient from '../../../services/apiClient';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useFileContext } from '../../../contexts/FileContext';
|
import { useFileContext } from '../../../contexts/FileContext';
|
||||||
import { useToolState, type ProcessingProgress } from './useToolState';
|
import { useToolState, type ProcessingProgress } from './useToolState';
|
||||||
@ -177,8 +177,8 @@ export const useToolOperation = <TParams>(
|
|||||||
for (const f of zeroByteFiles) {
|
for (const f of zeroByteFiles) {
|
||||||
(fileActions.markFileError as any)((f as any).fileId);
|
(fileActions.markFileError as any)((f as any).fileId);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('markFileError', e);
|
console.log('markFileError', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const validFiles = selectedFiles.filter(file => (file as any)?.size > 0);
|
const validFiles = selectedFiles.filter(file => (file as any)?.size > 0);
|
||||||
@ -243,7 +243,7 @@ export const useToolOperation = <TParams>(
|
|||||||
const formData = config.buildFormData(params, filesForAPI);
|
const formData = config.buildFormData(params, filesForAPI);
|
||||||
const endpoint = typeof config.endpoint === 'function' ? config.endpoint(params) : config.endpoint;
|
const endpoint = typeof config.endpoint === 'function' ? config.endpoint(params) : config.endpoint;
|
||||||
|
|
||||||
const response = await axios.post(endpoint, formData, { responseType: 'blob' });
|
const response = await apiClient.post(endpoint, formData, { responseType: 'blob' });
|
||||||
|
|
||||||
// Multi-file responses are typically ZIP files that need extraction, but some may return single PDFs
|
// Multi-file responses are typically ZIP files that need extraction, but some may return single PDFs
|
||||||
if (config.responseHandler) {
|
if (config.responseHandler) {
|
||||||
|
22
frontend/src/services/apiClient.ts
Normal file
22
frontend/src/services/apiClient.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// frontend/src/services/http.ts
|
||||||
|
import axios from 'axios';
|
||||||
|
import { handleHttpError } from './httpErrorHandler';
|
||||||
|
|
||||||
|
// Create axios instance with default config
|
||||||
|
const apiClient = axios.create({
|
||||||
|
baseURL: import.meta.env.VITE_API_BASE_URL || '/', // Use env var or relative path (proxied by Vite in dev)
|
||||||
|
responseType: 'json',
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------- Install error interceptor ----------
|
||||||
|
apiClient.interceptors.response.use(
|
||||||
|
(response) => response,
|
||||||
|
async (error) => {
|
||||||
|
await handleHttpError(error); // Handle error (shows toast unless suppressed)
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
// ---------- Exports ----------
|
||||||
|
export default apiClient;
|
@ -1,255 +0,0 @@
|
|||||||
// frontend/src/services/http.ts
|
|
||||||
import axios from 'axios';
|
|
||||||
import type { AxiosInstance } from 'axios';
|
|
||||||
import { alert } from '../components/toast';
|
|
||||||
import { broadcastErroredFiles, extractErrorFileIds, normalizeAxiosErrorData } from './errorUtils';
|
|
||||||
import { showSpecialErrorToast } from './specialErrorToasts';
|
|
||||||
|
|
||||||
const FRIENDLY_FALLBACK = 'There was an error processing your request.';
|
|
||||||
const MAX_TOAST_BODY_CHARS = 400; // avoid massive, unreadable toasts
|
|
||||||
|
|
||||||
function clampText(s: string, max = MAX_TOAST_BODY_CHARS): string {
|
|
||||||
return s && s.length > max ? `${s.slice(0, max)}…` : s;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isUnhelpfulMessage(msg: string | null | undefined): boolean {
|
|
||||||
const s = (msg || '').trim();
|
|
||||||
if (!s) return true;
|
|
||||||
// Common unhelpful payloads we see
|
|
||||||
if (s === '{}' || s === '[]') return true;
|
|
||||||
if (/^request failed/i.test(s)) return true;
|
|
||||||
if (/^network error/i.test(s)) return true;
|
|
||||||
if (/^[45]\d\d\b/.test(s)) return true; // "500 Server Error" etc.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function titleForStatus(status?: number): string {
|
|
||||||
if (!status) return 'Network error';
|
|
||||||
if (status >= 500) return 'Server error';
|
|
||||||
if (status >= 400) return 'Request error';
|
|
||||||
return 'Request failed';
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractAxiosErrorMessage(error: any): { title: string; body: string } {
|
|
||||||
if (axios.isAxiosError(error)) {
|
|
||||||
const status = error.response?.status;
|
|
||||||
const _statusText = error.response?.statusText || '';
|
|
||||||
let parsed: any = undefined;
|
|
||||||
const raw = error.response?.data;
|
|
||||||
if (typeof raw === 'string') {
|
|
||||||
try { parsed = JSON.parse(raw); } catch { /* keep as string */ }
|
|
||||||
} else {
|
|
||||||
parsed = raw;
|
|
||||||
}
|
|
||||||
const extractIds = (): string[] | undefined => {
|
|
||||||
if (Array.isArray(parsed?.errorFileIds)) return parsed.errorFileIds as string[];
|
|
||||||
const rawText = typeof raw === 'string' ? raw : '';
|
|
||||||
const uuidMatches = rawText.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g);
|
|
||||||
return uuidMatches && uuidMatches.length > 0 ? Array.from(new Set(uuidMatches)) : undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
const body = ((): string => {
|
|
||||||
const data = parsed;
|
|
||||||
if (!data) return typeof raw === 'string' ? raw : '';
|
|
||||||
const ids = extractIds();
|
|
||||||
if (ids && ids.length > 0) return `Failed files: ${ids.join(', ')}`;
|
|
||||||
if (data?.message) return data.message as string;
|
|
||||||
if (typeof raw === 'string') return raw;
|
|
||||||
try { return JSON.stringify(data); } catch { return ''; }
|
|
||||||
})();
|
|
||||||
const ids = extractIds();
|
|
||||||
const title = titleForStatus(status);
|
|
||||||
if (ids && ids.length > 0) {
|
|
||||||
return { title, body: 'Process failed due to invalid/corrupted file(s)' };
|
|
||||||
}
|
|
||||||
if (status === 422) {
|
|
||||||
const fallbackMsg = 'Process failed due to invalid/corrupted file(s)';
|
|
||||||
const bodyMsg = isUnhelpfulMessage(body) ? fallbackMsg : body;
|
|
||||||
return { title, body: bodyMsg };
|
|
||||||
}
|
|
||||||
const bodyMsg = isUnhelpfulMessage(body) ? FRIENDLY_FALLBACK : body;
|
|
||||||
return { title, body: bodyMsg };
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const msg = (error?.message || String(error)) as string;
|
|
||||||
return { title: 'Network error', body: isUnhelpfulMessage(msg) ? FRIENDLY_FALLBACK : msg };
|
|
||||||
} catch (e) {
|
|
||||||
// ignore extraction errors
|
|
||||||
console.debug('extractAxiosErrorMessage', e);
|
|
||||||
return { title: 'Network error', body: FRIENDLY_FALLBACK };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- Axios instance creation ----------
|
|
||||||
const __globalAny = (typeof window !== 'undefined' ? (window as any) : undefined);
|
|
||||||
|
|
||||||
type ExtendedAxiosInstance = AxiosInstance & {
|
|
||||||
CancelToken: typeof axios.CancelToken;
|
|
||||||
isCancel: typeof axios.isCancel;
|
|
||||||
};
|
|
||||||
|
|
||||||
const __PREV_CLIENT: ExtendedAxiosInstance | undefined =
|
|
||||||
__globalAny?.__SPDF_HTTP_CLIENT as ExtendedAxiosInstance | undefined;
|
|
||||||
|
|
||||||
let __createdClient: any;
|
|
||||||
if (__PREV_CLIENT) {
|
|
||||||
__createdClient = __PREV_CLIENT;
|
|
||||||
} else if (typeof (axios as any)?.create === 'function') {
|
|
||||||
try {
|
|
||||||
__createdClient = (axios as any).create();
|
|
||||||
} catch (e) {
|
|
||||||
console.debug('createClient', e);
|
|
||||||
__createdClient = axios as any;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
__createdClient = axios as any;
|
|
||||||
}
|
|
||||||
|
|
||||||
const apiClient: ExtendedAxiosInstance = (__createdClient || (axios as any)) as ExtendedAxiosInstance;
|
|
||||||
|
|
||||||
// Augment instance with axios static helpers for backwards compatibility
|
|
||||||
if (apiClient) {
|
|
||||||
try { (apiClient as any).CancelToken = (axios as any).CancelToken; } catch (e) { console.debug('setCancelToken', e); }
|
|
||||||
try { (apiClient as any).isCancel = (axios as any).isCancel; } catch (e) { console.debug('setIsCancel', e); }
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- Base defaults ----------
|
|
||||||
try {
|
|
||||||
const env = (import.meta as any)?.env || {};
|
|
||||||
apiClient.defaults.baseURL = env?.VITE_API_BASE_URL ?? '/';
|
|
||||||
apiClient.defaults.responseType = 'json';
|
|
||||||
// If OSS relies on cookies, uncomment:
|
|
||||||
// apiClient.defaults.withCredentials = true;
|
|
||||||
// Sensible timeout to avoid “forever hanging”:
|
|
||||||
apiClient.defaults.timeout = 20000;
|
|
||||||
} catch (e) {
|
|
||||||
console.debug('setDefaults', e);
|
|
||||||
apiClient.defaults.baseURL = apiClient.defaults.baseURL || '/';
|
|
||||||
apiClient.defaults.responseType = apiClient.defaults.responseType || 'json';
|
|
||||||
apiClient.defaults.timeout = apiClient.defaults.timeout || 20000;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- Install a single response error interceptor (dedup + UX) ----------
|
|
||||||
if (__globalAny?.__SPDF_HTTP_ERR_INTERCEPTOR_ID !== undefined && __PREV_CLIENT) {
|
|
||||||
try {
|
|
||||||
__PREV_CLIENT.interceptors.response.eject(__globalAny.__SPDF_HTTP_ERR_INTERCEPTOR_ID);
|
|
||||||
} catch (e) {
|
|
||||||
console.debug('ejectInterceptor', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const __recentSpecialByEndpoint: Record<string, number> = (__globalAny?.__SPDF_RECENT_SPECIAL || {});
|
|
||||||
const __SPECIAL_SUPPRESS_MS = 1500; // brief window to suppress generic duplicate after special toast
|
|
||||||
|
|
||||||
const __INTERCEPTOR_ID__ = apiClient?.interceptors?.response?.use
|
|
||||||
? apiClient.interceptors.response.use(
|
|
||||||
(response) => response,
|
|
||||||
async (error) => {
|
|
||||||
// Compute title/body (friendly) from the error object
|
|
||||||
const { title, body } = extractAxiosErrorMessage(error);
|
|
||||||
|
|
||||||
// Normalize response data ONCE, reuse for both ID extraction and special-toast matching
|
|
||||||
const raw = (error?.response?.data) as any;
|
|
||||||
let normalized: unknown = raw;
|
|
||||||
try { normalized = await normalizeAxiosErrorData(raw); } catch (e) { console.debug('normalizeAxiosErrorData', e); }
|
|
||||||
|
|
||||||
// 1) If server sends structured file IDs for failures, also mark them errored in UI
|
|
||||||
try {
|
|
||||||
const ids = extractErrorFileIds(normalized);
|
|
||||||
if (ids && ids.length > 0) {
|
|
||||||
broadcastErroredFiles(ids);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.debug('extractErrorFileIds', e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2) Generic-vs-special dedupe by endpoint
|
|
||||||
const url: string | undefined = error?.config?.url;
|
|
||||||
const status: number | undefined = error?.response?.status;
|
|
||||||
const now = Date.now();
|
|
||||||
const isSpecial =
|
|
||||||
status === 422 ||
|
|
||||||
status === 409 || // often actionable conflicts
|
|
||||||
/Failed files:/.test(body) ||
|
|
||||||
/invalid\/corrupted file\(s\)/i.test(body);
|
|
||||||
|
|
||||||
if (isSpecial && url) {
|
|
||||||
__recentSpecialByEndpoint[url] = now;
|
|
||||||
if (__globalAny) __globalAny.__SPDF_RECENT_SPECIAL = __recentSpecialByEndpoint;
|
|
||||||
}
|
|
||||||
if (!isSpecial && url) {
|
|
||||||
const last = __recentSpecialByEndpoint[url] || 0;
|
|
||||||
if (now - last < __SPECIAL_SUPPRESS_MS) {
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3) Show specialized friendly toasts if matched; otherwise show the generic one
|
|
||||||
let rawString: string | undefined;
|
|
||||||
try {
|
|
||||||
rawString =
|
|
||||||
typeof normalized === 'string'
|
|
||||||
? normalized
|
|
||||||
: JSON.stringify(normalized);
|
|
||||||
} catch (e) {
|
|
||||||
console.debug('extractErrorFileIds', e);
|
|
||||||
}
|
|
||||||
|
|
||||||
const handled = showSpecialErrorToast(rawString, { status });
|
|
||||||
if (!handled) {
|
|
||||||
const displayBody = clampText(body);
|
|
||||||
alert({ alertType: 'error', title, body: displayBody, expandable: true, isPersistentPopup: false });
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.reject(error);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
: undefined as any;
|
|
||||||
|
|
||||||
if (__globalAny) {
|
|
||||||
__globalAny.__SPDF_HTTP_ERR_INTERCEPTOR_ID = __INTERCEPTOR_ID__;
|
|
||||||
__globalAny.__SPDF_RECENT_SPECIAL = __recentSpecialByEndpoint;
|
|
||||||
__globalAny.__SPDF_HTTP_CLIENT = apiClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- Fetch helper ----------
|
|
||||||
export async function apiFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
|
||||||
const res = await fetch(input, { credentials: init?.credentials ?? 'include', ...init });
|
|
||||||
|
|
||||||
if (!res.ok) {
|
|
||||||
let detail = '';
|
|
||||||
try {
|
|
||||||
const ct = res.headers.get('content-type') || '';
|
|
||||||
if (ct.includes('application/json')) {
|
|
||||||
const data = await res.json();
|
|
||||||
detail = typeof data === 'string' ? data : (data?.message || JSON.stringify(data));
|
|
||||||
} else {
|
|
||||||
detail = await res.text();
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// ignore parse errors
|
|
||||||
}
|
|
||||||
|
|
||||||
const title = titleForStatus(res.status);
|
|
||||||
const body = isUnhelpfulMessage(detail || res.statusText) ? FRIENDLY_FALLBACK : (detail || res.statusText);
|
|
||||||
alert({ alertType: 'error', title, body: clampText(body), expandable: true, isPersistentPopup: false });
|
|
||||||
|
|
||||||
// Important: match Axios semantics so callers can try/catch
|
|
||||||
throw new Error(body || res.statusText);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- Convenience API surface and exports ----------
|
|
||||||
export const api = {
|
|
||||||
get: apiClient.get,
|
|
||||||
post: apiClient.post,
|
|
||||||
put: apiClient.put,
|
|
||||||
patch: apiClient.patch,
|
|
||||||
delete: apiClient.delete,
|
|
||||||
request: apiClient.request,
|
|
||||||
};
|
|
||||||
|
|
||||||
export default apiClient;
|
|
||||||
export type { CancelTokenSource } from 'axios';
|
|
147
frontend/src/services/httpErrorHandler.ts
Normal file
147
frontend/src/services/httpErrorHandler.ts
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
// frontend/src/services/httpErrorHandler.ts
|
||||||
|
import axios from 'axios';
|
||||||
|
import { alert } from '../components/toast';
|
||||||
|
import { broadcastErroredFiles, extractErrorFileIds, normalizeAxiosErrorData } from './errorUtils';
|
||||||
|
import { showSpecialErrorToast } from './specialErrorToasts';
|
||||||
|
|
||||||
|
const FRIENDLY_FALLBACK = 'There was an error processing your request.';
|
||||||
|
const MAX_TOAST_BODY_CHARS = 400; // avoid massive, unreadable toasts
|
||||||
|
|
||||||
|
function clampText(s: string, max = MAX_TOAST_BODY_CHARS): string {
|
||||||
|
return s && s.length > max ? `${s.slice(0, max)}…` : s;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isUnhelpfulMessage(msg: string | null | undefined): boolean {
|
||||||
|
const s = (msg || '').trim();
|
||||||
|
if (!s) return true;
|
||||||
|
// Common unhelpful payloads we see
|
||||||
|
if (s === '{}' || s === '[]') return true;
|
||||||
|
if (/^request failed/i.test(s)) return true;
|
||||||
|
if (/^network error/i.test(s)) return true;
|
||||||
|
if (/^[45]\d\d\b/.test(s)) return true; // "500 Server Error" etc.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function titleForStatus(status?: number): string {
|
||||||
|
if (!status) return 'Network error';
|
||||||
|
if (status >= 500) return 'Server error';
|
||||||
|
if (status >= 400) return 'Request error';
|
||||||
|
return 'Request failed';
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractAxiosErrorMessage(error: any): { title: string; body: string } {
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
const status = error.response?.status;
|
||||||
|
const _statusText = error.response?.statusText || '';
|
||||||
|
let parsed: any = undefined;
|
||||||
|
const raw = error.response?.data;
|
||||||
|
if (typeof raw === 'string') {
|
||||||
|
try { parsed = JSON.parse(raw); } catch { /* keep as string */ }
|
||||||
|
} else {
|
||||||
|
parsed = raw;
|
||||||
|
}
|
||||||
|
const extractIds = (): string[] | undefined => {
|
||||||
|
if (Array.isArray(parsed?.errorFileIds)) return parsed.errorFileIds as string[];
|
||||||
|
const rawText = typeof raw === 'string' ? raw : '';
|
||||||
|
const uuidMatches = rawText.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/g);
|
||||||
|
return uuidMatches && uuidMatches.length > 0 ? Array.from(new Set(uuidMatches)) : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
const body = ((): string => {
|
||||||
|
const data = parsed;
|
||||||
|
if (!data) return typeof raw === 'string' ? raw : '';
|
||||||
|
const ids = extractIds();
|
||||||
|
if (ids && ids.length > 0) return `Failed files: ${ids.join(', ')}`;
|
||||||
|
if (data?.message) return data.message as string;
|
||||||
|
if (typeof raw === 'string') return raw;
|
||||||
|
try { return JSON.stringify(data); } catch { return ''; }
|
||||||
|
})();
|
||||||
|
const ids = extractIds();
|
||||||
|
const title = titleForStatus(status);
|
||||||
|
if (ids && ids.length > 0) {
|
||||||
|
return { title, body: 'Process failed due to invalid/corrupted file(s)' };
|
||||||
|
}
|
||||||
|
if (status === 422) {
|
||||||
|
const fallbackMsg = 'Process failed due to invalid/corrupted file(s)';
|
||||||
|
const bodyMsg = isUnhelpfulMessage(body) ? fallbackMsg : body;
|
||||||
|
return { title, body: bodyMsg };
|
||||||
|
}
|
||||||
|
const bodyMsg = isUnhelpfulMessage(body) ? FRIENDLY_FALLBACK : body;
|
||||||
|
return { title, body: bodyMsg };
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const msg = (error?.message || String(error)) as string;
|
||||||
|
return { title: 'Network error', body: isUnhelpfulMessage(msg) ? FRIENDLY_FALLBACK : msg };
|
||||||
|
} catch (e) {
|
||||||
|
// ignore extraction errors
|
||||||
|
console.debug('extractAxiosErrorMessage', e);
|
||||||
|
return { title: 'Network error', body: FRIENDLY_FALLBACK };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Module-scoped state to reduce global variable usage
|
||||||
|
const recentSpecialByEndpoint: Record<string, number> = {};
|
||||||
|
const SPECIAL_SUPPRESS_MS = 1500; // brief window to suppress generic duplicate after special toast
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles HTTP errors with toast notifications and file error broadcasting
|
||||||
|
* Returns true if the error should be suppressed (deduplicated), false otherwise
|
||||||
|
*/
|
||||||
|
export async function handleHttpError(error: any): Promise<boolean> {
|
||||||
|
// Compute title/body (friendly) from the error object
|
||||||
|
const { title, body } = extractAxiosErrorMessage(error);
|
||||||
|
|
||||||
|
// Normalize response data ONCE, reuse for both ID extraction and special-toast matching
|
||||||
|
const raw = (error?.response?.data) as any;
|
||||||
|
let normalized: unknown = raw;
|
||||||
|
try { normalized = await normalizeAxiosErrorData(raw); } catch (e) { console.debug('normalizeAxiosErrorData', e); }
|
||||||
|
|
||||||
|
// 1) If server sends structured file IDs for failures, also mark them errored in UI
|
||||||
|
try {
|
||||||
|
const ids = extractErrorFileIds(normalized);
|
||||||
|
if (ids && ids.length > 0) {
|
||||||
|
broadcastErroredFiles(ids);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.debug('extractErrorFileIds', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2) Generic-vs-special dedupe by endpoint
|
||||||
|
const url: string | undefined = error?.config?.url;
|
||||||
|
const status: number | undefined = error?.response?.status;
|
||||||
|
const now = Date.now();
|
||||||
|
const isSpecial =
|
||||||
|
status === 422 ||
|
||||||
|
status === 409 || // often actionable conflicts
|
||||||
|
/Failed files:/.test(body) ||
|
||||||
|
/invalid\/corrupted file\(s\)/i.test(body);
|
||||||
|
|
||||||
|
if (isSpecial && url) {
|
||||||
|
recentSpecialByEndpoint[url] = now;
|
||||||
|
}
|
||||||
|
if (!isSpecial && url) {
|
||||||
|
const last = recentSpecialByEndpoint[url] || 0;
|
||||||
|
if (now - last < SPECIAL_SUPPRESS_MS) {
|
||||||
|
return true; // Suppress this error (deduplicated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3) Show specialized friendly toasts if matched; otherwise show the generic one
|
||||||
|
let rawString: string | undefined;
|
||||||
|
try {
|
||||||
|
rawString =
|
||||||
|
typeof normalized === 'string'
|
||||||
|
? normalized
|
||||||
|
: JSON.stringify(normalized);
|
||||||
|
} catch (e) {
|
||||||
|
console.debug('extractErrorFileIds', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
const handled = showSpecialErrorToast(rawString, { status });
|
||||||
|
if (!handled) {
|
||||||
|
const displayBody = clampText(body);
|
||||||
|
alert({ alertType: 'error', title, body: displayBody, expandable: true, isPersistentPopup: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
return false; // Error was handled with toast, continue normal rejection
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user