fix(staples): forward backend error status code instead of always 500
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -21,7 +21,8 @@ export const PATCH: RequestHandler = async ({ request, fetch }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return json({ error: 'Failed to update ingredient' }, { status: 500 });
|
const status = (error as { status?: number }).status ?? 500;
|
||||||
|
return json({ error: 'Failed to update ingredient' }, { status });
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(null, { status: 204 });
|
return new Response(null, { status: 204 });
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ describe('household staples PATCH handler', () => {
|
|||||||
expect(response.status).toBe(204);
|
expect(response.status).toBe(204);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns 500 when backend returns an error', async () => {
|
it('returns 500 when backend returns a 500 error', async () => {
|
||||||
mockPatch.mockResolvedValue({ data: undefined, error: { status: 500, message: 'error' } });
|
mockPatch.mockResolvedValue({ data: undefined, error: { status: 500, message: 'error' } });
|
||||||
|
|
||||||
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: false }));
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: false }));
|
||||||
@@ -54,6 +54,14 @@ describe('household staples PATCH handler', () => {
|
|||||||
expect(response.status).toBe(500);
|
expect(response.status).toBe(500);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('forwards backend 404 status when ingredient not found', async () => {
|
||||||
|
mockPatch.mockResolvedValue({ data: undefined, error: { status: 404 } });
|
||||||
|
|
||||||
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: false }));
|
||||||
|
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
it('returns 400 when id is missing', async () => {
|
it('returns 400 when id is missing', async () => {
|
||||||
const response = await PATCH(createRequest({ isStaple: true }));
|
const response = await PATCH(createRequest({ isStaple: true }));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user