diff --git a/.changeset/twelve-teachers-dream.md b/.changeset/twelve-teachers-dream.md new file mode 100644 index 000000000..45d990041 --- /dev/null +++ b/.changeset/twelve-teachers-dream.md @@ -0,0 +1,5 @@ +--- +'@celo/celocli': patch +--- + +Added a link to create an issue when an error occurs diff --git a/packages/cli/src/base.test.ts b/packages/cli/src/base.test.ts new file mode 100644 index 000000000..a3deb7bb0 --- /dev/null +++ b/packages/cli/src/base.test.ts @@ -0,0 +1,71 @@ +import { Connection } from '@celo/connect' +import { testWithAnvilL1 } from '@celo/dev-utils/lib/anvil-test' +import { ux } from '@oclif/core' +import Web3 from 'web3' +import { BaseCommand } from './base' +import { testLocallyWithWeb3Node } from './test-utils/cliUtils' + +process.env.NO_SYNCCHECK = 'true' + +testWithAnvilL1('BaseCommand', (web3: Web3) => { + it('logs command execution error', async () => { + class TestErrorCommand extends BaseCommand { + async run() { + throw new Error('test error') + } + } + + const errorSpy = jest.spyOn(console, 'error') + + await expect(testLocallyWithWeb3Node(TestErrorCommand, [], web3)).rejects.toThrow() + + expect(errorSpy.mock.calls).toMatchInlineSnapshot(` + [ + [ + " + Received an error during command execution, if you believe this is a bug you can create an issue here: + + https://github.com/celo-org/developer-tooling/issues/new?assignees=&labels=bug+report&projects=&template=BUG-FORM.yml + + ", + [Error: test error], + ], + ] + `) + }) + + it('logs connection close error', async () => { + class TestConnectionStopErrorCommand extends BaseCommand { + async run() { + console.log('Successful run') + } + } + + const writeMock = jest.spyOn(ux.write, 'stdout') + const logSpy = jest.spyOn(console, 'log') + const errorSpy = jest.spyOn(console, 'error') + + jest.spyOn(Connection.prototype, 'stop').mockImplementation(() => { + throw new Error('Mock connection stop error') + }) + + await testLocallyWithWeb3Node(TestConnectionStopErrorCommand, [], web3) + + expect(logSpy.mock.calls).toMatchInlineSnapshot(` + [ + [ + "Successful run", + ], + ] + `) + expect(errorSpy.mock.calls).toMatchInlineSnapshot(`[]`) + expect(writeMock.mock.calls).toMatchInlineSnapshot(` + [ + [ + "Failed to close the connection: Error: Mock connection stop error + ", + ], + ] + `) + }) +}) diff --git a/packages/cli/src/base.ts b/packages/cli/src/base.ts index 4a91d1c16..e147ce770 100644 --- a/packages/cli/src/base.ts +++ b/packages/cli/src/base.ts @@ -6,6 +6,7 @@ import { AddressValidation, newLedgerWalletWithSetup } from '@celo/wallet-ledger import { LocalWallet } from '@celo/wallet-local' import _TransportNodeHid from '@ledgerhq/hw-transport-node-hid' import { Command, Flags } from '@oclif/core' +import { CLIError } from '@oclif/core/lib/errors' import chalk from 'chalk' import net from 'net' import Web3 from 'web3' @@ -134,6 +135,7 @@ export abstract class BaseCommand extends Command { if (res.flags && res.flags.privateKey && !res.flags.useLedger && !res.flags.useAKV) { this._kit.connection.addAccount(res.flags.privateKey) } + return this._kit } @@ -231,10 +233,22 @@ export abstract class BaseCommand extends Command { async finally(arg: Error | undefined): Promise { try { if (arg) { - console.error('received error while cleaning up', arg) + if (!(arg instanceof CLIError)) { + console.error( + ` +Received an error during command execution, if you believe this is a bug you can create an issue here: + +https://github.com/celo-org/developer-tooling/issues/new?assignees=&labels=bug+report&projects=&template=BUG-FORM.yml + +`, + arg + ) + } + } + + if (this._kit !== null) { + this._kit.connection.stop() } - const kit = await this.getKit() - kit.connection.stop() } catch (error) { this.log(`Failed to close the connection: ${error}`) }