- 组件改造:EmptyState、ExportPanel、SearchConditionItem、SearchHistoryPanel、 SearchResultCard、SearchResultList、Select、SortSwitcher、TemplatePanel、 BottomNavigation、SearchConditionPanel - 替换硬编码中文为 $t() 调用和 computed 响应式翻译 - 新增翻译键:search(搜索历史/模板/条件)、export(导出选项)、 common.pleaseSelect 等 - 同步更新 zh-CN、en、zh-TW、ja、ko、es、fr、de、pt 共9种语言文件 - 重构 errorHandler:从 API 错误处理改为本地应用错误处理 - 移除 search.ts 中未使用的 SearchService 接口
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
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<AppErrorType, string> = {
|
|
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 }
|