Skip to content

Commit a49154a

Browse files
committed
http_request_fail
1 parent 77eee7f commit a49154a

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/cdp/domains/network.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn putAssumeCapacity(headers: *std.ArrayListUnmanaged(std.http.Header), extra: s
203203
return true;
204204
}
205205

206-
pub fn httpRequestFail(arena: Allocator, bc: anytype, request: *const Notification.RequestFail) !void {
206+
pub fn httpRequestFail(arena: Allocator, bc: anytype, data: *const Notification.RequestFail) !void {
207207
// It's possible that the request failed because we aborted when the client
208208
// sent Target.closeTarget. In that case, bc.session_id will be cleared
209209
// already, and we can skip sending these messages to the client.
@@ -215,10 +215,10 @@ pub fn httpRequestFail(arena: Allocator, bc: anytype, request: *const Notificati
215215

216216
// We're missing a bunch of fields, but, for now, this seems like enough
217217
try bc.cdp.sendEvent("Network.loadingFailed", .{
218-
.requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{request.id}),
218+
.requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{data.request.id.?}),
219219
// Seems to be what chrome answers with. I assume it depends on the type of error?
220220
.type = "Ping",
221-
.errorText = request.err,
221+
.errorText = data.err,
222222
.canceled = false,
223223
}, .{ .session_id = session_id });
224224
}

src/http/Client.zig

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn abort(self: *Client) void {
151151
log.err(.http, "get private info", .{ .err = err, .source = "abort" });
152152
continue;
153153
};
154-
transfer.req.error_callback(transfer.ctx, error.Abort);
154+
self.requestFailed(&transfer.req, error.Abort);
155155
self.endTransfer(transfer);
156156
}
157157
std.debug.assert(self.active == 0);
@@ -221,6 +221,20 @@ pub fn blockingRequest(self: *Client, req: Request) !void {
221221
return self.makeRequest(&self.blocking, req);
222222
}
223223

224+
fn requestFailed(self: *Client, req: *Request, err: anyerror) void {
225+
if (req._notified_fail) return;
226+
req._notified_fail = true;
227+
228+
if (self.notification) |notification| {
229+
notification.dispatch(.http_request_fail, &.{
230+
.request = req,
231+
.err = err,
232+
});
233+
}
234+
235+
req.error_callback(req.ctx, err);
236+
}
237+
224238
// Restrictive since it'll only work if there are no inflight requests. In some
225239
// cases, the libcurl documentation is clear that changing settings while a
226240
// connection is inflight is undefined. It doesn't say anything about CURLOPT_PROXY,
@@ -326,7 +340,6 @@ fn perform(self: *Client, timeout_ms: c_int) !void {
326340
const transfer = try Transfer.fromEasy(easy);
327341
const ctx = transfer.ctx;
328342
const done_callback = transfer.req.done_callback;
329-
const error_callback = transfer.req.error_callback;
330343

331344
// release it ASAP so that it's available; some done_callbacks
332345
// will load more resources.
@@ -336,10 +349,10 @@ fn perform(self: *Client, timeout_ms: c_int) !void {
336349
done_callback(ctx) catch |err| {
337350
// transfer isn't valid at this point, don't use it.
338351
log.err(.http, "done_callback", .{ .err = err });
339-
error_callback(ctx, err);
352+
self.requestFailed(&transfer.req, err);
340353
};
341354
} else |err| {
342-
error_callback(ctx, err);
355+
self.requestFailed(&transfer.req, err);
343356
}
344357
}
345358
}
@@ -491,6 +504,8 @@ pub const Request = struct {
491504
body: ?[]const u8 = null,
492505
cookie_jar: *storage.CookieJar,
493506

507+
_notified_fail: bool = false,
508+
494509
// arbitrary data that can be associated with this request
495510
ctx: *anyopaque = undefined,
496511

src/notification.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ pub const Notification = struct {
103103
};
104104

105105
pub const RequestFail = struct {
106-
id: usize,
107-
url: *const std.Uri,
108-
err: []const u8,
106+
request: *Request,
107+
err: anyerror,
109108
};
110109

111110
pub const RequestComplete = struct {

0 commit comments

Comments
 (0)