Skip to content

Commit

Permalink
celocli: display URL to create a bug report when error occurs (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre authored Sep 11, 2024
1 parent 717809e commit f5e0101
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-teachers-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@celo/celocli': patch
---

Added a link to create an issue when an error occurs
71 changes: 71 additions & 0 deletions packages/cli/src/base.test.ts
Original file line number Diff line number Diff line change
@@ -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
",
],
]
`)
})
})
20 changes: 17 additions & 3 deletions packages/cli/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -231,10 +233,22 @@ export abstract class BaseCommand extends Command {
async finally(arg: Error | undefined): Promise<any> {
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}`)
}
Expand Down

0 comments on commit f5e0101

Please sign in to comment.