Skip to content

Commit

Permalink
better error logging with --json flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Feb 7, 2024
1 parent 6bac51b commit 370412a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/cli-common/src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { mergeWith } from 'lodash-es'
import { commandLogger } from '../lib/log.js'
import { composeFlags, pluginFlags } from '../lib/common-flags/index.js'
import { PreevyConfig } from '../../../core/src/config.js'
import { errorToJson } from '../lib/errors.js'

// eslint-disable-next-line no-use-before-define
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<typeof BaseCommand['baseFlags'] & T['flags']>
Expand Down Expand Up @@ -139,17 +140,21 @@ abstract class BaseCommand<T extends typeof Command=typeof Command> extends Comm
this.stdErrLogger.info(message, ...args)
}

// eslint-disable-next-line class-methods-use-this
async catch(error: Error) {
const emitter = telemetryEmitter()
emitter.capture('error', {
error,
error: errorToJson(error),
})
emitter.unref()
await emitter.flush()
// eslint-disable-next-line @typescript-eslint/return-await
return await super.catch(error)
}

// eslint-disable-next-line class-methods-use-this
protected toErrorJson(err: unknown) {
return { error: errorToJson(err) }
}
}

export default BaseCommand
1 change: 1 addition & 0 deletions packages/cli-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './lib/plugins/model.js'
export * as text from './lib/text.js'
export { HookName, HookFunc, HooksListeners, Hooks } from './lib/hooks.js'
export { PluginContext, PluginInitContext } from './lib/plugins/context.js'
export { errorToJson } from './lib/errors.js'
export {
composeFlags, pluginFlags, envIdFlags, tunnelServerFlags, urlFlags, buildFlags, tableFlags, parseBuildFlags,
} from './lib/common-flags/index.js'
Expand Down
12 changes: 12 additions & 0 deletions packages/cli-common/src/lib/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const errorToJson = (e: unknown) => {
if (!(e instanceof Error)) {
return e
}
return {
...e,
name: e.name,
class: e.constructor.name,
message: e.message,
stack: e.stack,
}
}
6 changes: 3 additions & 3 deletions packages/cli/src/hooks/init/load-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Hook as OclifHook } from '@oclif/core'
import { initHook } from '@preevy/cli-common'
import { errorToJson, initHook } from '@preevy/cli-common'
import { telemetryEmitter } from '@preevy/core'

const wrappedHook: OclifHook<'init'> = async function wrappedHook(...args) {
try {
await initHook.call(this, ...args)
} catch (e) {
// eslint-disable-next-line no-console
console.warn(`warning: failed to init context: ${(e as Error).stack || e}`)
telemetryEmitter().capture('plugin-init-error', { error: e })
console.error(`init plugin failed: ${(e as Error).stack || e}`)
telemetryEmitter().capture('plugin-init-error', { error: errorToJson(e) })
await telemetryEmitter().flush()
process.exit(1)
}
Expand Down

0 comments on commit 370412a

Please sign in to comment.