Skip to content

Commit

Permalink
feat: Include reason why parcel is denied in exception message (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored Jun 19, 2020
1 parent b1dfba1 commit 202a6bf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/lib/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ describe('deliverParcel', () => {
);
});

test('PoHTTPInvalidParcelError should mention the reason if available', async () => {
// @ts-ignore
stubAxiosPost.mockReset();
const reason = 'Denied';
stubAxiosPost.mockRejectedValueOnce({ response: { data: { message: reason }, status: 403 } });

await expect(deliverParcel(url, body)).rejects.toEqual(
new PoHTTPInvalidParcelError(`Server refused to accept parcel: ${reason}`),
);
});

test('Unexpected axios exceptions should be wrapped rethrown', async () => {
// @ts-ignore
stubAxiosPost.mockReset();
Expand Down
5 changes: 4 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ async function postRequest(
} catch (error) {
const responseStatus = error.response?.status;
if (responseStatus && responseStatus === 403) {
throw new PoHTTPInvalidParcelError('Server refused to accept parcel');
const reason = error.response?.data?.message;
throw new PoHTTPInvalidParcelError(
reason ? `Server refused to accept parcel: ${reason}` : 'Server refused to accept parcel',
);
}
if (!responseStatus || responseStatus < 300 || 400 <= responseStatus) {
throw new PoHTTPError(error, 'Failed to deliver parcel');
Expand Down

0 comments on commit 202a6bf

Please sign in to comment.