Skip to content

Commit

Permalink
fix: wrap JwtGrantErrors (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel authored Aug 2, 2024
1 parent 2a60597 commit 79e4df6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/commands/org/login/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ export default class LoginJwt extends SfCommand<AuthFields> {
result = authInfo.getFields(true);
await AuthInfo.identifyPossibleScratchOrgs(result, authInfo);
} catch (err) {
if (!(err instanceof Error)) {
throw err;
}
throw messages.createError('JwtGrantError', [err.message]);
const msg = err instanceof Error ? `${err.name}::${err.message}` : typeof err === 'string' ? err : 'UNKNOWN';
throw SfError.create({
message: messages.getMessage('JwtGrantError', [msg]),
name: 'JwtGrantError',
...(err instanceof Error ? { cause: err } : {}),
});
}

const successMsg = commonMessages.getMessage('authorizeCommandSuccess', [result.username, result.orgId]);
Expand Down
15 changes: 15 additions & 0 deletions test/commands/org/login/login.jwt.nut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ describe('org:login:jwt NUTs', () => {
const output = getString(result, 'shellOutput.stdout');
expect(output).to.include(`Successfully authorized ${username} with org ID`);
});

it('should throw correct error for JwtAuthError', () => {
const command = `org:login:jwt -d -o ${username} -i incorrect -f ${jwtKey} -r ${instanceUrl} --json`;
const json = execCmd(command).jsonOutput;
expect(json).to.have.property('name', 'JwtGrantError');
expect(json).to.have.property('exitCode', 1);
expect(json)
.to.have.property('message')
.and.include(
'We encountered a JSON web token error, which is likely not an issue with Salesforce CLI. Here’s the error: JwtAuthError::Error authenticating with JWT.'
);
expect(json).to.have.property('stack').and.include('client identifier invalid');
expect(json).to.have.property('cause').and.include('SfError [JwtAuthError]: Error authenticating with JWT.');
expect(json).to.have.property('cause').and.include('at AuthInfo.authJwt');
});
});
6 changes: 5 additions & 1 deletion test/commands/org/login/login.jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ describe('org:login:jwt', () => {
await LoginJwt.run(['-u', testData.username, '-f', 'path/to/key.json', '-i', '123456INVALID', '--json']);
expect.fail('Should have thrown an error');
} catch (e) {
expect((e as Error).message).to.include('We encountered a JSON web token error');
expect(e).to.be.instanceOf(SfError);
const jwtAuthError = e as SfError;
expect(jwtAuthError.message).to.include('We encountered a JSON web token error');
expect(jwtAuthError.message).to.include('invalid client id');
expect(jwtAuthError.cause, 'JwtAuthErrors should include original error as the cause').to.be.ok;
}
});

Expand Down

0 comments on commit 79e4df6

Please sign in to comment.