Skip to content

Commit

Permalink
refactor: merge custom error logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Dec 28, 2023
1 parent db60ecf commit c36c54a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
7 changes: 2 additions & 5 deletions packages/cli/bin/barnard59.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
27 changes: 16 additions & 11 deletions packages/cli/lib/cli.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -52,20 +53,24 @@ export default async function () {
try {
await program.parseAsync(process.argv)
} catch (error) {
const { groups } = /unknown command '(?<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 '(?<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
}
}
},
Expand Down

0 comments on commit c36c54a

Please sign in to comment.