Better error messages

This commit is contained in:
jaina heartles 2022-07-12 21:31:52 -07:00
parent 44cb7017b2
commit d27c38330b

View file

@ -50,7 +50,7 @@ fn respondJson(ctx: *http.server.Context, status: http.Status, value: anytype, a
} }
fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const body = ctx.request.body orelse return respondJson(ctx, .bad_request, .{ .@"error" = "no note body provided" }, srv.alloc); const body = ctx.request.body orelse return respondJson(ctx, .bad_request, .{ .@"error" = "'content' cannot be empty" }, srv.alloc);
var tokens = std.json.TokenStream.init(body); var tokens = std.json.TokenStream.init(body);
const info = try std.json.parse(api.CreateInfo(models.Note), &tokens, .{ .allocator = srv.alloc }); const info = try std.json.parse(api.CreateInfo(models.Note), &tokens, .{ .allocator = srv.alloc });
defer std.json.parseFree(api.CreateInfo(models.Note), info, .{ .allocator = srv.alloc }); defer std.json.parseFree(api.CreateInfo(models.Note), info, .{ .allocator = srv.alloc });
@ -62,8 +62,8 @@ fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !voi
fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { fn getNote(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
const id_str = args.get("id") orelse return error.NotFound; const id_str = args.get("id") orelse return error.NotFound;
const id = Uuid.parse(id_str) catch return respondJson(ctx, .bad_request, .{ .@"error" = "invalid id" }, srv.alloc); const id = Uuid.parse(id_str) catch return respondJson(ctx, .bad_request, .{ .@"error" = "Invalid UUID" }, srv.alloc);
const note = (try srv.api.getNote(id, srv.alloc)) orelse return error.NotFound; const note = (try srv.api.getNote(id, srv.alloc)) orelse return respondJson(ctx, .not_found, .{ .@"error" = "Note not found" }, srv.alloc);
defer api.free(srv.alloc, note); defer api.free(srv.alloc, note);
try respondJson(ctx, .ok, note, srv.alloc); try respondJson(ctx, .ok, note, srv.alloc);