Skip to content

Commit

Permalink
fix: separate out command error handler
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <[email protected]>
  • Loading branch information
leninmehedy committed Feb 28, 2024
1 parent f18b3f2 commit 6833573
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/commands/base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ import { FullstackTestingError, MissingArgumentError } from '../core/errors.mjs'
import { ConfigManager } from '../core/index.mjs'
import { ShellRunner } from '../core/shell_runner.mjs'

/**
* This is a default command error handler used by handleCommand
* @param err error
* @param logger logger
*/
const defaultErrorHandler = (err, logger) => {
logger.showUserError(err)

Check warning on line 28 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L28

Added line #L28 was not covered by tests

// do not exit immediately so that logger can flush properly
setTimeout(() => {
process.exit(1)

Check warning on line 32 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L31-L32

Added lines #L31 - L32 were not covered by tests
}, 1)
}

export class BaseCommand extends ShellRunner {
async prepareChartPath (chartDir, chartRepo, chartName) {
if (!chartRepo) throw new MissingArgumentError('chart repo name is required')
Expand Down Expand Up @@ -57,9 +71,10 @@ export class BaseCommand extends ShellRunner {
*
* @param argv argv of the command
* @param handleFunc async function to be invoked
* @return {Promise<boolean>} true if the execution succeeded
* @param errHandler error handler
* @return {Promise<void>}
*/
async handleCommand (argv, handleFunc) {
async handleCommand (argv, handleFunc, errHandler = defaultErrorHandler) {
if (!argv) throw new MissingArgumentError('argv is required')
if (!handleFunc) throw new MissingArgumentError('handleFunc is required')

Expand All @@ -76,16 +91,7 @@ export class BaseCommand extends ShellRunner {
}

if (error) {
this.logger.showUserError(error)

// do not exit immediately so that logger can flush properly
setTimeout(() => {
process.exit(1)
}, 1)

return false // we return false here, but process will exit with error code eventually.
return errHandler(error, this.logger)

Check warning on line 94 in src/commands/base.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/base.mjs#L94

Added line #L94 was not covered by tests
}

return true
}
}

0 comments on commit 6833573

Please sign in to comment.