mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00

zap.Endpont.Listener and zap.App now support on_error callbacks: zap.Endpont.Listener.Settings contains an `on_error` optional callback field. zap.App supports those two callbacks: /// ```zig /// const MyContext = struct { /// // You may (optionally) define the following global handlers: /// pub fn unhandledRequest(_: *MyContext, _: Allocator, _: Request) anyerror!void {} /// pub fn unhandledError(_: *MyContext, _: Request, _: anyerror) void {} /// }; /// ``` The `endpoint` example has been updated to showcase `on_error`, and the new example `app_errors` showcases `Context.unhandledError`.
22 lines
842 B
Zig
22 lines
842 B
Zig
const std = @import("std");
|
|
const zap = @import("zap");
|
|
|
|
/// A simple endpoint listening on the /error route that causes an error on GET
|
|
/// requests, which gets logged to the response (=browser) by default
|
|
pub const ErrorEndpoint = @This();
|
|
|
|
path: []const u8 = "/error",
|
|
error_strategy: zap.Endpoint.ErrorStrategy = .log_to_response,
|
|
|
|
pub fn get(_: *ErrorEndpoint, _: zap.Request) !void {
|
|
// error_strategy is set to .log_to_response
|
|
// --> this error will be shown in the browser, with a nice error trace
|
|
return error.@"Oh-no!";
|
|
}
|
|
|
|
// unused:
|
|
pub fn post(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
pub fn put(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
pub fn delete(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
pub fn patch(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
pub fn options(_: *ErrorEndpoint, _: zap.Request) !void {}
|