Skip to content

Commit

Permalink
fix: use error return trace in senderror
Browse files Browse the repository at this point in the history
This should produce more usable error messages. The error messages before
this were pointing at zap runtime functions, instead of at the code that
produced the error.
  • Loading branch information
leroycep committed Apr 18, 2024
1 parent 518199f commit 5f4b795
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/senderror/senderror.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn MAKE_MEGA_ERROR() !void {

fn MY_REQUEST_HANDLER(r: zap.Request) void {
MAKE_MEGA_ERROR() catch |err| {
r.sendError(err, 505);
r.sendError(err, if (@errorReturnTrace()) |t| t.* else null, 505);
};
}

Expand Down
15 changes: 9 additions & 6 deletions src/request.zig
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,17 @@ pub fn getUserContext(self: *const Self, comptime Context: type) ?*Context {
}

/// Tries to send an error stack trace.
pub fn sendError(self: *const Self, err: anyerror, errorcode_num: usize) void {
pub fn sendError(self: *const Self, err: anyerror, err_trace: ?std.builtin.StackTrace, errorcode_num: usize) void {
// TODO: query accept headers
if (self._internal_sendError(err, errorcode_num)) {
if (self._internal_sendError(err, err_trace, errorcode_num)) {
return;
} else |_| {
self.sendBody(@errorName(err)) catch return;
}
}

/// Used internally. Probably does not need to be public.
pub fn _internal_sendError(self: *const Self, err: anyerror, errorcode_num: usize) !void {
pub fn _internal_sendError(self: *const Self, err: anyerror, err_trace: ?std.builtin.StackTrace, errorcode_num: usize) !void {
// TODO: query accept headers
// TODO: let's hope 20k is enough. Maybe just really allocate here
self.h.*.status = errorcode_num;
Expand All @@ -339,9 +339,12 @@ pub fn _internal_sendError(self: *const Self, err: anyerror, errorcode_num: usiz
var writer = string.writer();
try writer.print("ERROR: {any}\n\n", .{err});

const debugInfo = try std.debug.getSelfDebugInfo();
const ttyConfig: std.io.tty.Config = .no_color;
try std.debug.writeCurrentStackTrace(writer, debugInfo, ttyConfig, null);
if (err_trace) |trace| {
const debugInfo = try std.debug.getSelfDebugInfo();
const ttyConfig: std.io.tty.Config = .no_color;
try std.debug.writeStackTrace(trace, writer, fba.allocator(), debugInfo, ttyConfig);
}

try self.sendBody(string.items);
}

Expand Down

0 comments on commit 5f4b795

Please sign in to comment.