Skip to content

Commit

Permalink
Print bootstrap url when browser cannot be opened (#2241)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk authored Nov 18, 2024
1 parent 9dcd092 commit 8c8fc5e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/seven-chefs-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/sandbox': patch
---

Print bootstrap url when browser cannot be opened
47 changes: 47 additions & 0 deletions packages/sandbox/src/file_watching_sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,53 @@ void describe('Sandbox to check if region is bootstrapped', () => {
openMock.mock.calls[0].arguments[0],
getBootstrapUrl(region)
);
assert.strictEqual(printer.log.mock.callCount(), 1);
assert.strictEqual(
printer.log.mock.calls[0].arguments[0],
'The given region has not been bootstrapped. Sign in to console as a Root user or Admin to complete the bootstrap process, then restart the sandbox.'
);
assert.strictEqual(printer.log.mock.calls[0].arguments[1], undefined);
});

void it('when region has not bootstrapped, and opening console url fails prints url to initiate bootstrap', async () => {
ssmClientSendMock.mock.mockImplementationOnce(() => {
throw new ParameterNotFound({
$metadata: {},
message: 'Parameter not found',
});
});

openMock.mock.mockImplementationOnce(() =>
Promise.reject(new Error('open error'))
);

await sandboxInstance.start({
dir: 'testDir',
exclude: ['exclude1', 'exclude2'],
});

assert.strictEqual(ssmClientSendMock.mock.callCount(), 1);
assert.strictEqual(openMock.mock.callCount(), 1);
assert.strictEqual(
openMock.mock.calls[0].arguments[0],
getBootstrapUrl(region)
);
assert.strictEqual(printer.log.mock.callCount(), 3);
assert.strictEqual(
printer.log.mock.calls[0].arguments[0],
'The given region has not been bootstrapped. Sign in to console as a Root user or Admin to complete the bootstrap process, then restart the sandbox.'
);
assert.strictEqual(printer.log.mock.calls[0].arguments[1], undefined);
assert.strictEqual(
printer.log.mock.calls[1].arguments[0],
'Unable to open bootstrap url, open error'
);
assert.strictEqual(printer.log.mock.calls[1].arguments[1], LogLevel.DEBUG);
assert.strictEqual(
printer.log.mock.calls[2].arguments[0],
`Open ${getBootstrapUrl(region)} in the browser.`
);
assert.strictEqual(printer.log.mock.calls[2].arguments[1], undefined);
});

void it('when user does not have proper credentials throw user error', async () => {
Expand Down
19 changes: 18 additions & 1 deletion packages/sandbox/src/file_watching_sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
BackendIdentifierConversions,
} from '@aws-amplify/platform-core';
import { LambdaFunctionLogStreamer } from './lambda_function_log_streamer.js';

/**
* CDK stores bootstrap version in parameter store. Example parameter name looks like /cdk-bootstrap/<qualifier>/version.
* The default value for qualifier is hnb659fds, i.e. default parameter path is /cdk-bootstrap/hnb659fds/version.
Expand Down Expand Up @@ -125,7 +126,23 @@ export class FileWatchingSandbox extends EventEmitter implements Sandbox {
);
// get region from an available sdk client;
const region = await this.ssmClient.config.region();
await this.open(getBootstrapUrl(region));
const bootstrapUrl = getBootstrapUrl(region);
try {
await this.open(bootstrapUrl);
} catch (e) {
// If opening the link fails for any reason we fall back to
// printing the url in the console.
// This might happen:
// - in headless environments
// - if user does not have any app to open URL
// - if browser crashes
let logEntry = 'Unable to open bootstrap url';
if (e instanceof Error) {
logEntry = `${logEntry}, ${e.message}`;
}
this.printer.log(logEntry, LogLevel.DEBUG);
this.printer.log(`Open ${bootstrapUrl} in the browser.`);
}
return;
}

Expand Down

0 comments on commit 8c8fc5e

Please sign in to comment.