mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 00:26:37 +08:00
refactor(repo): consolidate openflare-server to root and move subprojects to internal/apps
- Merge all files inside openflare-server to the repository root directory. - Relocate agent, relay, and flared subprojects from internal/ to internal/apps/. - Combine docker-compose files and update build context paths to root. - Update GitHub workflows and Dockerfiles to refer to new directories and package names. - Rewrite Go package imports across all files. - Resolve database renew test race condition and clean up docs.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import {isCancelError} from '@/lib/services/core'
|
||||
import {toast} from 'sonner'
|
||||
|
||||
/**
|
||||
* 错误处理选项
|
||||
*/
|
||||
interface HandleContextErrorOptions {
|
||||
/** 是否显示 toast 提示 */
|
||||
showToast?: boolean
|
||||
/** 是否在控制台记录错误 */
|
||||
logError?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一处理 Context 中的错误
|
||||
*
|
||||
* @param error - 捕获的错误对象
|
||||
* @param defaultMessage - 默认错误消息
|
||||
* @param options - 错误处理选项
|
||||
* @returns 标准化的 Error 对象
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* try {
|
||||
* await fetchData()
|
||||
* } catch (error) {
|
||||
* const errorObject = handleContextError(error, '加载数据失败', {
|
||||
* showToast: true,
|
||||
* logError: true
|
||||
* })
|
||||
* setError(errorObject)
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function handleContextError(
|
||||
error: unknown,
|
||||
defaultMessage: string,
|
||||
options: HandleContextErrorOptions = {}
|
||||
): Error {
|
||||
const { showToast = false, logError = true } = options
|
||||
|
||||
// 取消的请求不算错误
|
||||
if (isCancelError(error)) {
|
||||
return new Error('请求已取消')
|
||||
}
|
||||
|
||||
const errorMessage = error instanceof Error ? error.message : defaultMessage
|
||||
const errorObject = error instanceof Error ? error : new Error(defaultMessage)
|
||||
|
||||
if (logError) {
|
||||
console.error(defaultMessage, error)
|
||||
}
|
||||
|
||||
if (showToast) {
|
||||
toast.error(defaultMessage, {
|
||||
description: errorMessage
|
||||
})
|
||||
}
|
||||
|
||||
return errorObject
|
||||
}
|
||||
Reference in New Issue
Block a user