From 5f4b7957dcb109f619a528abd6e94d7af0f06812 Mon Sep 17 00:00:00 2001 From: geemili Date: Thu, 18 Apr 2024 16:16:15 -0600 Subject: [PATCH] fix: use error return trace in senderror 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. --- examples/senderror/senderror.zig | 2 +- src/request.zig | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/senderror/senderror.zig b/examples/senderror/senderror.zig index 9a2fe73a..a9756ce1 100644 --- a/examples/senderror/senderror.zig +++ b/examples/senderror/senderror.zig @@ -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); }; } diff --git a/src/request.zig b/src/request.zig index 75a3be7f..64850037 100644 --- a/src/request.zig +++ b/src/request.zig @@ -319,9 +319,9 @@ 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; @@ -329,7 +329,7 @@ pub fn sendError(self: *const Self, err: anyerror, errorcode_num: usize) void { } /// 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; @@ -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); }