Skip to content

Commit b7d7bb6

Browse files
committed
fix: improve json output with multiple errors
1 parent ee6e3de commit b7d7bb6

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

src/baseCommands/user/permset/assign.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ type SuccessMsg = {
2727
type FailureMsg = {
2828
name: string;
2929
message: string;
30+
details?: ErrorDetail[];
31+
};
32+
33+
type ErrorDetail = {
34+
message: string;
35+
errorCode?: string;
36+
fields: string[];
3037
};
3138

3239
export type PermsetAssignResult = {
@@ -67,9 +74,50 @@ export abstract class UserPermSetAssignBaseCommand extends SfCommand<PermsetAssi
6774
});
6875
} catch (e) {
6976
const err = e as SfError;
77+
let errorMessage = err.message;
78+
let errorDetails: FailureMsg['details'];
79+
80+
// Check if there are multiple errors in the data property.
81+
if (Array.isArray(err.data)) {
82+
// Extract structured error details from each error object
83+
errorDetails = err.data
84+
.map((d: unknown): ErrorDetail | undefined => {
85+
if (typeof d !== 'object' || d === null) {
86+
return undefined;
87+
}
88+
const details = d as {
89+
message?: unknown;
90+
errorCode?: unknown;
91+
fields?: unknown;
92+
};
93+
const msgDetails = details.message;
94+
if (typeof msgDetails !== 'string') {
95+
return undefined;
96+
}
97+
const errorCodeDetails = details.errorCode;
98+
const fieldsDetails = details.fields;
99+
const fields = Array.isArray(fieldsDetails)
100+
? fieldsDetails.filter((field): field is string => typeof field === 'string')
101+
: [];
102+
return {
103+
message: msgDetails,
104+
errorCode: typeof errorCodeDetails === 'string' ? errorCodeDetails : undefined,
105+
fields,
106+
};
107+
})
108+
.filter((detail): detail is ErrorDetail => Boolean(detail));
109+
110+
if (errorDetails.length > 1) {
111+
// Keep table output concise and expose structured details in JSON output.
112+
errorMessage = 'Multiple errors occurred. See JSON output for details.';
113+
} else if (errorDetails.length === 1) {
114+
errorMessage = errorDetails[0].message;
115+
}
116+
}
70117
this.failures.push({
71118
name: aliasOrUsername,
72-
message: err.message,
119+
message: errorMessage,
120+
...(errorDetails?.length ? { details: errorDetails } : {}),
73121
});
74122
}
75123
}

0 commit comments

Comments
 (0)