Fix: Return error for unrecognized slash commands instead of LLM passthrough
- Modified Execute() to return OutcomeHandled with error message for unknown commands - Updated test case to verify error handling behavior - Fixes issue #45: Unrecognized slash commands incorrectly forwarded to LLM Previously, unrecognized slash commands would return OutcomePassthrough, causing them to be forwarded to the LLM instead of triggering an error. This change ensures users get clear feedback when they use invalid commands.
This commit is contained in:
parent
1c6874fa74
commit
f092429d15
2 changed files with 19 additions and 7 deletions
|
|
@ -44,7 +44,9 @@ func (e *Executor) Execute(ctx context.Context, req Request) ExecuteResult {
|
||||||
|
|
||||||
def, found := e.reg.Lookup(cmdName)
|
def, found := e.reg.Lookup(cmdName)
|
||||||
if !found {
|
if !found {
|
||||||
return ExecuteResult{Outcome: OutcomePassthrough, Command: cmdName}
|
// Return an error for unrecognized commands instead of forwarding to LLM
|
||||||
|
err := req.Reply(fmt.Sprintf("Unknown command: %s", cmdName))
|
||||||
|
return ExecuteResult{Outcome: OutcomeHandled, Command: cmdName, Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.executeDefinition(ctx, req, def)
|
return e.executeDefinition(ctx, req, def)
|
||||||
|
|
@ -86,4 +88,4 @@ func (e *Executor) executeDefinition(ctx context.Context, req Request, def Defin
|
||||||
// Unknown sub-command
|
// Unknown sub-command
|
||||||
err := req.Reply(fmt.Sprintf("Unknown option: %s. Usage: %s", subName, def.EffectiveUsage()))
|
err := req.Reply(fmt.Sprintf("Unknown option: %s. Usage: %s", subName, def.EffectiveUsage()))
|
||||||
return ExecuteResult{Outcome: OutcomeHandled, Command: def.Name, Err: err}
|
return ExecuteResult{Outcome: OutcomeHandled, Command: def.Name, Err: err}
|
||||||
}
|
}
|
||||||
|
|
@ -17,13 +17,23 @@ func TestExecutor_RegisteredWithoutHandler_ReturnsPassthrough(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExecutor_UnknownSlashCommand_ReturnsPassthrough(t *testing.T) {
|
func TestExecutor_UnknownSlashCommand_ReturnsError(t *testing.T) {
|
||||||
defs := []Definition{{Name: "show"}}
|
defs := []Definition{{Name: "show"}}
|
||||||
ex := NewExecutor(NewRegistry(defs), nil)
|
ex := NewExecutor(NewRegistry(defs), nil)
|
||||||
|
|
||||||
res := ex.Execute(context.Background(), Request{Channel: "telegram", Text: "/unknown"})
|
var reply string
|
||||||
if res.Outcome != OutcomePassthrough {
|
res := ex.Execute(context.Background(), Request{Channel: "telegram", Text: "/unknown", Reply: func(text string) error { reply = text; return nil }})
|
||||||
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomePassthrough)
|
if res.Outcome != OutcomeHandled {
|
||||||
|
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomeHandled)
|
||||||
|
}
|
||||||
|
if res.Command != "unknown" {
|
||||||
|
t.Fatalf("command=%q, want=%q", res.Command, "unknown")
|
||||||
|
}
|
||||||
|
if res.Err != nil {
|
||||||
|
t.Fatalf("expected error, got nil")
|
||||||
|
}
|
||||||
|
if reply != "Unknown command: unknown" {
|
||||||
|
t.Fatalf("reply=%q, want=%q", reply, "Unknown command: unknown")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -257,4 +267,4 @@ func TestExecutor_SubCommand_NilHandler_ReturnsPassthrough(t *testing.T) {
|
||||||
if res.Outcome != OutcomePassthrough {
|
if res.Outcome != OutcomePassthrough {
|
||||||
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomePassthrough)
|
t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomePassthrough)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue