Skip to content

Commit 2637a87

Browse files
committed
refactor: simplify VersionResolver and DependencyLoader and improve error handling
1 parent 0fcd4af commit 2637a87

4 files changed

Lines changed: 26 additions & 13 deletions

File tree

messages/shared.utils.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Couldn't find identity data while generating preview arguments
5050

5151
Couldn't find entity ID while generating preview arguments
5252

53+
# error.org.api-unsupported
54+
55+
Your org is on API Version %s. This version of the plugin supports only %s. Please update your plugin.
56+
5357
# error.no-project
5458

5559
This command is required to run from within a Salesforce project directory. %s

src/shared/orgUtils.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Connection } from '@salesforce/core';
18-
import { VersionChannel, resolveChannel, getDefaultChannel, getAllChannels } from './versionResolver.js';
17+
import { Connection, Messages, Logger } from '@salesforce/core';
18+
import {
19+
VersionChannel,
20+
resolveChannel,
21+
getDefaultChannel,
22+
getAllChannels,
23+
getSupportedVersionsList,
24+
} from './versionResolver.js';
25+
26+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
27+
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'shared.utils');
1928

2029
type LightningPreviewMetadataResponse = {
2130
enableLightningPreviewPref?: string;
@@ -33,6 +42,8 @@ export type AppDefinition = {
3342
* the local dev server matches with Org API versions, we rely on defining a metadata section in package.json
3443
*/
3544
export class OrgUtils {
45+
private static logger = Logger.childFromRoot('OrgUtils');
46+
3647
/**
3748
* Given an app name, it queries the AppDefinition table in the org to find
3849
* the DurableId for the app. To do so, it will first attempt at finding the
@@ -148,9 +159,10 @@ export class OrgUtils {
148159
if (validChannels.includes(envOverride as VersionChannel)) {
149160
return envOverride as VersionChannel;
150161
} else {
151-
throw new Error(
152-
`Invalid FORCE_VERSION_CHANNEL value: "${envOverride}". ` + `Valid values are: ${validChannels.join(', ')}`,
153-
);
162+
const message =
163+
`Invalid FORCE_VERSION_CHANNEL value: "${envOverride}". ` + `Valid values are: ${validChannels.join(', ')}`;
164+
this.logger.error(message);
165+
throw new Error(message);
154166
}
155167
}
156168

@@ -165,12 +177,9 @@ export class OrgUtils {
165177
try {
166178
return resolveChannel(orgVersion);
167179
} catch (error) {
168-
// Enhance error with helpful message
169-
throw new Error(
170-
`${error instanceof Error ? error.message : String(error)}\n` +
171-
`Your org is on API version ${orgVersion}. ` +
172-
'Please ensure you are using the correct version of the CLI and this plugin.',
173-
);
180+
const message = messages.getMessage('error.org.api-unsupported', [orgVersion, getSupportedVersionsList()]);
181+
this.logger.error(message);
182+
throw new Error(message);
174183
}
175184
}
176185
}

src/shared/versionResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function getMajorMinor(version: string): string {
3333
/**
3434
* Returns a formatted list of all supported API versions
3535
*/
36-
function getSupportedVersionsList(): string {
36+
export function getSupportedVersionsList(): string {
3737
const channels = packageJson.apiVersionMetadata.channels;
3838
const allVersions: string[] = [];
3939

test/shared/orgUtils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('orgUtils', () => {
7373
const conn = new Connection({ authInfo: new AuthInfo() });
7474
$$.SANDBOX.stub(conn, 'version').get(() => '64.0');
7575

76-
expect(() => OrgUtils.getVersionChannel(conn)).to.throw(/Unsupported org API version: 64.0/);
76+
expect(() => OrgUtils.getVersionChannel(conn)).to.throw(/Your org is on API Version 64.0/);
7777
});
7878
});
7979

0 commit comments

Comments
 (0)