Skip to content

Commit

Permalink
fix(node): don't throw unhandled exception if version check failed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 9, 2023
1 parent 0bec5b5 commit d4e5250
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export interface NodeInfo {
}

export default class Node extends (NodeTransformed as unknown as NodeTransformedApi) {
#networkIdPromise?: Promise<string>;
#networkIdPromise?: Promise<string | Error>;

/**
* @param url - Url for node API
Expand All @@ -134,8 +134,8 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed
});
if (!ignoreVersion) {
const statusPromise = this.getStatus();
const versionPromise = statusPromise.then(({ nodeVersion }) => nodeVersion);
this.#networkIdPromise = statusPromise.then(({ networkId }) => networkId);
const versionPromise = statusPromise.then(({ nodeVersion }) => nodeVersion, (error) => error);
this.#networkIdPromise = statusPromise.then(({ networkId }) => networkId, (error) => error);
this.pipeline.addPolicy(
genVersionCheckPolicy('node', '/v3/status', versionPromise, '6.2.0', '7.0.0'),
);
Expand All @@ -145,7 +145,9 @@ export default class Node extends (NodeTransformed as unknown as NodeTransformed

async getNetworkId(): Promise<string> {
this.#networkIdPromise ??= this.getStatus().then(({ networkId }) => networkId);
return this.#networkIdPromise;
const networkId = await this.#networkIdPromise;
if (networkId instanceof Error) throw networkId;
return networkId;
}

async getNodeInfo(): Promise<NodeInfo> {
Expand Down
3 changes: 2 additions & 1 deletion src/contract/compiler/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export default class CompilerHttp extends CompilerBase {
],
});
if (ignoreVersion !== true) {
const versionPromise = this.api.apiVersion().then(({ apiVersion }) => apiVersion);
const versionPromise = this.api.apiVersion()
.then(({ apiVersion }) => apiVersion, (error) => error);
this.api.pipeline.addPolicy(
genVersionCheckPolicy('compiler', '/api-version', versionPromise, '7.0.1', '8.0.0'),
);
Expand Down
6 changes: 4 additions & 2 deletions src/utils/autorest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ export const genErrorFormatterPolicy = (
export const genVersionCheckPolicy = (
name: string,
ignorePath: string,
versionPromise: Promise<string>,
versionPromise: Promise<string | Error>,
geVersion: string,
ltVersion: string,
): PipelinePolicy => ({
name: 'version-check',
async sendRequest(request, next) {
if (new URL(request.url).pathname === ignorePath) return next(request);
const args = [await versionPromise, geVersion, ltVersion] as const;
const version = await versionPromise;
if (version instanceof Error) throw version;
const args = [version, geVersion, ltVersion] as const;
if (!semverSatisfies(...args)) throw new UnsupportedVersionError(name, ...args);
return next(request);
},
Expand Down
19 changes: 19 additions & 0 deletions test/environment/node-unhandled-exception.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node
/* eslint-disable import/extensions */
import { Node, CompilerHttp } from '../../es/index.mjs';
import { pause } from '../../es/utils/other.mjs';
/* eslint-enable import/extensions */

const invalidUrl = 'https://404.aeternity.io';
const compiler = new CompilerHttp(invalidUrl);
const node = new Node(invalidUrl);

await pause(2000);

const message1 = await compiler.version().catch((error) => error.message);
const message2 = await node.getStatus().catch((error) => error.message);
if (message1 !== message2 || message2 !== 'getaddrinfo ENOTFOUND 404.aeternity.io') {
throw new Error('Invalid exception');
}

console.log('Failure of version check doesn\'t emit unhandled exception');
2 changes: 2 additions & 0 deletions test/examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ echo Run environment/node.mjs
./test/environment/node.mjs
echo Run environment/node.ts
./test/environment/node.ts
echo Run environment/node-unhandled-exception.mjs
./test/environment/node-unhandled-exception.mjs

echo Check typescript
cd ./test/environment/typescript/
Expand Down

0 comments on commit d4e5250

Please sign in to comment.