Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print errors when --json option used #5718

Merged
merged 4 commits into from
Dec 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})