Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 2x 18x 1x 17x 10x 7x 18x 18x 2x 2x 36x | import { create } from 'zustand';
import { formatErrorForDisplay, type FormattedError } from '@/utils/errorHandling';
import type { AxiosError } from 'axios';
interface ErrorToastState {
error: FormattedError | null;
showError: (error: string | Error | AxiosError | FormattedError) => void;
clearError: () => void;
}
/**
* Global error toast state store
* Manages error display across the application
*/
const useErrorToastStore = create<ErrorToastState>((set) => ({
error: null,
showError: (error) => {
let formattedError: FormattedError;
// Handle different error types
if (typeof error === 'string') {
formattedError = {
message: error,
};
} else if ('message' in error && 'statusCode' in error) {
// Already a FormattedError
formattedError = error as FormattedError;
} else {
// Error or AxiosError
formattedError = formatErrorForDisplay(error);
}
// Log error to console
console.error('Error occurred:', {
message: formattedError.message,
statusCode: formattedError.statusCode,
correlationId: formattedError.correlationId,
isNetworkError: formattedError.isNetworkError,
isTimeoutError: formattedError.isTimeoutError,
originalError: formattedError.originalError,
});
// Update state
set({ error: formattedError });
},
clearError: () => {
set({ error: null });
},
}));
/**
* Hook for displaying global error toasts
*
* @example
* const { error, showError, clearError } = useErrorToast();
*
* // Show error from API
* try {
* await apiClient.get('/users');
* } catch (err) {
* showError(err);
* }
*
* // Show custom error
* showError('Invalid email format');
*
* // Clear error
* clearError();
*/
export const useErrorToast = () => {
return useErrorToastStore();
};
|