Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,26 @@ describe("public dog trials api route", () => {
error: "Dog profile not found.",
});
});

it("returns structured errors when the service throws", async () => {
getBeagleDogTrialsMock.mockRejectedValue(new Error("boom"));

const { GET } = await import("../route");
const request = new NextRequest(
"http://localhost/api/beagle/dogs/dog_1/trials",
{
headers: { origin: "http://localhost:3000" },
},
);
const response = await GET(request, {
params: Promise.resolve({ dogId: "dog_1" }),
});

expect(response.status).toBe(500);
await expect(response.json()).resolves.toEqual({
ok: false,
error: "Failed to load dog trials.",
code: "INTERNAL_ERROR",
});
});
});
29 changes: 22 additions & 7 deletions apps/web/app/api/beagle/dogs/[dogId]/trials/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@ export async function OPTIONS(request: NextRequest) {
}

export async function GET(request: NextRequest, { params }: RouteParams) {
const { dogId } = await params;
const result = await dogsService.getBeagleDogTrials(dogId);
return jsonResponse(result.body, {
status: result.status,
methods: "GET,OPTIONS",
origin: request.headers.get("origin"),
});
try {
const { dogId } = await params;
const result = await dogsService.getBeagleDogTrials(dogId);
return jsonResponse(result.body, {
status: result.status,
methods: "GET,OPTIONS",
origin: request.headers.get("origin"),
});
} catch {
return jsonResponse(
{
ok: false,
error: "Failed to load dog trials.",
code: "INTERNAL_ERROR",
},
{
status: 500,
methods: "GET,OPTIONS",
origin: request.headers.get("origin"),
},
);
}
}
Loading