cdp: add send error options with session id by default

This commit is contained in:
Pierre Tachoire
2025-09-06 17:59:58 +02:00
parent a40590b4bf
commit 5ea97c4910
3 changed files with 11 additions and 7 deletions

View File

@@ -151,18 +151,18 @@ pub fn CDPT(comptime TypeProvider: type) type {
if (std.mem.eql(u8, input_session_id, "STARTUP")) {
is_startup = true;
} else if (self.isValidSessionId(input_session_id) == false) {
return command.sendError(-32001, "Unknown sessionId");
return command.sendError(-32001, "Unknown sessionId", .{});
}
}
if (is_startup) {
dispatchStartupCommand(&command) catch |err| {
command.sendError(-31999, @errorName(err)) catch {};
command.sendError(-31999, @errorName(err), .{}) catch {};
return err;
};
} else {
dispatchCommand(&command, input.method) catch |err| {
command.sendError(-31998, @errorName(err)) catch {};
command.sendError(-31998, @errorName(err), .{}) catch {};
return err;
};
}
@@ -757,10 +757,14 @@ pub fn Command(comptime CDP_T: type, comptime Sender: type) type {
return self.cdp.sendEvent(method, p, opts);
}
pub fn sendError(self: *Self, code: i32, message: []const u8) !void {
const SendErrorOpts = struct {
include_session_id: bool = true,
};
pub fn sendError(self: *Self, code: i32, message: []const u8, opts: SendErrorOpts) !void {
return self.sender.sendJSON(.{
.id = self.input.id,
.@"error" = .{ .code = code, .message = message },
.sessionId = if (opts.include_session_id) self.input.session_id else null,
});
}

View File

@@ -471,7 +471,7 @@ fn getFrameOwner(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const target_id = bc.target_id orelse return error.TargetNotLoaded;
if (std.mem.eql(u8, target_id, params.frameId) == false) {
return cmd.sendError(-32000, "Frame with the given id does not belong to the target.");
return cmd.sendError(-32000, "Frame with the given id does not belong to the target.", .{});
}
const page = bc.session.currentPage() orelse return error.PageNotLoaded;

View File

@@ -79,7 +79,7 @@ fn createBrowserContext(cmd: anytype) !void {
}
const bc = cmd.createBrowserContext() catch |err| switch (err) {
error.AlreadyExists => return cmd.sendError(-32000, "Cannot have more than one browser context at a time"),
error.AlreadyExists => return cmd.sendError(-32000, "Cannot have more than one browser context at a time", .{}),
else => return err,
};
@@ -102,7 +102,7 @@ fn disposeBrowserContext(cmd: anytype) !void {
})) orelse return error.InvalidParams;
if (cmd.cdp.disposeBrowserContext(params.browserContextId) == false) {
return cmd.sendError(-32602, "No browser context with the given id found");
return cmd.sendError(-32602, "No browser context with the given id found", .{});
}
try cmd.sendResult(null, .{});
}