File size: 4,995 Bytes
5da4770 |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
import { toast } from 'sonner';
import { BillingError } from './api';
export interface ApiError extends Error {
status?: number;
code?: string;
details?: any;
response?: Response;
}
export interface ErrorContext {
operation?: string;
resource?: string;
silent?: boolean;
}
const getStatusMessage = (status: number): string => {
switch (status) {
case 400:
return 'Invalid request. Please check your input and try again.';
case 401:
return 'Authentication required. Please sign in again.';
case 403:
return 'Access denied. You don\'t have permission to perform this action.';
case 404:
return 'The requested resource was not found.';
case 408:
return 'Request timeout. Please try again.';
case 409:
return 'Conflict detected. The resource may have been modified by another user.';
case 422:
return 'Invalid data provided. Please check your input.';
case 429:
return 'Too many requests. Please wait a moment and try again.';
case 500:
return 'Server error. Our team has been notified.';
case 502:
return 'Service temporarily unavailable. Please try again in a moment.';
case 503:
return 'Service maintenance in progress. Please try again later.';
case 504:
return 'Request timeout. The server took too long to respond.';
default:
return 'An unexpected error occurred. Please try again.';
}
};
const extractErrorMessage = (error: any): string => {
if (error instanceof BillingError) {
return error.detail?.message || error.message || 'Billing issue detected';
}
if (error instanceof Error) {
return error.message;
}
if (error?.response) {
const status = error.response.status;
return getStatusMessage(status);
}
if (error?.status) {
return getStatusMessage(error.status);
}
if (typeof error === 'string') {
return error;
}
if (error?.message) {
return error.message;
}
if (error?.error) {
return typeof error.error === 'string' ? error.error : error.error.message || 'Unknown error';
}
return 'An unexpected error occurred';
};
const shouldShowError = (error: any, context?: ErrorContext): boolean => {
if (context?.silent) {
return false;
}
if (error instanceof BillingError) {
return false;
}
if (error?.status === 404 && context?.resource) {
return false;
}
return true;
};
const formatErrorMessage = (message: string, context?: ErrorContext): string => {
if (!context?.operation && !context?.resource) {
return message;
}
const parts = [];
if (context.operation) {
parts.push(`Failed to ${context.operation}`);
}
if (context.resource) {
parts.push(context.resource);
}
const prefix = parts.join(' ');
if (message.toLowerCase().includes(context.operation?.toLowerCase() || '')) {
return message;
}
return `${prefix}: ${message}`;
};
export const handleApiError = (error: any, context?: ErrorContext): void => {
console.error('API Error:', error, context);
if (!shouldShowError(error, context)) {
return;
}
const rawMessage = extractErrorMessage(error);
const formattedMessage = formatErrorMessage(rawMessage, context);
if (error?.status >= 500) {
toast.error(formattedMessage, {
description: 'Our team has been notified and is working on a fix.',
duration: 6000,
});
} else if (error?.status === 401) {
toast.error(formattedMessage, {
description: 'Please refresh the page and sign in again.',
duration: 8000,
});
} else if (error?.status === 403) {
toast.error(formattedMessage, {
description: 'Contact support if you believe this is an error.',
duration: 6000,
});
} else if (error?.status === 429) {
toast.warning(formattedMessage, {
description: 'Please wait a moment before trying again.',
duration: 5000,
});
} else {
toast.error(formattedMessage, {
duration: 5000,
});
}
};
export const handleNetworkError = (error: any, context?: ErrorContext): void => {
const isNetworkError =
error?.message?.includes('fetch') ||
error?.message?.includes('network') ||
error?.message?.includes('connection') ||
error?.code === 'NETWORK_ERROR' ||
!navigator.onLine;
if (isNetworkError) {
toast.error('Connection error', {
description: 'Please check your internet connection and try again.',
duration: 6000,
});
} else {
handleApiError(error, context);
}
};
export const handleApiSuccess = (message: string, description?: string): void => {
toast.success(message, {
description,
duration: 3000,
});
};
export const handleApiWarning = (message: string, description?: string): void => {
toast.warning(message, {
description,
duration: 4000,
});
};
export const handleApiInfo = (message: string, description?: string): void => {
toast.info(message, {
description,
duration: 3000,
});
}; |