Skip to content

W-18218754: add logic to handle multiple errors returned from api call #677

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

Merged
merged 4 commits into from
May 14, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
"dependencies": {
"@inquirer/confirm": "^3.1.22",
"@inquirer/password": "^2.2.0",
"@oclif/core": "^4.2.10",
"@oclif/core": "^4.3.0",
"@oclif/table": "^0.4.6",
"@salesforce/core": "^8.8.5",
"@salesforce/core": "^8.10.0",
"@salesforce/kit": "^3.2.3",
"@salesforce/ts-types": "^2.0.12",
"ansis": "^3.3.2",
Expand Down
57 changes: 56 additions & 1 deletion src/errorFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import { inspect } from 'node:util';
import type { Ansis } from 'ansis';
import { Mode, Messages, envVars } from '@salesforce/core';
import { Mode, Messages, envVars, SfError } from '@salesforce/core';
import { AnyJson, ensureString, isAnyJson } from '@salesforce/ts-types';
import { StandardColors } from './ux/standardColors.js';
import { SfCommandError } from './SfCommandError.js';
import { Ux } from './ux/ux.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');
Expand Down Expand Up @@ -44,6 +46,7 @@ export const formatActions = (
export const formatError = (error: SfCommandError): string =>
[
`${formatErrorPrefix(error)} ${error.message}`,
formatMultipleErrorMessages(error),
...formatActions(error.actions ?? []),
error.stack && envVars.getString('SF_ENV') === Mode.DEVELOPMENT
? StandardColors.info(`\n*** Internal Diagnostic ***\n\n${inspect(error)}\n******\n`)
Expand All @@ -55,3 +58,55 @@ const formatErrorPrefix = (error: SfCommandError): string =>

const formatErrorCode = (error: SfCommandError): string =>
typeof error.code === 'string' || typeof error.code === 'number' ? ` (${error.code})` : '';

type JsforceApiError = {
errorCode: string;
message?: AnyJson;
};

const isJsforceApiError = (item: AnyJson): item is JsforceApiError =>
typeof item === 'object' && item !== null && !Array.isArray(item) && ('errorCode' in item || 'message' in item);

const formatMultipleErrorMessages = (error: SfCommandError): string => {
if (error.code === 'MULTIPLE_API_ERRORS' && error.cause) {
const errorData = getErrorData(error.cause);
if (errorData && Array.isArray(errorData) && errorData.length > 0) {
const errors = errorData.filter(isJsforceApiError).map((d) => ({
errorCode: d.errorCode,
message: ensureString(d.message ?? ''),
}));

const ux = new Ux();
return ux.makeTable({
data: errors,
columns: [
{ key: 'errorCode', name: 'Error Code' },
{ key: 'message', name: 'Message' },
],
});
}
}
return '';
};

/**
* Utility function to extract error data from an error object.
* Recursively traverses the error chain to find the first error that contains data.
* Returns undefined if no error in the chain contains data or if the input is not an Error/SfError.
*
* This is used in the top-level catch in sfCommand for deeply-nested error data.
*
* @param error - The error object to extract data from
* @returns The error data if found, undefined otherwise
*/
const getErrorData = (error: unknown): AnyJson | undefined => {
if (!(error instanceof Error || error instanceof SfError)) return undefined;

if ('data' in error && error.data && isAnyJson(error.data)) {
return error.data;
} else if (error.cause) {
return getErrorData(error.cause);
} else {
return undefined;
}
};
24 changes: 24 additions & 0 deletions test/unit/errorFormatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,28 @@ describe('errorFormatting.formatError()', () => {
expect(errorOutput).to.contain('warnings: undefined');
expect(errorOutput).to.contain('result: undefined');
});

it('should have correct output for multiple errors in table format when errorCode is MULTIPLE_API_ERRORS', () => {
const innerError = SfError.create({
message: 'foo',
data: [
{ errorCode: 'ERROR_1', message: 'error 1' },
{ errorCode: 'ERROR_2', message: 'error 2' },
],
});
const sfError = SfError.create({
name: 'myError',
message: 'foo',
actions: ['bar'],
context: 'myContext',
exitCode: 8,
cause: innerError,
});
const err = SfCommandError.from(sfError, 'thecommand');
err.code = 'MULTIPLE_API_ERRORS';
const errorOutput = formatError(err);
expect(errorOutput).to.match(/Error Code.+Message/);
expect(errorOutput).to.match(/ERROR_1.+error 1/);
expect(errorOutput).to.match(/ERROR_2.+error 2/);
});
});
57 changes: 14 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,10 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@jsforce/jsforce-node@^3.6.5":
version "3.6.6"
resolved "https://registry.yarnpkg.com/@jsforce/jsforce-node/-/jsforce-node-3.6.6.tgz#26fe2fc9f4f3671ca8bfb0c98a728cb8a0219265"
integrity sha512-WdIo2lLbrz6nkfiaz2UynyaNiM8o+fEjaRev7zA4KKSaQYB1MJ66xHubeI5Iheq8WgkY9XGwWKAwPDhuV+GROQ==
"@jsforce/jsforce-node@^3.6.5", "@jsforce/jsforce-node@^3.8.1":
version "3.8.1"
resolved "https://registry.yarnpkg.com/@jsforce/jsforce-node/-/jsforce-node-3.8.1.tgz#482fcf2820b48a6b10930d33550eb4e4cbd1e480"
integrity sha512-+IZZC7VfNjhkTyeAspBc4Z35Y5eAP0RIWQnyfKahsbY/aLjiFRIM9ejl1YbWbrbabf5ODFSUgBGmOiEYLW3f7Q==
dependencies:
"@sindresorhus/is" "^4"
base64url "^3.0.1"
Expand Down Expand Up @@ -556,10 +556,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@oclif/core@^4.2.10":
version "4.2.10"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-4.2.10.tgz#31dfb7481c79887c3e672e10c981fcc01fcbaeb3"
integrity sha512-fAqcXgqkUm4v5FYy7qWP4w1HaOlVSVJveah+yVTo5Nm5kTiXhmD5mQQ7+knGeBaStyrtQy6WardoC2xSic9rlQ==
"@oclif/core@^4.3.0":
version "4.3.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-4.3.0.tgz#9a2951f05f81a4c7ae5ffcc00b2d720cca0898e6"
integrity sha512-lIzHY+JMP6evrS5E/sGijNnwrCoNtGy8703jWXcMuPOYKiFhWoAqnIm1BGgoRgmxczkbSfRsHUL/lwsSgh74Lw==
dependencies:
ansi-escapes "^4.3.2"
ansis "^3.17.0"
Expand Down Expand Up @@ -608,36 +608,12 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

"@salesforce/core@^8.8.3":
version "8.8.4"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-8.8.4.tgz#1995d387a62c48810b4e0a66c68192ccdcf8001f"
integrity sha512-TKioMWh/QWmXjnD0bMNvFHEqaH175TCYWNXUCO1CixdhhI0p1MQj4AnQsk8wuRJiH5kVhiYw6Z7NukcIxLtbUw==
"@salesforce/core@^8.10.0", "@salesforce/core@^8.8.3":
version "8.10.3"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-8.10.3.tgz#3cc2c99d097757cb4b08dab921254cfa3a00c7c1"
integrity sha512-juqbU304TBrrjb8sZGw+QkeAJISKu4+v2XIMTCxGJoEjs4LLhsyI7/drxCUY+7FNye+veAGeJdn/PCxkKhSgcA==
dependencies:
"@jsforce/jsforce-node" "^3.6.5"
"@salesforce/kit" "^3.2.2"
"@salesforce/schemas" "^1.9.0"
"@salesforce/ts-types" "^2.0.10"
ajv "^8.17.1"
change-case "^4.1.2"
fast-levenshtein "^3.0.0"
faye "^1.4.0"
form-data "^4.0.0"
js2xmlparser "^4.0.1"
jsonwebtoken "9.0.2"
jszip "3.10.1"
pino "^9.4.0"
pino-abstract-transport "^1.2.0"
pino-pretty "^11.2.2"
proper-lockfile "^4.1.2"
semver "^7.6.3"
ts-retry-promise "^0.8.1"

"@salesforce/core@^8.8.5":
version "8.8.5"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-8.8.5.tgz#4004e22523e4e45b42631733d06dc8aec9fdc23d"
integrity sha512-eCiiO4NptvKkz04A4ivBVLzEBy/6IIFmaXoZ4tnF1FcD5MESvC+Xuc+0RFSRiYmPi5oloKNl6njrfVCKAho2zQ==
dependencies:
"@jsforce/jsforce-node" "^3.6.5"
"@jsforce/jsforce-node" "^3.8.1"
"@salesforce/kit" "^3.2.2"
"@salesforce/schemas" "^1.9.0"
"@salesforce/ts-types" "^2.0.10"
Expand Down Expand Up @@ -1143,12 +1119,7 @@ ansi-styles@^6.0.0, ansi-styles@^6.1.0, ansi-styles@^6.2.1:
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.1.tgz#0e62320cf99c21afff3b3012192546aacbfb05c5"
integrity sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==

ansis@^3.10.0, ansis@^3.3.2:
version "3.10.0"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.10.0.tgz#6886afb0f729b1fa865df6b710b97a9915b7d0d4"
integrity sha512-hxDKLYT7hy3Y4sF3HxI926A3urzPxi73mZBB629m9bCVF+NyKNxbwCqqm+C/YrGPtxLwnl6d8/ZASCsz6SyvJA==

ansis@^3.17.0:
ansis@^3.10.0, ansis@^3.17.0, ansis@^3.3.2:
version "3.17.0"
resolved "https://registry.yarnpkg.com/ansis/-/ansis-3.17.0.tgz#fa8d9c2a93fe7d1177e0c17f9eeb562a58a832d7"
integrity sha512-0qWUglt9JEqLFr3w1I1pbrChn1grhaiAR2ocX1PP/flRmxgtwTzPFFFnfIlD6aMOLQZgSuCRlidD70lvx8yhzg==
Expand Down