Skip to content
Merged
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
36 changes: 34 additions & 2 deletions templates/cli/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,38 @@ const printQueryErrorHint = (err: Error): void => {
);
};

const ERROR_DETAIL_KEYS = ["code", "type", "response"] as const;

export const formatErrorForLog = (err: Error): string => {
const stack = err.stack || `${err.name}: ${err.message}`;
const detailLines = ERROR_DETAIL_KEYS.flatMap((key) => {
if (!Object.prototype.hasOwnProperty.call(err, key)) {
return [];
}

const value = (err as unknown as Record<string, unknown>)[key];
let detail = "undefined";
try {
detail =
typeof value === "string"
? JSON.stringify(value)
: JSON.stringify(value) ?? String(value);
} catch {
detail = String(value);
}
Comment on lines +676 to +684

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 The detail variable is initialised to "undefined" but this value is never actually used: the try block always assigns detail (the nullish-coalescing fallback ensures a non-undefined string even when JSON.stringify returns JS undefined), and the catch block overwrites it with String(value). The dead initialiser is misleading because it suggests there is a path that produces the string "undefined" outside of the String(undefined) call.

Suggested change
let detail = "undefined";
try {
detail =
typeof value === "string"
? JSON.stringify(value)
: JSON.stringify(value) ?? String(value);
} catch {
detail = String(value);
}
let detail: string;
try {
detail =
typeof value === "string"
? JSON.stringify(value)
: JSON.stringify(value) ?? String(value);
} catch {
detail = String(value);
}

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


return [`${key}: ${detail}`];
});

if (detailLines.length === 0) {
return stack;
}

const [summary, ...frames] = stack.split("\n");

return [summary, ...detailLines, ...frames].join("\n");
};

export const parseError = (err: Error): void => {
if (cliConfig.report) {
void (async () => {
Expand Down Expand Up @@ -712,12 +744,12 @@ export const parseError = (err: Error): void => {
printQueryErrorHint(err);

error("\n Stack Trace: \n");
console.error(err);
console.error(formatErrorForLog(err));
process.exit(1);
})();
} else {
if (cliConfig.verbose) {
console.error(err);
console.error(formatErrorForLog(err));
printQueryErrorHint(err);
} else {
log("For detailed error pass the --verbose or --report flag");
Expand Down
Loading