Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 6 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /webmcp/imperative/executeTool-error-window-onerror.https.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<html>
<head>
<title>WebMCP failed tool execution does not fire window.onerror</title>
<link rel="author" href="mailto:dom@chromium.org">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
promise_test(async t => {
let errorFired = false;
const errorHandler = () => {
errorFired = true;
};
window.addEventListener('error', errorHandler);
t.add_cleanup(() => {
window.removeEventListener('error', errorHandler);
});
// Register a tool that throws an error when executed.
await document.modelContext.registerTool({
name: 'throwing_tool',
description: 'A tool that throws an error',
execute: async () => {
throw new Error('Tool execution failed intentionally');
}
});
const tools = await document.modelContext.getTools();
const tool = tools.find(t => t.name === 'throwing_tool');
assert_true(!!tool, 'Should find the registered tool');
// `executeTool()` should reject since the tool execution failed.
await promise_rejects_dom(t, 'OperationError',
document.modelContext.executeTool(tool),
'executeTool() rejects when the tool execution throws');
assert_false(errorFired, 'window.onerror/error event should not be fired');
}, 'Failed tool execution does not trigger window.onerror');
promise_test(async t => {
let errorFired = false;
const errorHandler = () => {
errorFired = true;
};
window.addEventListener('error', errorHandler);
t.add_cleanup(() => {
window.removeEventListener('error', errorHandler);
});
// Register a tool that returns a circular object when executed.
await document.modelContext.registerTool({
name: 'circular_tool',
description: 'A tool that returns a circular object',
execute: () => {
let a = {};
a['a'] = a;
return a;
}
});
const tools = await document.modelContext.getTools();
const tool = tools.find(t => t.name === 'circular_tool');
assert_true(!!tool, 'Should find the registered tool');
// `executeTool()` should reject since serializing the return value throws a TypeError.
await promise_rejects_dom(t, 'OperationError',
document.modelContext.executeTool(tool),
'executeTool() rejects when the tool execution returns a circular object');
assert_false(errorFired, 'window.onerror/error event should not be fired');
}, 'Tool execution returning circular object rejects and does not trigger window.onerror');
// Each case returns a value that can't become a tool result string: either
// JSON serialization (objects) or string conversion (primitives) throws.
const unserializableCases = [
{
name: 'symbol_tool',
description: 'a Symbol',
execute: () => Symbol('result'),
},
{
name: 'bigint_property_tool',
description: 'an object with a BigInt property',
execute: () => ({value: 1n}),
},
{
name: 'throwing_tojson_tool',
description: 'an object whose toJSON() throws',
execute: () => ({toJSON() { throw new Error('toJSON'); }}),
},
{
name: 'throwing_getter_tool',
description: 'an object with a throwing getter',
execute: () => ({get value() { throw new Error('getter'); }}),
},
];
for (const {name, description, execute} of unserializableCases) {
promise_test(async t => {
let errorFired = false;
const errorHandler = () => {
errorFired = true;
};
window.addEventListener('error', errorHandler);
t.add_cleanup(() => {
window.removeEventListener('error', errorHandler);
});
await document.modelContext.registerTool({
name,
description: `A tool that returns ${description}`,
execute
});
const tools = await document.modelContext.getTools();
const tool = tools.find(t => t.name === name);
assert_true(!!tool, 'Should find the registered tool');
await promise_rejects_dom(t, 'OperationError',
document.modelContext.executeTool(tool),
`executeTool() rejects when the tool execution returns ${description}`);
assert_false(errorFired, 'window.onerror/error event should not be fired');
}, `Tool execution returning ${description} rejects and does not trigger window.onerror`);
}
</script>
</body>
</html>