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: report correct errors when using the pnpm server #7032

Merged
merged 1 commit into from
Sep 2, 2023
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
6 changes: 6 additions & 0 deletions .changeset/wet-lies-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/default-reporter": patch
pnpm: patch
---

Fix a bug causing errors to be printed as `Cannot read properties of undefined (reading 'code')` instead of the underlying reason when using the pnpm store server.
10 changes: 9 additions & 1 deletion cli/default-reporter/src/reportError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const colorPath = chalk.gray

export function reportError (logObj: Log, config?: Config) {
const errorInfo = getErrorInfo(logObj, config)
let output = formatErrorSummary(errorInfo.title, logObj['err']['code'])
let output = formatErrorSummary(errorInfo.title, (logObj as LogObjWithPossibleError).err?.code)
Copy link
Member Author

@gluxon gluxon Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would normally add a test when fixing a bug, but in this case the bug happened as a result of bypassing TypeScript compilation checks through the [] indexing syntax. The new LogObjWithPossibleError interface here should make this lookup a bit safer in the future.

if (logObj['pkgsStack'] != null) {
if (logObj['pkgsStack'].length > 0) {
output += `\n\n${formatPkgsStack(logObj['pkgsStack'])}`
Expand All @@ -33,6 +33,14 @@ export function reportError (logObj: Log, config?: Config) {
output += `\n\n${errorInfo.body}`
}
return output

/**
* A type to assist with introspection of the logObj.
* These objects may or may not have an `err` field.
*/
interface LogObjWithPossibleError {
readonly err?: { code?: string }
}
}

function getErrorInfo (logObj: Log, config?: Config): {
Expand Down