Skip to content

Commit

Permalink
Improve error message for VM require (#2512)
Browse files Browse the repository at this point in the history
* Improve error message for VM require

* Update error message, changelog

* Further improve error message
  • Loading branch information
stwiname authored Aug 1, 2024
1 parent 3c2be57 commit 072f3ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- A more useful error message when failing to require modules from the VM (#2512)

### Added
- Support for endpoint configs (#2511)
Expand Down
23 changes: 17 additions & 6 deletions packages/node-core/src/indexer/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Cache, Store} from '@subql/types-core';
import {levelFilter} from '@subql/utils';
import {last, merge} from 'lodash';
import {SourceMapConsumer, NullableMappedPosition} from 'source-map';
import {NodeVM, NodeVMOptions, VMScript} from 'vm2';
import {NodeVM, NodeVMOptions, VMError, VMScript} from 'vm2';
import {NodeConfig} from '../configure/NodeConfig';
import {getLogger} from '../logger';
import {timeout} from '../utils';
Expand Down Expand Up @@ -76,11 +76,22 @@ export class Sandbox extends NodeVM {
}

async runTimeout<T = unknown>(duration: number): Promise<T> {
return timeout(
this.run(this.script),
duration,
`Sandbox execution timeout in ${duration} seconds. Please increase --timeout`
);
try {
return await timeout(
this.run(this.script),
duration,
`Sandbox execution timeout in ${duration} seconds. Please increase --timeout`
);
} catch (e) {
const msgPart = 'Cannot find module ';
if (e instanceof VMError && e.message.includes(msgPart)) {
throw new Error(
`Unable to resolve module ${e.message.replace(msgPart, '')}. To resolve this you can either:\n\tNarrow your import. e.g Instead of "import { BigNumber } from 'ethers'" you can use "import {BigNumber} from '@ethersproject/bignumber';"\n\tEnable the --unsafe flag.`,
{cause: e}
);
}
throw e;
}
}

protected async convertStack(stackTrace: string | undefined): Promise<string | undefined> {
Expand Down

0 comments on commit 072f3ee

Please sign in to comment.