|
| 1 | +// ============================================================================ |
| 2 | +// Global Error Handling Utilities |
| 3 | +// ============================================================================ |
| 4 | + |
| 5 | +import {SerializedError} from "@reduxjs/toolkit" |
| 6 | +import {FetchBaseQueryError} from "@reduxjs/toolkit/query" |
| 7 | +import {TFunction} from "i18next" |
| 8 | + |
| 9 | +/** |
| 10 | + * Standard error structure from the backend |
| 11 | + */ |
| 12 | +export interface ServerErrorType { |
| 13 | + data: { |
| 14 | + detail: string |
| 15 | + [key: string]: any |
| 16 | + } |
| 17 | + status: number |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Type guard to check if error is FetchBaseQueryError |
| 22 | + */ |
| 23 | +export function isFetchBaseQueryError( |
| 24 | + error: unknown |
| 25 | +): error is FetchBaseQueryError { |
| 26 | + return typeof error === "object" && error != null && "status" in error |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Type guard to check if error is SerializedError |
| 31 | + */ |
| 32 | +export function isSerializedError(error: unknown): error is SerializedError { |
| 33 | + return ( |
| 34 | + typeof error === "object" && |
| 35 | + error != null && |
| 36 | + ("message" in error || "code" in error) |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Extract user-friendly error message from various error types |
| 42 | + */ |
| 43 | +export function getErrorMessage( |
| 44 | + error: unknown, |
| 45 | + t: TFunction, |
| 46 | + context?: string |
| 47 | +): string { |
| 48 | + // Handle RTK Query FetchBaseQueryError |
| 49 | + if (isFetchBaseQueryError(error)) { |
| 50 | + // Network errors |
| 51 | + if (error.status === "FETCH_ERROR") { |
| 52 | + return t("errors.network_error", { |
| 53 | + defaultValue: "Network error. Please check your connection." |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + // Parsing errors |
| 58 | + if (error.status === "PARSING_ERROR") { |
| 59 | + return t("errors.parsing_error", { |
| 60 | + defaultValue: "Server response error." |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + // Timeout errors |
| 65 | + if (error.status === "TIMEOUT_ERROR") { |
| 66 | + return t("errors.timeout_error", { |
| 67 | + defaultValue: "Request timed out. Please try again." |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + // Handle HTTP status codes |
| 72 | + if (typeof error.status === "number") { |
| 73 | + const serverError = error as ServerErrorType |
| 74 | + |
| 75 | + // If backend provides specific error detail, use it |
| 76 | + if (serverError.data?.detail) { |
| 77 | + return serverError.data.detail |
| 78 | + } |
| 79 | + |
| 80 | + // Generic status code messages with context |
| 81 | + const statusMessages: Record<number, string> = { |
| 82 | + 400: t(`errors.${context}.bad_request`, { |
| 83 | + defaultValue: t("errors.bad_request", { |
| 84 | + defaultValue: "Invalid request. Please check your input." |
| 85 | + }) |
| 86 | + }), |
| 87 | + 401: t(`errors.${context}.unauthorized`, { |
| 88 | + defaultValue: t("errors.unauthorized", { |
| 89 | + defaultValue: "You are not authorized to perform this action." |
| 90 | + }) |
| 91 | + }), |
| 92 | + 403: t(`errors.${context}.forbidden`, { |
| 93 | + defaultValue: t("errors.forbidden", { |
| 94 | + defaultValue: "Access denied." |
| 95 | + }) |
| 96 | + }), |
| 97 | + 404: t(`errors.${context}.not_found`, { |
| 98 | + defaultValue: t("errors.not_found", { |
| 99 | + defaultValue: "Resource not found." |
| 100 | + }) |
| 101 | + }), |
| 102 | + 409: t(`errors.${context}.conflict`, { |
| 103 | + defaultValue: t("errors.conflict", { |
| 104 | + defaultValue: "Conflict: Resource already exists or is in use." |
| 105 | + }) |
| 106 | + }), |
| 107 | + 422: t(`errors.${context}.validation_error`, { |
| 108 | + defaultValue: t("errors.validation_error", { |
| 109 | + defaultValue: "Validation error. Please check your input." |
| 110 | + }) |
| 111 | + }), |
| 112 | + 500: t(`errors.${context}.server_error`, { |
| 113 | + defaultValue: t("errors.server_error", { |
| 114 | + defaultValue: "Server error. Please try again later." |
| 115 | + }) |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + if (error.status in statusMessages) { |
| 120 | + return statusMessages[error.status] |
| 121 | + } |
| 122 | + |
| 123 | + // Generic 5xx error |
| 124 | + if (error.status >= 500) { |
| 125 | + return t("errors.server_error", { |
| 126 | + defaultValue: "Server error. Please try again later." |
| 127 | + }) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Handle SerializedError from RTK |
| 133 | + if (isSerializedError(error)) { |
| 134 | + if (error.message) { |
| 135 | + return error.message |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Fallback error message with context |
| 140 | + if (context) { |
| 141 | + return t(`errors.${context}.generic`, { |
| 142 | + defaultValue: t("errors.generic", { |
| 143 | + defaultValue: "An unexpected error occurred. Please try again." |
| 144 | + }) |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + return t("errors.generic", { |
| 149 | + defaultValue: "An unexpected error occurred. Please try again." |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Get error title for notifications |
| 155 | + */ |
| 156 | +export function getErrorTitle( |
| 157 | + error: unknown, |
| 158 | + t: TFunction, |
| 159 | + context?: string |
| 160 | +): string { |
| 161 | + if (isFetchBaseQueryError(error) && typeof error.status === "number") { |
| 162 | + if (error.status >= 500) { |
| 163 | + return t("errors.title.server_error", { |
| 164 | + defaultValue: "Server Error" |
| 165 | + }) |
| 166 | + } |
| 167 | + if (error.status === 401 || error.status === 403) { |
| 168 | + return t("errors.title.access_denied", { |
| 169 | + defaultValue: "Access Denied" |
| 170 | + }) |
| 171 | + } |
| 172 | + if (error.status === 404) { |
| 173 | + return t("errors.title.not_found", { |
| 174 | + defaultValue: "Not Found" |
| 175 | + }) |
| 176 | + } |
| 177 | + if (error.status === 409) { |
| 178 | + return t("errors.title.conflict", { |
| 179 | + defaultValue: "Conflict" |
| 180 | + }) |
| 181 | + } |
| 182 | + if (error.status === 422 || error.status === 400) { |
| 183 | + return t("errors.title.validation_error", { |
| 184 | + defaultValue: "Validation Error" |
| 185 | + }) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if (context) { |
| 190 | + return t(`errors.${context}.title`, { |
| 191 | + defaultValue: t("errors.title.error", {defaultValue: "Error"}) |
| 192 | + }) |
| 193 | + } |
| 194 | + |
| 195 | + return t("errors.title.error", {defaultValue: "Error"}) |
| 196 | +} |
0 commit comments