mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-02 05:10:00 +08:00
fix(browser): cdp 里页面函数抛错时按失败处理并停止后续命令
Codex 评审(P1):原始 Runtime.callFunctionOn 在页面里抛错时,协议响应本身是成功的, 异常放在 exceptionDetails 里。之前这种情况被记成成功,后面的命令(例如提交点击)会在 没改动的数据上继续执行,外层 batch 也不会停。 把检查放进统一的发送函数:任何命令(辅助命令或原始命令)的响应带 exceptionDetails 都 按失败处理,抛出页面里的异常描述,序列和外层 batch 都在这里停下。 Assisted-by: Cindy (https://cindy.app) Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
This commit is contained in:
@@ -2060,6 +2060,34 @@ describe('browser · batch, cdp and target-tailored rules', () => {
|
||||
} finally { await h.cleanup(); }
|
||||
});
|
||||
|
||||
// Codex review on #9701 (P1): a raw Runtime.callFunctionOn that throws in the page comes back as a
|
||||
// successful protocol response carrying exceptionDetails. It used to be recorded as success, so the
|
||||
// next command (a submit click) ran on unchanged data, and a batch around it went on too.
|
||||
it('stops cdp and the batch around it when a raw page function throws', async () => {
|
||||
const h = await scripted((request) => {
|
||||
const command = request.recipeCommand as { method: string };
|
||||
if (command.method === 'Cindy.begin') return { data: { sessionId: 's1' } };
|
||||
if (command.method === 'Runtime.callFunctionOn') {
|
||||
return { data: { result: { type: 'object' }, exceptionDetails: { text: 'Uncaught', exception: { description: 'TypeError: x is not a function' } } } };
|
||||
}
|
||||
return { data: {} };
|
||||
});
|
||||
try {
|
||||
const { isError, body } = await h.invoke({ action: 'batch', targetId: 't1', steps: [
|
||||
{ action: 'cdp', commands: [
|
||||
{ method: 'Runtime.callFunctionOn', params: { objectId: 'o1', functionDeclaration: 'function(){ x() }' } },
|
||||
{ method: 'Input.dispatchMouseEvent', params: { type: 'mousePressed', x: 1, y: 1 } },
|
||||
] },
|
||||
{ action: 'navigate', url: 'https://example.test/next' },
|
||||
] });
|
||||
expect(isError).toBe(true);
|
||||
expect(body).toMatchObject({ ok: false, completed: 0, failedStep: 0 });
|
||||
expect(body.steps[0].data.results).toEqual([{ method: 'Runtime.callFunctionOn', ok: false, error: 'TypeError: x is not a function' }]);
|
||||
const sent = h.calls.map((request) => request.action === 'recipeCommand' ? (request.recipeCommand as { method: string }).method : request.action);
|
||||
expect(sent).toEqual(['recipeCommand', 'recipeCommand', 'recipeCommand'].map((_, i) => ['Cindy.begin', 'Runtime.callFunctionOn', 'Cindy.end'][i]));
|
||||
} finally { await h.cleanup(); }
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ commands: [{ method: 'nodes' }, { method: 'Network.getCookies' }] }, {}, 'Cookie'],
|
||||
[{ commands: [{ method: 'nodes' }] }, { backendCapabilities: () => ({ rawProtocol: false }) }, '不要自行切换'],
|
||||
|
||||
@@ -169,7 +169,13 @@ export async function runCdpCommands(
|
||||
action: 'recipeCommand', ...base, targetId, recipeCommand: { method, sessionId, ...(params ? { params } : {}) },
|
||||
} as BrowserControlRequest);
|
||||
if (!sent.ok) throw new Error(sent.message ?? `${method} failed`);
|
||||
return (sent.data ?? {}) as Record<string, unknown>;
|
||||
const data = (sent.data ?? {}) as Record<string, unknown>;
|
||||
// A script that throws in the page still comes back as a successful protocol response, with the
|
||||
// exception beside the result. Any command, helper or raw, counts as failed then, so the sequence
|
||||
// (and a batch around it) stops instead of going on to e.g. a submit click on unchanged data.
|
||||
const failure = data.exceptionDetails as { exception?: { description?: unknown }; text?: unknown } | undefined;
|
||||
if (failure) throw new Error(String(failure.exception?.description ?? failure.text ?? '页面里的函数执行出错').slice(0, 500));
|
||||
return data;
|
||||
};
|
||||
const callOn = async (backendNodeId: number, fn: string, args: unknown[] = []): Promise<unknown> => {
|
||||
const resolved = await send('DOM.resolveNode', { backendNodeId });
|
||||
@@ -180,8 +186,6 @@ export async function runCdpCommands(
|
||||
objectId, functionDeclaration: fn, returnByValue: true, awaitPromise: true, userGesture: true,
|
||||
...(args.length ? { arguments: args.map((value) => ({ value })) } : {}),
|
||||
});
|
||||
const failure = called.exceptionDetails as { exception?: { description?: unknown }; text?: unknown } | undefined;
|
||||
if (failure) throw new Error(String(failure.exception?.description ?? failure.text ?? '函数执行出错').slice(0, 500));
|
||||
return (called.result as { value?: unknown } | undefined)?.value;
|
||||
} finally {
|
||||
await send('Runtime.releaseObject', { objectId }).catch(() => {});
|
||||
|
||||
Reference in New Issue
Block a user