Skip to content

Commit

Permalink
add 404 return if not data exists
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Sep 26, 2024
1 parent 60e5672 commit 26fd0e0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/types/zod.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from "zod";


export const apiErrorSchema = z.object({ "status": z.union([z.literal(500), z.literal(504), z.literal(400), z.literal(401), z.literal(403), z.literal(404), z.literal(405)]), "code": z.enum(["bad_database_response", "bad_header", "missing_required_header", "bad_query_input", "database_timeout", "forbidden", "internal_server_error", "method_not_allowed", "route_not_found", "unauthorized"]), "message": z.coerce.string() });
export const apiErrorSchema = z.object({ "status": z.union([z.literal(500), z.literal(504), z.literal(400), z.literal(401), z.literal(403), z.literal(404), z.literal(405)]), "code": z.enum(["bad_database_response", "bad_header", "missing_required_header", "bad_query_input", "database_timeout", "forbidden", "internal_server_error", "method_not_allowed", "route_not_found", "unauthorized", "not_found_data"]), "message": z.coerce.string() });
export type ApiErrorSchema = z.infer<typeof apiErrorSchema>;


Expand Down
3 changes: 2 additions & 1 deletion src/typespec/openapi3.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ alias ApiErrorCode =
| "internal_server_error" // an unknown error occurred on the backend
| "method_not_allowed" // http method is not allowed on this endpoint
| "route_not_found" // the requested route was not found
| "unauthorized"; // invalid authorization information given
| "unauthorized" // invalid authorization information given
| "not_found_data"; // no data found for the given query

alias ErrorStatusCode = 500 | 504 | 400 | 401 | 403 | 404 | 405;

Expand Down
6 changes: 5 additions & 1 deletion src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use
if (query_params.offset) query.push("OFFSET {offset: int}");

try {
return ctx.json(await makeQuery<UsageElementReturnType>(query.join(" "), { ...query_params }));
const result = await makeQuery<UsageElementReturnType>(query.join(" "), { ...query_params });
if (result.data.length === 0) {
return APIErrorResponse(ctx, 404, "not_found_data", `No data found for ${table_name}`);
}
return ctx.json(result);
} catch (err) {
return APIErrorResponse(ctx, 500, "bad_database_response", err);
}
Expand Down
3 changes: 2 additions & 1 deletion static/@typespec/openapi3/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,8 @@
"internal_server_error",
"method_not_allowed",
"route_not_found",
"unauthorized"
"unauthorized",
"not_found_data"
]
},
"message": {
Expand Down

0 comments on commit 26fd0e0

Please sign in to comment.