{"record":{"id":"47eb85edea8d3dc9","repo":"babalae/better-genshin-impact","slug":"currentstate-handler","errorCode":null,"errorMessage":"状态 {CurrentState} 的 Handler 返回失败","messagePattern":"状态 (.+?) 的 Handler 返回失败","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/GameTask/Common/StateMachine/StateMachineBase.cs","lineNumber":762,"sourceCode":"                            case StateTransitionWaitStatus.IntermediateTimeout:\n                                throw new InvalidOperationException($\"状态 {CurrentState} 等待中间态转换超时\");\n                        }\n                        break;\n\n                    case StateHandlerStatus.Wait:\n                        // 可预期的等待：状态机继续循环，重新检测状态。\n                        Logger.LogDebug(\"Handler 返回 Wait，状态机继续等待\");\n                        break;\n\n                    case StateHandlerStatus.Retry:\n                        // 意外失败重试：检查重试次数。\n                        RecordStateRetry(\"Handler 返回 Retry\", retryPolicy, stateRetryStopwatch);\n                        nextLoopInterval = retryInterval;\n                        break;\n\n                    case StateHandlerStatus.Fail:\n                        // 失败：抛出异常。\n                        throw new InvalidOperationException($\"状态 {CurrentState} 的 Handler 返回失败\");\n                }\n            }\n            else if (!EqualityComparer<TState>.Default.Equals(CurrentState, default) && _unknownStateHandler != null)\n            {\n                Logger.LogWarning(\"状态 {State} 无注册处理器，使用未知状态处理器\", CurrentState);\n                await _unknownStateHandler(context);\n            }\n            else if (_unknownStateHandler != null)\n            {\n                await _unknownStateHandler(context);\n            }\n            else\n            {\n                Logger.LogWarning(\"状态 {State} 无注册处理器，跳过\", CurrentState);\n            }\n\n            await Delay(nextLoopInterval, _ct);\n        }","sourceCodeStart":744,"sourceCodeEnd":780,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/Common/StateMachine/StateMachineBase.cs#L744-L780","documentation":"Thrown in RunStateMachineUntil when a registered state handler returns StateHandlerResult.Fail. Unlike Retry (transient, retried) or Wait (expected, loop continues), Fail is the handler's explicit signal of an unrecoverable error for the current state, and the state machine immediately aborts with InvalidOperationException.","triggerScenarios":"The handler for CurrentState executed and returned StateHandlerResult.Fail. This is by design the non-retryable failure path — e.g. the handler detected a condition that cannot be recovered (missing required element, contradictory state, fatal game error dialog).","commonSituations":"Handler logic intentionally returns Fail on an unrecoverable condition (e.g. an error popup appeared); a bug causes the handler to return Fail when it should return Retry; a precondition the handler assumes is violated.","solutions":["Inspect the handler for CurrentState to understand which condition produced Fail (the exception itself carries no detail — add context in the handler before returning Fail).","If the condition is actually transient, return Retry instead so the state machine can use the retry policy.","Fix the upstream game/UI condition that made the handler deem the state unrecoverable.","Improve handler logging so the Fail reason is recorded before returning."],"exampleFix":"// before — handler returns bare Fail with no recorded reason\ncase StateHandlerStatus.Fail:\n    throw new InvalidOperationException($\"状态 {CurrentState} 的 Handler 返回失败\");\n\n// after — capture the reason from the handler via a richer result so the throw is diagnosable\n// (handler side) return StateHandlerResult.Fail(\"队伍配置不存在\");\n// (state machine side) include the reason\nthrow new InvalidOperationException($\"状态 {CurrentState} 的 Handler 返回失败: {result.Reason}\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await RunStateMachineUntil(context, targets); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Handler 返回失败\"))\n{ Logger.LogError(ex, \"状态 {State} 处理器返回失败\", CurrentState); throw; }","preventionTips":["Return Fail only for genuinely unrecoverable conditions; use Retry for transient ones.","Log the specific reason inside the handler before returning Fail."],"tags":["state-machine","handler-failure","game-automation","diagnostics"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}