Skip to content

Commit a2d245f

Browse files
committed
Handle 404 using custom handler
1 parent bd35ea1 commit a2d245f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/server.zig

+7-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ pub fn run(allocator: std.mem.Allocator, indexes: *MultiIndex, address: []const
6464
var server = try Server.init(allocator, config, &ctx);
6565
defer server.deinit();
6666

67-
server.errorHandler(errorHandler);
67+
server.errorHandler(handleError);
68+
server.notFound(handleNotFound);
6869

6970
try installSignalHandlers(&server);
7071

@@ -179,7 +180,11 @@ const ErrorResponse = struct {
179180
}
180181
};
181182

182-
fn errorHandler(_: *Context, req: *httpz.Request, res: *httpz.Response, err: anyerror) void {
183+
fn handleNotFound(_: *Context, req: *httpz.Request, res: *httpz.Response) !void {
184+
try writeErrorResponse(404, error.NotFound, req, res);
185+
}
186+
187+
fn handleError(_: *Context, req: *httpz.Request, res: *httpz.Response, err: anyerror) void {
183188
log.err("unhandled error in {s}: {any}", .{ req.url.raw, err });
184189
writeErrorResponse(500, err, req, res) catch {
185190
res.status = 500;

0 commit comments

Comments
 (0)