Skip to content

Commit

Permalink
fix(node): show correct error message if ECONNREFUSED
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 14, 2024
1 parent add6ee7 commit ef347a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/utils/autorest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,20 @@ export const genErrorFormatterPolicy = (
return await next(request);
} catch (error) {
if (!(error instanceof RestError) || error.request == null) throw error;
if (error.response?.bodyAsText == null) throw error;
const prefix = `${new URL(error.request.url).pathname.slice(1)} error`;

if (error.response?.bodyAsText == null) {
if (error.message === '') error.message = `${prefix}: ${error.code}`;
throw error;
}

let body;
try {
body = JSON.parse(error.response.bodyAsText);
} catch (e) {
body = null;
}
error.message = `${new URL(error.request.url).pathname.slice(1)} error`;
error.message = prefix;
const message = body == null ? ` ${error.response.status} status code` : getMessage(body);
if (message !== '') error.message += `:${message}`;
throw error;
Expand Down Expand Up @@ -123,7 +128,7 @@ export const genRetryOnFailurePolicy = (

const intervals = new Array(retryCount).fill(0)
.map((_, idx) => ((idx + 1) / retryCount) ** 2);
const intervalSum = intervals.reduce((a, b) => a + b);
const intervalSum = intervals.reduce((a, b) => a + b, 0);
const intervalsInMs = intervals.map((el) => (el / intervalSum) * retryOverallDelay);

let error = new RestError('Not expected to be thrown');
Expand Down
5 changes: 5 additions & 0 deletions test/integration/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ describe('Node client', () => {
node.pipeline.removePolicy({ name: 'remove-response-body' });
});

it('throws clear exceptions if ECONNREFUSED', async () => {
const n = new Node('http://localhost:60148', { retryCount: 0 });
await expect(n.getStatus()).to.be.rejectedWith('v3/status error: ECONNREFUSED');
});

it('retries requests if failed', async () => ([
['ak_test', 1],
['ak_2CxRaRcMUGn9s5UwN36UhdrtZVFUbgG1BSX5tUAyQbCNneUwti', 4],
Expand Down

0 comments on commit ef347a1

Please sign in to comment.