Skip to content

Commit 5d8b31d

Browse files
committed
fix: handle and throw a better error for getCurrentApiVersion
1 parent b665587 commit 5d8b31d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/registry/coverage.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import { OptionsOfTextResponseBody } from 'got';
1010
import got from 'got';
1111
import { ProxyAgent } from 'proxy-agent';
12+
import { isString } from '@salesforce/ts-types';
13+
import { SfError } from '@salesforce/core';
1214
import { CoverageObject } from '../../src/registry/types';
1315

1416
const getProxiedOptions = (url: string): OptionsOfTextResponseBody => ({
@@ -31,9 +33,16 @@ let apiVer: number;
3133

3234
export const getCurrentApiVersion = async (): Promise<number> => {
3335
if (apiVer === undefined) {
34-
const apiVersionsUrl = 'https://appexchange.salesforce.com/services/data';
35-
const lastVersionEntry = (await got(getProxiedOptions(apiVersionsUrl)).json<ApiVersion[]>()).pop() as ApiVersion;
36-
apiVer = +lastVersionEntry.version;
36+
try {
37+
const apiVersionsUrl = 'https://appexchange.salesforce.com/services/data';
38+
const lastVersionEntry = (await got(getProxiedOptions(apiVersionsUrl)).json<ApiVersion[]>()).at(-1) as ApiVersion;
39+
apiVer = +lastVersionEntry.version;
40+
} catch (e: unknown) {
41+
const err = e instanceof Error ? e : SfError.wrap(isString(e) ? e : 'unknown');
42+
const eMsg = 'Unable to get a current API version from the appexchange org';
43+
const eActions = ['Provide an API version explicitly', 'Set an API version in the project configuration'];
44+
throw new SfError(eMsg, 'ApiVersionRetrievalError', eActions, err);
45+
}
3746
}
3847
return apiVer;
3948
};

0 commit comments

Comments
 (0)