From c36c54a493462166ee92e2d4b040a6a867c42959 Mon Sep 17 00:00:00 2001 From: tpluscode Date: Thu, 28 Dec 2023 10:19:32 +0100 Subject: [PATCH] refactor: merge custom error logic --- packages/cli/bin/barnard59.js | 7 ++----- packages/cli/lib/cli.js | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/cli/bin/barnard59.js b/packages/cli/bin/barnard59.js index 7d774714..383f8001 100755 --- a/packages/cli/bin/barnard59.js +++ b/packages/cli/bin/barnard59.js @@ -6,10 +6,8 @@ import { Resource, envDetector, processDetector } from '@opentelemetry/resources import { NodeSDK } from '@opentelemetry/sdk-node' import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' import { BatchSpanProcessor } from '@opentelemetry/tracing' - import { Command } from 'commander' import * as monitoringOptions from '../lib/cli/monitoringOptions.js' -import { MultipleRootsError } from '../findPipeline.js' const sdk = new NodeSDK({ // Automatic detection is disabled, see comment below @@ -30,10 +28,9 @@ const onError = async err => { process.off('SIGTERM', onError) if (err) { - if (err instanceof MultipleRootsError) { - const alternatives = err.alternatives.map(x => `\n\t--pipeline ${x}`).join('') + if (err.skipTrace) { // eslint-disable-next-line no-console - console.error(`Multiple root pipelines found. Try one of these:${alternatives}`) + console.log(err.message) } else { // eslint-disable-next-line no-console console.error(err) diff --git a/packages/cli/lib/cli.js b/packages/cli/lib/cli.js index ee2ef107..8ba3c691 100644 --- a/packages/cli/lib/cli.js +++ b/packages/cli/lib/cli.js @@ -1,5 +1,6 @@ import { program } from 'commander' import isInstalledGlobally from 'is-installed-globally' +import { MultipleRootsError } from '../findPipeline.js' import runAction from './cli/runAction.js' import * as monitoringOptions from './cli/monitoringOptions.js' import * as commonOptions from './cli/commonOptions.js' @@ -52,20 +53,24 @@ export default async function () { try { await program.parseAsync(process.argv) } catch (error) { - const { groups } = /unknown command '(?[^']+)'/.exec(error.message) || {} - if (groups && groups.command) { - /* eslint-disable no-console */ - if (isInstalledGlobally) { - console.error(`Try running 'npm install (-g) barnard59-${groups.command}'`) - } - - console.error(`Try running 'npm install barnard59-${groups.command}'`) - } + if (error instanceof program.CommanderError) { + const { groups } = /unknown command '(?[^']+)'/.exec(error.message) || {} + if (groups && groups.command) { + /* eslint-disable no-console */ + if (isInstalledGlobally) { + console.error(`Try running 'npm install (-g) barnard59-${groups.command}'`) + } - if ('exitCode' in error) { + console.error(`Try running 'npm install barnard59-${groups.command}'`) + } process.exit(error.exitCode) - } else { + } else if (error instanceof MultipleRootsError) { + const alternatives = error.alternatives.map(x => `\n\t--pipeline ${x}`).join('') + // eslint-disable-next-line no-console + console.error(`Multiple root pipelines found. Try one of these:${alternatives}`) process.exit(1) + } else { + throw error } } },