Skip to content

Commit

Permalink
fix(node): show correct error message if body is missed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 13, 2024
1 parent 92f3ac1 commit ed10482
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/channel/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class Channel {

_options!: ChannelOptions;

#debugId: number;
readonly #debugId: number;

_channelId?: Encoded.Channel;

Expand Down
4 changes: 2 additions & 2 deletions src/utils/autorest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export const genErrorFormatterPolicy = (
try {
body = JSON.parse(error.response.bodyAsText);
} catch (e) {
throw error;
body = null;
}
error.message = `${new URL(error.request.url).pathname.slice(1)} error`;
const message = getMessage(body);
const message = body == null ? ` ${error.response.status} status code` : getMessage(body);
if (message !== '') error.message += `:${message}`;
throw error;
}
Expand Down
22 changes: 21 additions & 1 deletion test/integration/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, it, before } from 'mocha';
import { expect } from 'chai';
import { createSandbox } from 'sinon';
import { PipelineRequest, PipelineResponse, SendRequest } from '@azure/core-rest-pipeline';
import {
PipelineRequest, PipelineResponse, RestError, SendRequest,
} from '@azure/core-rest-pipeline';
import { url } from '.';
import {
AeSdkBase, Node, NodeNotFoundError, ConsensusProtocolVersion,
Expand Down Expand Up @@ -30,6 +32,24 @@ describe('Node client', () => {
.to.be.rejectedWith('v3/transactions/th_test error: Invalid hash');
});

it('throws clear exceptions when body is empty', async () => {
node.pipeline.addPolicy({
name: 'remove-response-body',
async sendRequest(request, next) {
try {
return await next(request);
} catch (error) {
if (!(error instanceof RestError) || error.response == null) throw error;
error.response.bodyAsText = '';
throw error;
}
},
});
await expect(node.getTransactionByHash('th_test'))
.to.be.rejectedWith('v3/transactions/th_test error: 400 status code');
node.pipeline.removePolicy({ name: 'remove-response-body' });
});

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

0 comments on commit ed10482

Please sign in to comment.