generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 103
refactor top level cli error handling #1730
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b0fd5a
refactor top level cli error handling
Amplifiyer fecbb3a
Merge branch 'main' into error_1
Amplifiyer 79bec5a
PR updates
Amplifiyer 7cd6a59
Add cause
Amplifiyer 27f2447
Merge branch 'main' into error_1
Amplifiyer 8cc4cb9
update api after clean build
Amplifiyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@aws-amplify/platform-core': patch | ||
| '@aws-amplify/sandbox': patch | ||
| '@aws-amplify/backend-cli': patch | ||
| --- | ||
|
|
||
| refactor top level cli error handling |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/platform-core/src/usage-data/account_id_fetcher.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { AccountIdFetcher } from './account_id_fetcher'; | ||
| import { GetCallerIdentityCommandOutput, STSClient } from '@aws-sdk/client-sts'; | ||
| import { describe, mock, test } from 'node:test'; | ||
| import assert from 'node:assert'; | ||
|
|
||
| void describe('AccountIdFetcher', async () => { | ||
| void test('fetches account ID successfully', async () => { | ||
| const mockSend = mock.method(STSClient.prototype, 'send', () => | ||
| Promise.resolve({ | ||
| Account: '123456789012', | ||
| } as GetCallerIdentityCommandOutput) | ||
| ); | ||
|
|
||
| const accountIdFetcher = new AccountIdFetcher(new STSClient({})); | ||
| const accountId = await accountIdFetcher.fetch(); | ||
|
|
||
| assert.strictEqual(accountId, '123456789012'); | ||
| mockSend.mock.resetCalls(); | ||
| }); | ||
|
|
||
| void test('returns default account ID when STS fails', async () => { | ||
| const mockSend = mock.method(STSClient.prototype, 'send', () => | ||
| Promise.reject(new Error('STS error')) | ||
| ); | ||
|
|
||
| const accountIdFetcher = new AccountIdFetcher(new STSClient({})); | ||
| const accountId = await accountIdFetcher.fetch(); | ||
|
|
||
| assert.strictEqual(accountId, 'NO_ACCOUNT_ID'); | ||
| mockSend.mock.resetCalls(); | ||
| }); | ||
|
|
||
| void test('returns cached account ID on subsequent calls', async () => { | ||
| const mockSend = mock.method(STSClient.prototype, 'send', () => | ||
| Promise.resolve({ | ||
| Account: '123456789012', | ||
| } as GetCallerIdentityCommandOutput) | ||
| ); | ||
|
|
||
| const accountIdFetcher = new AccountIdFetcher(new STSClient({})); | ||
| const accountId1 = await accountIdFetcher.fetch(); | ||
| const accountId2 = await accountIdFetcher.fetch(); | ||
|
|
||
| assert.strictEqual(accountId1, '123456789012'); | ||
| assert.strictEqual(accountId2, '123456789012'); | ||
|
|
||
| // we only call the service once. | ||
| assert.strictEqual(mockSend.mock.callCount(), 1); | ||
| mockSend.mock.resetCalls(); | ||
| }); | ||
| }); |
26 changes: 18 additions & 8 deletions
26
packages/platform-core/src/usage-data/account_id_fetcher.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,32 @@ | ||
| import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts'; | ||
|
|
||
| const NO_ACCOUNT_ID = 'NO_ACCOUNT_ID'; | ||
| /** | ||
| * Retrieves the account ID of the user | ||
| */ | ||
| export class AccountIdFetcher { | ||
| private accountId: string | undefined; | ||
| /** | ||
| * constructor for AccountIdFetcher | ||
| */ | ||
| constructor(private readonly stsClient = new STSClient()) {} | ||
| fetch = async () => { | ||
| const stsResponse = await this.stsClient.send( | ||
| new GetCallerIdentityCommand({}) | ||
| ); | ||
| if (stsResponse && stsResponse.Account) { | ||
| return stsResponse.Account; | ||
| if (this.accountId) { | ||
| return this.accountId; | ||
| } | ||
| try { | ||
| const stsResponse = await this.stsClient.send( | ||
| new GetCallerIdentityCommand({}) | ||
| ); | ||
| if (stsResponse && stsResponse.Account) { | ||
| this.accountId = stsResponse.Account; | ||
| return this.accountId; | ||
| } | ||
| // We failed to get the account Id. Most likely the user doesn't have credentials | ||
| return NO_ACCOUNT_ID; | ||
edwardfoyle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| // We failed to get the account Id. Most likely the user doesn't have credentials | ||
Amplifiyer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return NO_ACCOUNT_ID; | ||
| } | ||
| throw new Error( | ||
| 'Cannot retrieve the account Id from GetCallerIdentityCommand' | ||
| ); | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.