Log error to output and add to data

This commit is contained in:
Bob Farrell 2024-01-20 12:25:59 +00:00
parent b66073955d
commit f8502be587
4 changed files with 7 additions and 6 deletions

View File

@ -4,7 +4,7 @@ _Jetzig_ is a web framework written in [Zig](https://ziglang.org) :lizard:.
The framework is under active development and is currently in an alpha state. The framework is under active development and is currently in an alpha state.
_Jetzig_ is an ambitious and opinionated web framework. It aims to provide a rich set of user-friendly tools for building modern web applications quickly. See the checklist below. _Jetzig_ aims to provide a rich set of user-friendly tools for building modern web applications quickly. See the checklist below.
If you are interested in _Jetzig_ you will probably find these tools interesting too: If you are interested in _Jetzig_ you will probably find these tools interesting too:
@ -16,11 +16,11 @@ If you are interested in _Jetzig_ you will probably find these tools interesting
* :white_check_mark: File system-based routing with [slug] matching. * :white_check_mark: File system-based routing with [slug] matching.
* :white_check_mark: _HTML_ and _JSON_ response (inferred from extension and/or `Accept` header). * :white_check_mark: _HTML_ and _JSON_ response (inferred from extension and/or `Accept` header).
* :white_check_mark: _JSON_-compatible response data builder. * :white_check_mark: _JSON_-compatible response data builder.
* :white_check_mark: _HTML_ templating (see [Zmpl](https://github.com/bobf/zmpl)). * :white_check_mark: _HTML_ templating (see [Zmpl](https://github.com/jetzig-framework/zmpl)).
* :white_check_mark: Per-request arena allocator. * :white_check_mark: Per-request arena allocator.
* :white_check_mark: Sessions. * :white_check_mark: Sessions.
* :white_check_mark: Cookies. * :white_check_mark: Cookies.
* :x: Error handling. * :white_check_mark: Error handling.
* :x: Headers (available but not yet wrapped). * :x: Headers (available but not yet wrapped).
* :x: Param/JSON payload parsing/abstracting. * :x: Param/JSON payload parsing/abstracting.
* :x: Development-mode responses for debugging. * :x: Development-mode responses for debugging.

View File

@ -8,6 +8,5 @@ const View = jetzig.views.View;
pub fn index(request: *Request, data: *Data) anyerror!View { pub fn index(request: *Request, data: *Data) anyerror!View {
var object = try data.object(); var object = try data.object();
try object.put("foo", data.string("hello")); try object.put("foo", data.string("hello"));
// return error.OhNo;
return request.render(.ok); return request.render(.ok);
} }

View File

@ -201,6 +201,7 @@ fn renderView(
template: ?jetzig.TemplateFn, template: ?jetzig.TemplateFn,
) !RenderedView { ) !RenderedView {
const view = matched_route.render(matched_route, request) catch |err| { const view = matched_route.render(matched_route, request) catch |err| {
self.logger.debug("Encountered error: {s}", .{@errorName(err)});
switch (err) { switch (err) {
error.OutOfMemory => return err, error.OutOfMemory => return err,
else => return try self.internalServerError(request, err), else => return try self.internalServerError(request, err),
@ -216,9 +217,10 @@ fn internalServerError(self: *Self, request: *jetzig.http.Request, err: anyerror
request.response_data.reset(); request.response_data.reset();
var object = try request.response_data.object(); var object = try request.response_data.object();
try object.put("error", request.response_data.string(@errorName(err))); try object.put("error", request.response_data.string(@errorName(err)));
return .{ return .{
.view = jetzig.views.View{ .data = request.response_data, .status_code = .internal_server_error }, .view = jetzig.views.View{ .data = request.response_data, .status_code = .internal_server_error },
.content = "An unexpected error occurred.", .content = "Internal Server Error\n",
}; };
} }

View File

@ -98,7 +98,7 @@ pub const TaggedStatusCode = union(StatusCode) {
switching_protocols: StatusCodeType("101", "Switching Protocols"), switching_protocols: StatusCodeType("101", "Switching Protocols"),
processing: StatusCodeType("102", "Processing"), processing: StatusCodeType("102", "Processing"),
early_hints: StatusCodeType("103", "Early Hints"), early_hints: StatusCodeType("103", "Early Hints"),
ok: StatusCodeType("200", "Ok"), ok: StatusCodeType("200", "OK"),
created: StatusCodeType("201", "Created"), created: StatusCodeType("201", "Created"),
accepted: StatusCodeType("202", "Accepted"), accepted: StatusCodeType("202", "Accepted"),
non_authoritative_info: StatusCodeType("203", "Non Authoritative Information"), non_authoritative_info: StatusCodeType("203", "Non Authoritative Information"),