Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent 9091063 commit 2da82d7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
24 changes: 24 additions & 0 deletions src/bin/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import process from 'process'

import { addPadding } from '../report/utils/indent.js'

// Print CLI errors and exit, depending on the error type
// TODO: error normalization?
// TODO: enforce error types for bugs inside the CLI logic?
export const handleCliError = function (error) {
const { exitCode, printStack, indented } = ERROR_PROPS[error.name]
const errorMessage = printStack ? error.stack : error.message
const errorMessageA = indented ? addPadding(errorMessage) : errorMessage
console.error(errorMessageA)
process.exitCode = exitCode
}

// Error type-specific behavior
const ERROR_PROPS = {
CoreError: { exitCode: 5, printStack: true, indented: false },
PluginError: { exitCode: 4, printStack: true, indented: false },
UserCodeError: { exitCode: 3, printStack: true, indented: false },
UserError: { exitCode: 2, printStack: false, indented: false },
LimitError: { exitCode: 1, printStack: false, indented: false },
StopError: { exitCode: 0, printStack: false, indented: true },
}
15 changes: 1 addition & 14 deletions src/bin/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/usr/bin/env node
import { dirname } from 'path'
import process from 'process'
import { fileURLToPath } from 'url'

import { readPackageUp } from 'read-pkg-up'
import UpdateNotifier from 'update-notifier'

import { ERROR_PROPS } from '../error/main.js'
import { run, show, remove, dev } from '../main.js'
import { addPadding } from '../report/utils/indent.js'

import { handleCliError } from './error.js'
import { parseCliFlags } from './parse.js'
import { defineCli } from './top.js'

Expand Down Expand Up @@ -53,15 +51,4 @@ const checkUpdate = async function () {

const COMMANDS = { run, show, remove, dev }

// Print CLI errors and exit, depending on the error type
// TODO: error normalization?
// TODO: enforce error types for bugs inside the CLI logic?
const handleCliError = function (error) {
const { exitCode, printStack, indented } = ERROR_PROPS[error.name]
const errorMessage = printStack ? error.stack : error.message
const errorMessageA = indented ? addPadding(errorMessage) : errorMessage
console.error(errorMessageA)
process.exitCode = exitCode
}

runCli()
10 changes: 0 additions & 10 deletions src/error/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,3 @@ export {
// Top-level error handler
onError,
}

// Error type-specific behavior
export const ERROR_PROPS = {
CoreError: { exitCode: 5, printStack: true, indented: false },
PluginError: { exitCode: 4, printStack: true, indented: false },
UserCodeError: { exitCode: 3, printStack: true, indented: false },
UserError: { exitCode: 2, printStack: false, indented: false },
LimitError: { exitCode: 1, printStack: false, indented: false },
StopError: { exitCode: 0, printStack: false, indented: true },
}

0 comments on commit 2da82d7

Please sign in to comment.