jetzig/demo/src/app/views/errors.zig
Bob Farrell 1c7df8a91d Custom error pages
Render `src/app/views/errors.zig` view (`index` action) when an
unexpected error occurs. If the view does not exist, try rendering
`public/404.html`, `public/500.html` (etc.), finally falling back to a
standard basic HTML/JSON response.
2024-04-28 11:54:05 +01:00

21 lines
777 B
Zig

const std = @import("std");
const jetzig = @import("jetzig");
// Generic handler for all errors.
// Use `jetzig.http.status_codes.get(request.status_code)` to get a value that provides string
// versions of the error code and message for use in templates.
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object();
var error_info = try data.object();
const status = jetzig.http.status_codes.get(request.status_code);
try error_info.put("code", data.string(status.getCode()));
try error_info.put("message", data.string(status.getMessage()));
try root.put("error", error_info);
// Render with the original error status code, or override if preferred.
return request.render(request.status_code);
}