Skip to content

Commit

Permalink
🧹 Added logging of body for failed requests (#1510)
Browse files Browse the repository at this point in the history
* Added logging of body for failed requests

* Fixed some specs
  • Loading branch information
ninadbstack authored Jan 31, 2024
1 parent 0a86255 commit 059074f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/client/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ export async function request(url, options = {}, callback) {
resolve(await callback?.(body, res) ?? body);
} else {
let err = body?.errors?.find(e => e.detail)?.detail;
throw new Error(err || `${statusCode} ${res.statusMessage || raw}`);
let statusMessage = `${statusCode} ${(res.statusMessage || '')}`;
let bodyText = (raw?.length > 0 && res.statusMessage !== raw) ? `\n${raw}` : '';
throw new Error(err || `${statusMessage}${bodyText}`);
}
} catch (error) {
let response = { statusCode, headers, body };
Expand Down
4 changes: 2 additions & 2 deletions packages/client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ describe('PercyClient', () => {

it('gets comparison data throw 500', async () => {
api.reply('/comparisons/104?sync=true&response_format=sync-cli', () => [500, { error: '<<comparison-data-failure>>' }]);
await expectAsync(client.getComparisonDetails(104)).toBeRejectedWithError('500 {"error":"<<comparison-data-failure>>"}');
await expectAsync(client.getComparisonDetails(104)).toBeRejectedWithError('500 \n{"error":"<<comparison-data-failure>>"}');
});
});

Expand All @@ -333,7 +333,7 @@ describe('PercyClient', () => {

it('gets snapshot data throw 500', async () => {
api.reply('/snapshots/104?sync=true&response_format=sync-cli', () => [500, { error: '<<snapshot-data-failure>>' }]);
await expectAsync(client.getSnapshotDetails(104)).toBeRejectedWithError('500 {"error":"<<snapshot-data-failure>>"}');
await expectAsync(client.getSnapshotDetails(104)).toBeRejectedWithError('500 \n{"error":"<<snapshot-data-failure>>"}');
});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/client/test/unit/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ describe('Unit / Request', () => {
server.reply('/status', () => [403]);

await expectAsync(server.request('/status'))
.toBeRejectedWithError('403 Forbidden');
.toBeRejectedWithError('403 Forbidden\ntest');

// empty status message
server.reply('/raw', (req, res) => {
res.writeHead(403, '', {}).end('STOP');
});

await expectAsync(server.request('/raw'))
.toBeRejectedWithError('403 STOP');
.toBeRejectedWithError('403 \nSTOP');
});

describe('retries', () => {
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('Unit / Request', () => {
server.reply('/test', () => responses.splice(0, 1)[0]);

await expectAsync(server.request('/test'))
.toBeRejectedWithError('404 Not Found');
.toBeRejectedWithError('404 Not Found\ntest');

expect(responses).toEqual([[500], [404], [200]]);
expect(server.received.length).toBe(2);
Expand All @@ -261,7 +261,7 @@ describe('Unit / Request', () => {
it('fails after 5 additional retries', async () => {
server.reply('/fail', () => [502]);
await expectAsync(server.request('/fail'))
.toBeRejectedWithError('502 Bad Gateway');
.toBeRejectedWithError('502 Bad Gateway\ntest');
expect(server.received.length).toBe(6);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('Unit / Server', () => {
it('handles server errors', async () => {
server.route('/e/foo', () => { throw new Error('foo'); });
server.route('/e/bar', () => { throw new Server.Error(418); });
await expectAsync(request('/e/foo')).toBeRejectedWithError('500 Internal Server Error');
await expectAsync(request('/e/foo')).toBeRejectedWithError('500 Internal Server Error\nfoo');
await expectAsync(request('/e/bar')).toBeRejectedWithError('418 I\'m a Teapot');
});

Expand Down

0 comments on commit 059074f

Please sign in to comment.