1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

Merge pull request #92 from leroycep/error-return-trace

fix: use error return trace in senderror
This commit is contained in:
Rene Schallner 2024-04-21 13:48:58 +02:00 committed by GitHub
commit 789e6b02b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 7 deletions

View file

@ -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);
};
}

View file

@ -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);
}