Skip to content

Commit 7227acc

Browse files
committed
feat: better error name/messaging/suggestion and DRY
1 parent 8617e5b commit 7227acc

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

messages/messages.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ Disable masking of user input; use with problematic terminals.
4848

4949
OAuth client secret of personal connected app?
5050

51-
# invalidInstanceUrl
51+
# lightningInstanceUrl
5252

53-
Invalid instance URL. Specify a Salesforce instance URL using the format <domainname>.salesforce.com
53+
Invalid instance URL. It should NOT be a lightning domain.
5454

5555
# accessTokenStdin
5656

src/common.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export class Common {
1414
public static async resolveLoginUrl(instanceUrl?: string): Promise<string> {
1515
const logger = await Logger.child('Common', { tag: 'resolveLoginUrl' });
1616
if (instanceUrl) {
17-
if (instanceUrl.match(/lightning\..*force\.com/)) {
18-
throw new SfError(messages.getMessage('invalidInstanceUrl'), 'URL_WARNING');
19-
}
17+
throwIfLightning(instanceUrl);
2018
return instanceUrl;
2119
}
2220
let loginUrl: string;
@@ -29,10 +27,17 @@ export class Common {
2927
logger.debug(`error occurred while trying to determine loginUrl: ${message}`);
3028
loginUrl = SfdcUrl.PRODUCTION;
3129
}
32-
if (loginUrl.match(/lightning\..*force\.com/)) {
33-
throw new SfError(messages.getMessage('invalidInstanceUrl'), 'URL_WARNING');
34-
}
30+
throwIfLightning(loginUrl);
31+
3532
logger.debug(`loginUrl: ${loginUrl}`);
3633
return loginUrl;
3734
}
3835
}
36+
37+
const throwIfLightning = (urlString?: string): void => {
38+
if (urlString?.match(/lightning\..*force\.com/)) {
39+
throw new SfError(messages.getMessage('lightningInstanceUrl'), 'LightningDomain', [
40+
messages.getMessage('flags.instance-url.description'),
41+
]);
42+
}
43+
};

test/common.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('common unit tests', () => {
7777
expect.fail('This test is failing because it is expecting an error that is never thrown');
7878
} catch (error) {
7979
assert(error instanceof Error);
80-
expect(error.name).to.equal('URL_WARNING');
80+
expect(error.name).to.equal('LightningDomain');
8181
}
8282
});
8383
it('should throw on lightning login URL passed in to resolveLoginUrl()', async () => {
@@ -87,7 +87,7 @@ describe('common unit tests', () => {
8787
expect.fail('This test is failing because it is expecting an error that is never thrown');
8888
} catch (error) {
8989
assert(error instanceof Error);
90-
expect(error.name).to.equal('URL_WARNING');
90+
expect(error.name).to.equal('LightningDomain');
9191
}
9292
});
9393

@@ -98,7 +98,7 @@ describe('common unit tests', () => {
9898
expect.fail('This test is failing because it is expecting an error that is never thrown');
9999
} catch (error) {
100100
assert(error instanceof Error);
101-
expect(error.name).to.equal('URL_WARNING');
101+
expect(error.name).to.equal('LightningDomain');
102102
}
103103
});
104104
});

0 commit comments

Comments
 (0)