Files
xingrin/frontend/lib/error-handler.ts

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-12-12 18:04:57 +08:00
/**
2025-12-29 18:09:57 +08:00
* Unified error handling utility
2025-12-12 18:04:57 +08:00
*
2025-12-29 18:09:57 +08:00
* According to project rule 24:
* - Success messages: Frontend constructs them
* - Error messages: Frontend constructs them, providing more specific error reasons
* - Console logs: Print complete backend response body
2025-12-12 18:04:57 +08:00
*/
import { toast } from "sonner"
/**
2025-12-29 18:09:57 +08:00
* API error information interface
2025-12-12 18:04:57 +08:00
*/
export interface ApiError {
response?: {
data?: unknown
}
message?: string
}
/**
2025-12-29 18:09:57 +08:00
* Handle mutation errors (generic)
* @param error Error object
* @param userMessage Frontend custom user-friendly error message
* @param toastId Optional toast ID (for dismissing loading toast)
2025-12-12 18:04:57 +08:00
*/
export function handleMutationError(
error: unknown,
userMessage: string,
toastId?: string
) {
2025-12-29 18:09:57 +08:00
// Dismiss loading toast (if any)
2025-12-12 18:04:57 +08:00
if (toastId) {
toast.dismiss(toastId)
}
2025-12-29 18:09:57 +08:00
// Print detailed error information to console
console.error('Operation failed:', error)
console.error('Backend response:', (error as ApiError)?.response?.data || error)
2025-12-12 18:04:57 +08:00
2025-12-29 18:09:57 +08:00
// Show frontend custom user-friendly error message
2025-12-12 18:04:57 +08:00
toast.error(userMessage)
}
/**
2025-12-29 18:09:57 +08:00
* Handle query errors (generic)
* @param error Error object
* @param userMessage Frontend custom user-friendly error message
2025-12-12 18:04:57 +08:00
*/
export function handleQueryError(error: unknown, userMessage: string) {
2025-12-29 18:09:57 +08:00
// Print detailed error information to console
console.error('Query failed:', error)
console.error('Backend response:', (error as ApiError)?.response?.data || error)
2025-12-12 18:04:57 +08:00
2025-12-29 18:09:57 +08:00
// Show frontend custom user-friendly error message
2025-12-12 18:04:57 +08:00
toast.error(userMessage)
}
/**
2025-12-29 18:09:57 +08:00
* Handle success response (generic)
* @param response Backend response
* @param successMessage Frontend custom success message
* @param toastId Optional toast ID (for dismissing loading toast)
2025-12-12 18:04:57 +08:00
*/
export function handleSuccess(
response: unknown,
successMessage: string,
toastId?: string
) {
2025-12-29 18:09:57 +08:00
// Dismiss loading toast (if any)
2025-12-12 18:04:57 +08:00
if (toastId) {
toast.dismiss(toastId)
}
2025-12-29 18:09:57 +08:00
// Print success information to console
console.log('Operation successful')
console.log('Backend response:', response)
2025-12-12 18:04:57 +08:00
2025-12-29 18:09:57 +08:00
// Show frontend custom success message
2025-12-12 18:04:57 +08:00
toast.success(successMessage)
}
/**
2025-12-29 18:09:57 +08:00
* Handle warning response (partial success scenarios)
* @param response Backend response
* @param warningMessage Frontend custom warning message
* @param toastId Optional toast ID (for dismissing loading toast)
2025-12-12 18:04:57 +08:00
*/
export function handleWarning(
response: unknown,
warningMessage: string,
toastId?: string
) {
2025-12-29 18:09:57 +08:00
// Dismiss loading toast (if any)
2025-12-12 18:04:57 +08:00
if (toastId) {
toast.dismiss(toastId)
}
2025-12-29 18:09:57 +08:00
// Print information to console (development environment only)
2025-12-12 18:04:57 +08:00
if (process.env.NODE_ENV === 'development') {
2025-12-29 18:09:57 +08:00
console.log('Operation partially successful')
console.log('Backend response:', response)
2025-12-12 18:04:57 +08:00
}
2025-12-29 18:09:57 +08:00
// Show frontend custom warning message
2025-12-12 18:04:57 +08:00
toast.warning(warningMessage)
}
/**
2025-12-29 18:09:57 +08:00
* Check if response is successful
* @param response API response
* @returns Whether successful
2025-12-12 18:04:57 +08:00
*/
export function isSuccessResponse(response: unknown): boolean {
return (response as { state?: string })?.state === 'success'
}
/**
2025-12-29 18:09:57 +08:00
* Extract data from response
* @param response API response
* @param defaultValue Default value
* @returns Response data
2025-12-12 18:04:57 +08:00
*/
export function extractData<T>(response: unknown, defaultValue: T): T {
if (isSuccessResponse(response) && (response as { data?: T }).data) {
return (response as { data: T }).data
}
return defaultValue
}