refactor(errors): migrate decisions to {code,msg}

This commit is contained in:
zetaloop
2026-02-28 07:21:51 +08:00
parent 4e2ee5be54
commit cc24a0cbc3
23 changed files with 157 additions and 165 deletions
+21 -21
View File
@@ -1,9 +1,9 @@
import type { Actor } from "@/lib/actor"
import { DISPUTE_TO_RESOLVED_MS, DISPUTE_TO_REVIEWING_MS } from "@/lib/config/demo-timers"
import { allow, deny } from "@/lib/decision"
import type { ApiDecision } from "@/lib/errors"
import { generateId } from "@/lib/id"
import { mockDisputes } from "@/lib/mock"
import type { Actor } from "@/lib/policy/actor"
import { allow, deny } from "@/lib/policy/assert"
import type { PolicyDecision } from "@/lib/policy/decision"
import type { Dispute } from "@/lib/types"
import { useAuthStore } from "@/store/auth"
import { useNotificationStore } from "@/store/notifications"
@@ -36,7 +36,7 @@ interface SubmitDisputeInput {
}
interface DisputeMutationResult {
decision: PolicyDecision
decision: ApiDecision
dispute?: DisputeRecord
}
@@ -49,8 +49,8 @@ interface DisputeState {
actorId: string,
reason: string,
evidence: string[],
) => PolicyDecision
submitAppeal: (disputeId: string, actorId: string, reason: string) => PolicyDecision
) => ApiDecision
submitAppeal: (disputeId: string, actorId: string, reason: string) => ApiDecision
}
const progressTimers = new Map<string, ReturnType<typeof setTimeout>[]>()
@@ -205,20 +205,20 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
submitDispute: (input) => {
const order = useOrderStore.getState().orders.find((item) => item.id === input.orderId)
if (!order) {
return { decision: deny("NOT_FOUND", "订单不存在") }
return { decision: deny(404, "订单不存在") }
}
if (order.status !== "in_progress" && order.status !== "pending_close") {
return { decision: deny("INVALID_STATUS", "当前阶段不可发起争议") }
return { decision: deny(400, "当前阶段不可发起争议") }
}
if (!input.reason.trim()) {
return { decision: deny("VALIDATION_FAILED", "请填写争议原因") }
return { decision: deny(400, "请填写争议原因") }
}
const actor = resolveParticipantActor(input.orderId, input.initiatorId)
if (!actor) {
return { decision: deny("NOT_PARTICIPANT", "仅订单参与方可发起争议") }
return { decision: deny(403, "仅订单参与方可发起争议") }
}
const markResult = useOrderStore.getState().markDisputed(input.orderId, actor)
@@ -254,28 +254,28 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
submitResponse: (disputeId, actorId, reason, evidence) => {
const dispute = get().disputes.find((item) => item.id === disputeId)
if (!dispute) {
return deny("NOT_FOUND", "争议不存在")
return deny(404, "争议不存在")
}
const actor = resolveParticipantActor(dispute.orderId, actorId)
if (!actor) {
return deny("NOT_PARTICIPANT", "仅订单参与方可提交回应")
return deny(403, "仅订单参与方可提交回应")
}
if (actorId === dispute.initiatorId) {
return deny("ROLE_FORBIDDEN", "争议发起方不可提交回应")
return deny(403, "争议发起方不可提交回应")
}
if (dispute.respondentReason) {
return deny("ALREADY_DONE", "回应已提交")
return deny(400, "回应已提交")
}
if (dispute.status !== "open" && dispute.status !== "reviewing") {
return deny("INVALID_STATUS", "当前状态不可提交回应")
return deny(400, "当前状态不可提交回应")
}
if (!reason.trim()) {
return deny("VALIDATION_FAILED", "请填写回应理由")
return deny(400, "请填写回应理由")
}
set((state) => ({
@@ -306,24 +306,24 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
submitAppeal: (disputeId, actorId, reason) => {
const dispute = get().disputes.find((item) => item.id === disputeId)
if (!dispute) {
return deny("NOT_FOUND", "争议不存在")
return deny(404, "争议不存在")
}
const actor = resolveParticipantActor(dispute.orderId, actorId)
if (!actor) {
return deny("NOT_PARTICIPANT", "仅订单参与方可提交申诉")
return deny(403, "仅订单参与方可提交申诉")
}
if (dispute.status !== "resolved") {
return deny("INVALID_STATUS", "当前状态不可申诉")
return deny(400, "当前状态不可申诉")
}
if (dispute.appealedAt) {
return deny("ALREADY_DONE", "该争议已申诉过")
return deny(400, "该争议已申诉过")
}
if (!reason.trim()) {
return deny("VALIDATION_FAILED", "请填写申诉理由")
return deny(400, "请填写申诉理由")
}
set((state) => ({