Skip to content

Commit

Permalink
feat(node): show error code if available
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Mar 9, 2024
1 parent f572254 commit 2cce91d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
genRequestQueuesPolicy(),
genCombineGetRequestsPolicy(),
genRetryOnFailurePolicy(retryCount, retryOverallDelay),
genErrorFormatterPolicy((body: ErrorModel) => ` ${body.reason}`),
genErrorFormatterPolicy((body: ErrorModel) => [
' ', body.reason, body.errorCode == null ? '' : ` (${body.errorCode})`,
].join('')),
],
...options,
});
Expand Down
9 changes: 2 additions & 7 deletions src/utils/autorest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RestError, PipelineResponse, PipelinePolicy } from '@azure/core-rest-pipeline';
import { AdditionalPolicyConfig } from '@azure/core-client';
import { AdditionalPolicyConfig, FullOperationResponse } from '@azure/core-client';
import { pause } from './other';
import semverSatisfies from './semver-satisfies';
import { UnsupportedVersionError } from './errors';
Expand Down Expand Up @@ -81,12 +81,7 @@ export const genErrorFormatterPolicy = (
throw error;
}

let body;
try {
body = JSON.parse(error.response.bodyAsText);
} catch (e) {
body = null;
}
const body = (error.response as FullOperationResponse).parsedBody;
error.message = prefix;
const message = body == null ? ` ${error.response.status} status code` : getMessage(body);
if (message !== '') error.message += `:${message}`;
Expand Down
15 changes: 13 additions & 2 deletions test/integration/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { describe, it, before } from 'mocha';
import { expect } from 'chai';
import { createSandbox } from 'sinon';
import { RestError } from '@azure/core-rest-pipeline';
import { FullOperationResponse } from '@azure/core-client';
import { url } from '.';
import {
AeSdkBase, Node, NodeNotFoundError, ConsensusProtocolVersion,
AeSdkBase, Node, NodeNotFoundError, ConsensusProtocolVersion, MemoryAccount, buildTx, Tag,
} from '../../src';
import { bindRequestCounter } from '../utils';

Expand Down Expand Up @@ -39,7 +40,7 @@ describe('Node client', () => {
return await next(request);
} catch (error) {
if (!(error instanceof RestError) || error.response == null) throw error;
error.response.bodyAsText = '';
(error.response as FullOperationResponse).parsedBody = null;
throw error;
}
},
Expand Down Expand Up @@ -78,6 +79,16 @@ describe('Node client', () => {
sandbox.restore();
});

it('throws exception with code', async () => {
const account = MemoryAccount.generate();
const spendTx = buildTx({
tag: Tag.SpendTx, recipientId: account.address, senderId: account.address, nonce: 1e9,
});
const tx = await account.signTransaction(spendTx, { networkId: await node.getNetworkId() });
await expect(node.postTransaction({ tx }))
.to.be.rejectedWith(RestError, 'v3/transactions error: Invalid tx (nonce_too_high)');
});

describe('Node Pool', () => {
it('Throw error on using API without node', () => {
const nodes = new AeSdkBase({});
Expand Down

0 comments on commit 2cce91d

Please sign in to comment.