Log errors instead of panic
This commit is contained in:
parent
bdd2d48a87
commit
9c94cafe95
3 changed files with 14 additions and 5 deletions
|
@ -45,8 +45,10 @@ pub const utils = struct {
|
|||
try stream.finish();
|
||||
}
|
||||
|
||||
pub fn respondError(ctx: *http.server.Context, status: http.Status, err: []const u8) !void {
|
||||
return respondJson(ctx, status, .{ .@"error" = err });
|
||||
pub fn respondError(ctx: *http.server.Context, status: http.Status, err: []const u8) void {
|
||||
respondJson(ctx, status, .{ .@"error" = err }) catch |write_err| {
|
||||
std.log.err("Unable to print error: {}", .{write_err});
|
||||
};
|
||||
}
|
||||
|
||||
pub fn parseRequestBody(comptime T: type, ctx: *http.server.Context) !T {
|
||||
|
@ -87,5 +89,9 @@ pub fn healthcheck(_: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !
|
|||
}
|
||||
|
||||
pub fn notFound(_: *RequestServer, ctx: *http.server.Context) void {
|
||||
utils.respondError(ctx, .not_found, "Not Found") catch unreachable;
|
||||
utils.respondError(ctx, .not_found, "Not Found");
|
||||
}
|
||||
|
||||
pub fn internalServerError(_: *RequestServer, ctx: *http.server.Context) void {
|
||||
utils.respondError(ctx, .internal_server_error, "Internal Server Error");
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ pub fn register(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !v
|
|||
defer api.close();
|
||||
|
||||
const user = api.register(info) catch |err| switch (err) {
|
||||
error.UsernameUnavailable => return try utils.respondError(ctx, .bad_request, "Username Unavailable"),
|
||||
error.UsernameUnavailable => return utils.respondError(ctx, .bad_request, "Username Unavailable"),
|
||||
else => return err,
|
||||
};
|
||||
|
||||
|
|
|
@ -62,7 +62,10 @@ pub const RequestServer = struct {
|
|||
|
||||
router.dispatch(self, &ctx, ctx.request.method, ctx.request.path) catch |err| switch (err) {
|
||||
error.NotFound, error.RouteNotApplicable => c.notFound(self, &ctx),
|
||||
else => return err,
|
||||
else => {
|
||||
std.log.err("Unhandled error in controller ({s}): {}", .{ ctx.request.path, err });
|
||||
c.internalServerError(self, &ctx);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue