|
1 | 1 | import { ref } from 'vue'; |
2 | 2 | import { useRouter } from 'vue-router'; |
3 | 3 | import { useToast } from 'primevue/usetoast'; |
| 4 | +import type { AxiosError } from 'axios'; |
| 5 | + |
| 6 | +type ValidationErrors = Record<string, string[]>; |
| 7 | + |
| 8 | +interface ValidationErrorResponse { |
| 9 | + errors: ValidationErrors; |
| 10 | +} |
| 11 | + |
| 12 | +interface ErrorHandlingOptions { |
| 13 | + onError?: (error: AxiosError) => void; |
| 14 | +} |
4 | 15 |
|
5 | 16 | export function useAxiosErrorHandling() { |
6 | 17 | const router = useRouter(); |
7 | 18 | const toast = useToast(); |
8 | | - const validationErrors = ref({}); |
| 19 | + const validationErrors = ref<ValidationErrors>({}); |
9 | 20 |
|
10 | | - const clearErrors = (...fields) => { |
| 21 | + function clearErrors(...fields: string[]): void { |
11 | 22 | if (fields.length > 0) { |
12 | | - fields.forEach((field) => { |
| 23 | + fields.forEach(field => { |
13 | 24 | if (field in validationErrors.value) { |
14 | 25 | delete validationErrors.value[field]; |
15 | 26 | } |
16 | 27 | }); |
17 | 28 | } else { |
18 | 29 | validationErrors.value = {}; |
19 | 30 | } |
20 | | - }; |
| 31 | + } |
21 | 32 |
|
22 | | - const showErrorToast = (summary, detail) => { |
| 33 | + function showErrorToast(summary: string, detail: string, severity: 'error' | 'warn' = 'error'): void { |
23 | 34 | toast.add({ |
24 | | - severity: 'error', |
| 35 | + severity, |
25 | 36 | summary, |
26 | 37 | detail, |
27 | | - life: 3000, |
| 38 | + life: 5000, |
28 | 39 | }); |
29 | | - }; |
| 40 | + } |
30 | 41 |
|
31 | | - const handleAxiosError = (error, options = {}) => { |
| 42 | + function handleAxiosError( |
| 43 | + error: AxiosError, |
| 44 | + options: ErrorHandlingOptions = {} |
| 45 | + ): void { |
32 | 46 | if (error.response) { |
33 | | - const status = error.response.status; |
34 | | - const responseData = error.response.data; |
35 | | - |
36 | | - if (status === 419) { |
37 | | - router.push({ name: 'login' }); |
38 | | - showErrorToast('Session Expired', 'Please log in again.'); |
39 | | - } else if (status === 422 && responseData?.errors) { |
40 | | - validationErrors.value = responseData.errors; |
41 | | - //showErrorToast('Validation Error', 'Validation failed.'); |
42 | | - } else if (status >= 500) { |
43 | | - showErrorToast('Server Error', 'A critical error occurred.'); |
| 47 | + const { status, data: responseData } = error.response; |
| 48 | + |
| 49 | + if (status === 401) { |
| 50 | + console.log('User is unauthorized'); |
| 51 | + //showErrorToast('401 - Unauthorized', 'Please reload the page and login in.', 'warn'); |
| 52 | + } else if (status === 403) { |
| 53 | + showErrorToast('403 - Forbidden', 'Sorry, you are unauthorized to access this resource/action.', 'warn'); |
| 54 | + } else if (status === 404) { |
| 55 | + showErrorToast('404 - Not Found', 'Sorry, the resource you are looking for could not be found.', 'warn'); |
| 56 | + } else if (status === 419) { |
| 57 | + router.push({ name: 'login' }).then(() => { |
| 58 | + showErrorToast('419 - Session Expired ', 'Please reload the page.', 'warn'); |
| 59 | + }); |
| 60 | + } else if (status === 422) { |
| 61 | + const errorData = responseData as ValidationErrorResponse; |
| 62 | + if (errorData.errors) { |
| 63 | + validationErrors.value = errorData.errors; |
| 64 | + } |
| 65 | + } else if (status === 500) { |
| 66 | + showErrorToast('500 - Server Error', 'Whoops, something went wrong on our end. Please try again.'); |
| 67 | + } else if (status === 503) { |
| 68 | + showErrorToast( |
| 69 | + '503 - Service Unavailable', |
| 70 | + 'Sorry, we are doing some maintenance. Please check back soon.' |
| 71 | + ); |
| 72 | + } else { |
| 73 | + showErrorToast('Error', 'Something went wrong...'); |
44 | 74 | } |
45 | 75 | } else if (error.request) { |
46 | | - showErrorToast('Network Error', 'Technical difficulties, please try again later.'); |
47 | | - //console.error('Network Error:', error.message); |
| 76 | + showErrorToast('Error', 'Technical difficulties, please contact I.T. support.'); |
48 | 77 | } |
49 | 78 |
|
50 | | - if (options.onError) options.onError(error); |
51 | | - }; |
| 79 | + options.onError?.(error); |
| 80 | + } |
52 | 81 |
|
53 | 82 | return { |
54 | 83 | validationErrors, |
|
0 commit comments