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
11 changes: 11 additions & 0 deletions src/services/__tests__/tipService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ describe('tipService', () => {
);
});

it('uses the fallback message when the API error payload is not a string', async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
json: async () => ({ error: { detail: 'Bad request' } }),
});

await expect(sendTip({ recipientId: 'user-99', amount: 0.05 })).rejects.toThrow(
'Unable to send tip.',
);
});

it('throws when the API returns an error', async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
Expand Down
15 changes: 11 additions & 4 deletions src/services/tipService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ export async function sendTip(payload: TipPayload): Promise<TipSendResult> {
});

if (!response.ok) {
const responseBody = (await response.json().catch(() => null)) as {
error?: string;
message?: string;
const errorBody = (await response.json().catch(() => null)) as {
error?: unknown;
message?: unknown;
} | null;
const message = responseBody?.error ?? responseBody?.message ?? 'Unable to send tip.';

const message =
typeof errorBody?.error === 'string'
? errorBody.error
: typeof errorBody?.message === 'string'
? errorBody.message
: 'Unable to send tip.';

throw new Error(message);
}

Expand Down
Loading