From f8502be58752abed20dba21fbe5c772a3d376995 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Sat, 20 Jan 2024 12:25:59 +0000 Subject: [PATCH] Log error to output and add to data --- README.md | 6 +++--- src/app/views/index.zig | 1 - src/jetzig/http/Server.zig | 4 +++- src/jetzig/http/status_codes.zig | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f4d2ebd..66256aa 100644 --- a/README.md +++ b/README.md @@ -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. -_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: @@ -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: _HTML_ and _JSON_ response (inferred from extension and/or `Accept` header). * :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: Sessions. * :white_check_mark: Cookies. -* :x: Error handling. +* :white_check_mark: Error handling. * :x: Headers (available but not yet wrapped). * :x: Param/JSON payload parsing/abstracting. * :x: Development-mode responses for debugging. diff --git a/src/app/views/index.zig b/src/app/views/index.zig index 18bf290..ad1eb80 100644 --- a/src/app/views/index.zig +++ b/src/app/views/index.zig @@ -8,6 +8,5 @@ const View = jetzig.views.View; pub fn index(request: *Request, data: *Data) anyerror!View { var object = try data.object(); try object.put("foo", data.string("hello")); - // return error.OhNo; return request.render(.ok); } diff --git a/src/jetzig/http/Server.zig b/src/jetzig/http/Server.zig index 21a5715..6c41cf4 100644 --- a/src/jetzig/http/Server.zig +++ b/src/jetzig/http/Server.zig @@ -201,6 +201,7 @@ fn renderView( template: ?jetzig.TemplateFn, ) !RenderedView { const view = matched_route.render(matched_route, request) catch |err| { + self.logger.debug("Encountered error: {s}", .{@errorName(err)}); switch (err) { error.OutOfMemory => return 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(); var object = try request.response_data.object(); try object.put("error", request.response_data.string(@errorName(err))); + return .{ .view = jetzig.views.View{ .data = request.response_data, .status_code = .internal_server_error }, - .content = "An unexpected error occurred.", + .content = "Internal Server Error\n", }; } diff --git a/src/jetzig/http/status_codes.zig b/src/jetzig/http/status_codes.zig index 535c630..cc4bad6 100644 --- a/src/jetzig/http/status_codes.zig +++ b/src/jetzig/http/status_codes.zig @@ -98,7 +98,7 @@ pub const TaggedStatusCode = union(StatusCode) { switching_protocols: StatusCodeType("101", "Switching Protocols"), processing: StatusCodeType("102", "Processing"), early_hints: StatusCodeType("103", "Early Hints"), - ok: StatusCodeType("200", "Ok"), + ok: StatusCodeType("200", "OK"), created: StatusCodeType("201", "Created"), accepted: StatusCodeType("202", "Accepted"), non_authoritative_info: StatusCodeType("203", "Non Authoritative Information"),