export enum AppErrorType { CALCULATION_ERROR = 'CALCULATION_ERROR', DATA_ERROR = 'DATA_ERROR', STORAGE_ERROR = 'STORAGE_ERROR', VALIDATION_ERROR = 'VALIDATION_ERROR', UNKNOWN_ERROR = 'UNKNOWN_ERROR' } export interface AppError { type: AppErrorType code: number message: string detail?: any } class ErrorHandler { private errorMessages: Record = { CALCULATION_ERROR: '计算出错,请稍后重试', DATA_ERROR: '数据异常,请稍后重试', STORAGE_ERROR: '存储操作失败,请检查设备空间', VALIDATION_ERROR: '输入数据有误,请检查后重试', UNKNOWN_ERROR: '未知错误,请稍后重试' } handleError(error: AppError | Error): void { const appError = this.toAppError(error) this.logError(appError) this.showErrorMessage(appError.message) } showErrorMessage(message: string): void { uni.showToast({ title: message, icon: 'none', duration: 3000 }) } showSuccessMessage(message: string): void { uni.showToast({ title: message, icon: 'success', duration: 2000 }) } logError(error: AppError): void { console.error('[App Error]', { type: error.type, code: error.code, message: error.message, detail: error.detail }) } getErrorMessage(error: AppError): string { return this.errorMessages[error.type] || error.message } createError(type: AppErrorType, message: string, code: number = 0, detail?: any): AppError { return { type, code, message, detail } } private toAppError(error: AppError | Error): AppError { if ('type' in error && 'code' in error) { return error as AppError } return { type: AppErrorType.UNKNOWN_ERROR, code: 0, message: error.message || '未知错误' } } } const errorHandler = new ErrorHandler() export default errorHandler export { ErrorHandler }