Skip to content

Commit f5039a5

Browse files
authored
Merge pull request #1398 from salesforcecli/sl/W-21567843
W-21567843 fix: improve JSON output for multiple errors
2 parents 7544443 + f5cb838 commit f5039a5

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

schemas/force-user-permset-assign.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@
3131
},
3232
"message": {
3333
"type": "string"
34+
},
35+
"details": {
36+
"type": "array",
37+
"items": {
38+
"type": "object",
39+
"properties": {
40+
"message": {
41+
"type": "string"
42+
},
43+
"errorCode": {
44+
"type": "string"
45+
},
46+
"fields": {
47+
"type": "array",
48+
"items": {
49+
"type": "string"
50+
}
51+
}
52+
},
53+
"required": ["message", "fields"],
54+
"additionalProperties": false
55+
}
3456
}
3557
},
3658
"required": ["name", "message"],

schemas/org-assign-permset.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@
3131
},
3232
"message": {
3333
"type": "string"
34+
},
35+
"details": {
36+
"type": "array",
37+
"items": {
38+
"type": "object",
39+
"properties": {
40+
"message": {
41+
"type": "string"
42+
},
43+
"errorCode": {
44+
"type": "string"
45+
},
46+
"fields": {
47+
"type": "array",
48+
"items": {
49+
"type": "string"
50+
}
51+
}
52+
},
53+
"required": ["message", "fields"],
54+
"additionalProperties": false
55+
}
3456
}
3557
},
3658
"required": ["name", "message"],

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)