Files
Stirling-PDF/frontend/src/core/utils/toolErrorHandler.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-08-08 16:01:56 +01:00
/**
* Standardized error handling utilities for tool operations
*/
import { normalizeAxiosErrorData } from '@app/services/errorUtils';
2025-08-08 16:01:56 +01:00
/**
* Default error extractor that follows the standard pattern
*/
export const extractErrorMessage = (error: any): string => {
if (error.response?.data && typeof error.response.data === 'string') {
return error.response.data;
}
if (error.message) {
return error.message;
}
2025-09-25 21:03:53 +01:00
return 'There was an error processing your request.';
2025-08-08 16:01:56 +01:00
};
/**
* Creates a standardized error handler for tool operations
* @param fallbackMessage - Message to show when no specific error can be extracted
* @returns Error handler function that follows the standard pattern
*/
export const createStandardErrorHandler = (fallbackMessage: string) => {
return (error: any): string => {
if (error.response?.data && typeof error.response.data === 'string') {
return error.response.data;
}
if (error.message) {
return error.message;
}
return fallbackMessage;
};
};
/**
* Handles password-related errors with status code checking
* @param error - The error object from axios
* @param incorrectPasswordMessage - Message to show for incorrect password (typically 500 status)
* @param fallbackMessage - Message to show for other errors
* @returns Error message string
*/
export const handlePasswordError = async (
error: any,
incorrectPasswordMessage: string,
fallbackMessage: string
): Promise<string> => {
const status = error?.response?.status;
// Handle specific error cases with user-friendly messages
if (status === 500) {
// 500 typically means incorrect password for encrypted PDFs
return incorrectPasswordMessage;
}
// For other errors, try to extract the message
const normalizedData = await normalizeAxiosErrorData(error?.response?.data);
const errorWithNormalizedData = {
...error,
response: {
...error?.response,
data: normalizedData
}
};
return extractErrorMessage(errorWithNormalizedData) || fallbackMessage;
2025-08-08 16:01:56 +01:00
};