[Bug] processToolUse throws unhandled error on __xml_validation_error__ and missing tools instead of returning error ToolResult
Description
In source/message-handler.ts, processToolUse contains checks for XML validation errors (__xml_validation_error__), uninitialized tool registry, and unknown tools. A code comment above them states: // Handle XML validation errors by throwing (will be caught and returned as error ToolResult). However, these checks are positioned on lines 57–70, which is before the try { block on line 72. When these conditions occur, the error is thrown outside the try/catch and causes an unhandled promise rejection rather than returning a formatted { role: 'tool', isError: true, content: 'Error: ...' } message.
Environment
- OS: Linux / macOS / Windows
- Node version: v20+ / v22+
- Nanocoder version: 1.30.0
- Provider: Any non-native tool calling model using fallback XML tool format
- Model: Small local models (e.g. Qwen2.5-Coder, Llama-3.2)
Steps to Reproduce
- Run Nanocoder with a model using XML fallback tool calling (
/tune tool-mode: xml). - Induce the model to emit a malformed XML tool call that triggers
__xml_validation_error__. processToolUsethrows an uncaught error rather than returning a tool result indicating the XML syntax error to the model.
Expected Behavior
processToolUse should catch the error and return a ToolResult with isError: true so the model can self-correct on the next turn.
Actual Behavior
An uncaught exception is thrown out of processToolUse.
Logs/Screenshots
In source/message-handler.ts:
export async function processToolUse(
toolCall: ToolCall,
options?: ToolExecutionContext,
): Promise<ToolResult> {
// Lines 57-70 are OUTSIDE the try block!
if (toolCall.function.name === '__xml_validation_error__') {
const args = toolCall.function.arguments as {error: string};
throw new Error(args.error);
}
if (!toolRegistryGetter) throw new Error('Tool registry not initialized');
const handler = toolRegistry[toolCall.function.name];
if (!handler) throw new Error(`Unknown tool: ${toolCall.function.name}`);
try {
...
} catch (error) {
...
}
}
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] I can reproduce this issue consistently
- [x] This issue occurs with the latest version of nanocoder
Picking this up - a fix will follow shortly.
Hi @addyCooks, commenting here as you asked so you can assign this to me. My findings for this one are in #1300. I'll wait for your go-ahead on the approach before opening a PR.