Skip to content

Commit 3c7ebb3

Browse files
committed
cdp: add send error options with session id by default
1 parent 2db4ecd commit 3c7ebb3

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/cdp/cdp.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,18 @@ pub fn CDPT(comptime TypeProvider: type) type {
151151
if (std.mem.eql(u8, input_session_id, "STARTUP")) {
152152
is_startup = true;
153153
} else if (self.isValidSessionId(input_session_id) == false) {
154-
return command.sendError(-32001, "Unknown sessionId");
154+
return command.sendError(-32001, "Unknown sessionId", .{});
155155
}
156156
}
157157

158158
if (is_startup) {
159159
dispatchStartupCommand(&command) catch |err| {
160-
command.sendError(-31999, @errorName(err)) catch {};
160+
command.sendError(-31999, @errorName(err), .{}) catch {};
161161
return err;
162162
};
163163
} else {
164164
dispatchCommand(&command, input.method) catch |err| {
165-
command.sendError(-31998, @errorName(err)) catch {};
165+
command.sendError(-31998, @errorName(err), .{}) catch {};
166166
return err;
167167
};
168168
}
@@ -757,10 +757,14 @@ pub fn Command(comptime CDP_T: type, comptime Sender: type) type {
757757
return self.cdp.sendEvent(method, p, opts);
758758
}
759759

760-
pub fn sendError(self: *Self, code: i32, message: []const u8) !void {
760+
const SendErrorOpts = struct {
761+
include_session_id: bool = true,
762+
};
763+
pub fn sendError(self: *Self, code: i32, message: []const u8, opts: SendErrorOpts) !void {
761764
return self.sender.sendJSON(.{
762765
.id = self.input.id,
763766
.@"error" = .{ .code = code, .message = message },
767+
.sessionId = if (opts.include_session_id) self.input.session_id else null,
764768
});
765769
}
766770

src/cdp/domains/dom.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ fn getFrameOwner(cmd: anytype) !void {
471471
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
472472
const target_id = bc.target_id orelse return error.TargetNotLoaded;
473473
if (std.mem.eql(u8, target_id, params.frameId) == false) {
474-
return cmd.sendError(-32000, "Frame with the given id does not belong to the target.");
474+
return cmd.sendError(-32000, "Frame with the given id does not belong to the target.", .{});
475475
}
476476

477477
const page = bc.session.currentPage() orelse return error.PageNotLoaded;

src/cdp/domains/target.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn createBrowserContext(cmd: anytype) !void {
7979
}
8080

8181
const bc = cmd.createBrowserContext() catch |err| switch (err) {
82-
error.AlreadyExists => return cmd.sendError(-32000, "Cannot have more than one browser context at a time"),
82+
error.AlreadyExists => return cmd.sendError(-32000, "Cannot have more than one browser context at a time", .{}),
8383
else => return err,
8484
};
8585

@@ -102,7 +102,7 @@ fn disposeBrowserContext(cmd: anytype) !void {
102102
})) orelse return error.InvalidParams;
103103

104104
if (cmd.cdp.disposeBrowserContext(params.browserContextId) == false) {
105-
return cmd.sendError(-32602, "No browser context with the given id found");
105+
return cmd.sendError(-32602, "No browser context with the given id found", .{});
106106
}
107107
try cmd.sendResult(null, .{});
108108
}

0 commit comments

Comments
 (0)