first commit
This commit is contained in:
76
lib/utils/error-handler.ts
Normal file
76
lib/utils/error-handler.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Centralized error handling utility
|
||||
* Converts API errors to user-friendly messages
|
||||
*/
|
||||
|
||||
export interface ApiErrorResponse {
|
||||
message?: string
|
||||
errors?: Record<string, string[]>
|
||||
status?: number
|
||||
}
|
||||
|
||||
export function getErrorMessage(error: any): string {
|
||||
if (!error) return "An unexpected error occurred"
|
||||
|
||||
// Axios error
|
||||
if (error.response?.data?.message) {
|
||||
return error.response.data.message
|
||||
}
|
||||
|
||||
if (error.response?.status === 401) {
|
||||
return "Please log in to continue"
|
||||
}
|
||||
|
||||
if (error.response?.status === 403) {
|
||||
return "You don't have permission to perform this action"
|
||||
}
|
||||
|
||||
if (error.response?.status === 404) {
|
||||
return "The requested resource was not found"
|
||||
}
|
||||
|
||||
if (error.response?.status === 500) {
|
||||
return "Server error occurred. Please try again later"
|
||||
}
|
||||
|
||||
if (error.message === "Network Error") {
|
||||
return "Network connection error. Please check your internet connection"
|
||||
}
|
||||
|
||||
if (typeof error === "string") {
|
||||
return error
|
||||
}
|
||||
|
||||
return "An error occurred. Please try again"
|
||||
}
|
||||
|
||||
export function getValidationErrors(error: any): Record<string, string> {
|
||||
if (error.response?.data?.errors && typeof error.response.data.errors === "object") {
|
||||
const errors: Record<string, string> = {}
|
||||
for (const [key, messages] of Object.entries(error.response.data.errors)) {
|
||||
errors[key] = Array.isArray(messages) ? messages[0] : String(messages)
|
||||
}
|
||||
return errors
|
||||
}
|
||||
return {}
|
||||
}
|
||||
|
||||
export function isNetworkError(error: any): boolean {
|
||||
return error?.message === "Network Error" || !error?.response
|
||||
}
|
||||
|
||||
export function isUnauthorized(error: any): boolean {
|
||||
return error?.response?.status === 401
|
||||
}
|
||||
|
||||
export function isForbidden(error: any): boolean {
|
||||
return error?.response?.status === 403
|
||||
}
|
||||
|
||||
export function isNotFound(error: any): boolean {
|
||||
return error?.response?.status === 404
|
||||
}
|
||||
|
||||
export function isServerError(error: any): boolean {
|
||||
return error?.response?.status >= 500
|
||||
}
|
||||
21
lib/utils/loading-states.ts
Normal file
21
lib/utils/loading-states.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Loading state utilities for better UX
|
||||
*/
|
||||
|
||||
export const loadingMessages = {
|
||||
fetching: "Loading...",
|
||||
submitting: "Processing...",
|
||||
deleting: "Deleting...",
|
||||
updating: "Updating...",
|
||||
saving: "Saving...",
|
||||
cart: "Adding to cart...",
|
||||
checkout: "Processing order...",
|
||||
} as const
|
||||
|
||||
export const skeletonCounts = {
|
||||
products: 10,
|
||||
categories: 6,
|
||||
cartItems: 3,
|
||||
orders: 6,
|
||||
reviews: 4,
|
||||
} as const
|
||||
Reference in New Issue
Block a user