Skip to content

Commit

Permalink
fix: print errors when --json option used (#5718)
Browse files Browse the repository at this point in the history
  • Loading branch information
await-ovo committed Dec 10, 2022
1 parent 73bb2a2 commit 2587011
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-masks-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pnpm": patch
---

The fatal error should be printed in JSON format, when running a pnpm command with the `--json` option [#5710](https://github.com/pnpm/pnpm/issues/5710).
25 changes: 16 additions & 9 deletions pnpm/src/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { logger } from '@pnpm/logger'
import { REPORTER_INITIALIZED } from './main'

export function errorHandler (error: Error) {
export function errorHandler (error: Error & { code?: string }) {
if (error.name != null && error.name !== 'pnpm' && !error.name.startsWith('pnpm:')) {
try {
error.name = 'pnpm'
} catch {
// Sometimes the name property is read-only
}
}

if (!global[REPORTER_INITIALIZED]) {
console.log(error)
// print parseable error on unhandled exception
console.log(JSON.stringify({
error: {
code: error.code ?? error.name,
message: error.message,
},
}, null, 2))
process.exitCode = 1
return
}
if (global[REPORTER_INITIALIZED] === 'silent') {
process.exitCode = 1
return
}
if (error.name != null && error.name !== 'pnpm' && !error.name.startsWith('pnpm:')) {
try {
error.name = 'pnpm'
} catch {
// Sometimes the name property is read-only
}
}

// bole passes only the name, message and stack of an error
// that is why we pass error as a message as well, to pass
Expand Down
2 changes: 1 addition & 1 deletion pnpm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export async function main (inputArgv: string[]) {
cmd,
config,
})
global[REPORTER_INITIALIZED] = reporterType
}
global[REPORTER_INITIALIZED] = reporterType

const selfUpdate = config.global && (cmd === 'add' || cmd === 'update') && cliParams.includes(packageManager.name)

Expand Down
37 changes: 37 additions & 0 deletions pnpm/test/errorHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { prepare, preparePackages } from '@pnpm/prepare'
import writeYamlFile from 'write-yaml-file'
import { execPnpmSync } from './utils'

test('should print json format error when publish --json failed', async () => {
prepare({
name: 'test-publish-package-no-version',
version: undefined,
})

const { status, stdout } = execPnpmSync(['publish', '--dry-run', '--json'])

expect(status).toBe(1)
const { error } = JSON.parse(stdout.toString())
expect(error?.code).toBe('ERR_PNPM_PACKAGE_VERSION_NOT_FOUND')
expect(error?.message).toBe('Package version is not defined in the package.json.')
})

test('should print json format error when add dependency on workspace root', async () => {
preparePackages([
{
name: 'project-a',
version: '1.0.0',
},
{
name: 'project-b',
version: '1.0.0',
},
])
await writeYamlFile('pnpm-workspace.yaml', { packages: ['**', '!store/**'] })

const { status, stdout } = execPnpmSync(['add', 'nanoid', '-p'])

expect(status).toBe(1)
const { error } = JSON.parse(stdout.toString())
expect(error?.code).toBe('ERR_PNPM_ADDING_TO_ROOT')
})

0 comments on commit 2587011

Please sign in to comment.