Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/nuxt-cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { CommandDef, SubCommandsDef } from 'citty'

import { consola } from 'consola'

import { asActionableError } from '../utils/errors'

type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>)
Expand Down Expand Up @@ -45,6 +47,8 @@ export function withRemedy(command: CommandDef): CommandDef {
return await run(context)
}
catch (error) {
// `@nuxt/vite-builder` can leave the console unwrapped when a build throws.
consola.wrapConsole()
throw asActionableError(error)
}
},
Expand Down
15 changes: 15 additions & 0 deletions packages/nuxt-cli/test/unit/command-remedy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CommandDef } from 'citty'

import { consola } from 'consola'
import { describe, expect, it } from 'vitest'

import { withRemedy } from '../../src/commands'
Expand Down Expand Up @@ -45,6 +46,20 @@ describe('command error boundary', () => {
await expect(run(command)).rejects.toBe(parse)
})

it('should report a failed command through consola after the console was unwrapped', async () => {
consola.restoreConsole()
const original = console.error
const command = withRemedy({ meta: { name: 'build' }, run: () => Promise.reject(new Error('build failed')) })

try {
await expect(run(command)).rejects.toThrow('build failed')
expect(console.error).not.toBe(original)
}
finally {
consola.restoreConsole()
}
})

it('should carry the boundary into subcommands', async () => {
const command = withRemedy({
meta: { name: 'module' },
Expand Down
Loading