refactor(logging): replace console.error in action-utils with logger
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
|
||||
import type { ActionState } from "@/shared/types/action-state"
|
||||
import { PermissionDeniedError } from "@/shared/lib/errors"
|
||||
import { createModuleLogger } from "@/shared/lib/logger"
|
||||
|
||||
const log = createModuleLogger("action")
|
||||
|
||||
/**
|
||||
* 已知的业务错误类型,消息可以安全返回给客户端。
|
||||
@@ -69,11 +72,11 @@ export function handleActionError(e: unknown): ActionState<never> {
|
||||
|
||||
// 未知错误:不暴露内部细节,仅记录服务端日志
|
||||
if (e instanceof Error) {
|
||||
console.error("[ActionError]", e.name, e.message, e.stack)
|
||||
log.error({ err: e }, "Action failed")
|
||||
return { success: false, message: "操作失败,请稍后重试", errorCode: "unexpected" }
|
||||
}
|
||||
|
||||
console.error("[ActionError] Unknown error:", e)
|
||||
log.error({ err: e }, "Unknown action error")
|
||||
return { success: false, message: "操作失败,请稍后重试", errorCode: "unexpected" }
|
||||
}
|
||||
|
||||
@@ -90,11 +93,11 @@ export function handleActionError(e: unknown): ActionState<never> {
|
||||
* const result = await safeActionCall(
|
||||
* () => createGradeRecordAction(null, formData),
|
||||
* {
|
||||
* onError: () => toast.error("保存失败"),
|
||||
* onError: () => notify.error("保存失败"),
|
||||
* onFinally: () => setIsSubmitting(false),
|
||||
* }
|
||||
* )
|
||||
* if (result?.success) { toast.success("保存成功") }
|
||||
* if (result?.success) { notify.success("保存成功") }
|
||||
* ```
|
||||
*/
|
||||
export async function safeActionCall<T>(
|
||||
@@ -109,7 +112,7 @@ export async function safeActionCall<T>(
|
||||
} catch (e) {
|
||||
// Action 抛出异常(非返回 failure),如网络错误、序列化错误等
|
||||
options?.onError?.(e)
|
||||
console.error("[SafeActionCall]", e)
|
||||
log.error({ err: e }, "Safe action call threw")
|
||||
return null
|
||||
} finally {
|
||||
options?.onFinally?.()
|
||||
|
||||
Reference in New Issue
Block a user