Skip to content

Commit 8c8fc5e

Browse files
author
Kamil Sobol
authored
Print bootstrap url when browser cannot be opened (#2241)
1 parent 9dcd092 commit 8c8fc5e

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

.changeset/seven-chefs-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/sandbox': patch
3+
---
4+
5+
Print bootstrap url when browser cannot be opened

packages/sandbox/src/file_watching_sandbox.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,53 @@ void describe('Sandbox to check if region is bootstrapped', () => {
187187
openMock.mock.calls[0].arguments[0],
188188
getBootstrapUrl(region)
189189
);
190+
assert.strictEqual(printer.log.mock.callCount(), 1);
191+
assert.strictEqual(
192+
printer.log.mock.calls[0].arguments[0],
193+
'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.'
194+
);
195+
assert.strictEqual(printer.log.mock.calls[0].arguments[1], undefined);
196+
});
197+
198+
void it('when region has not bootstrapped, and opening console url fails prints url to initiate bootstrap', async () => {
199+
ssmClientSendMock.mock.mockImplementationOnce(() => {
200+
throw new ParameterNotFound({
201+
$metadata: {},
202+
message: 'Parameter not found',
203+
});
204+
});
205+
206+
openMock.mock.mockImplementationOnce(() =>
207+
Promise.reject(new Error('open error'))
208+
);
209+
210+
await sandboxInstance.start({
211+
dir: 'testDir',
212+
exclude: ['exclude1', 'exclude2'],
213+
});
214+
215+
assert.strictEqual(ssmClientSendMock.mock.callCount(), 1);
216+
assert.strictEqual(openMock.mock.callCount(), 1);
217+
assert.strictEqual(
218+
openMock.mock.calls[0].arguments[0],
219+
getBootstrapUrl(region)
220+
);
221+
assert.strictEqual(printer.log.mock.callCount(), 3);
222+
assert.strictEqual(
223+
printer.log.mock.calls[0].arguments[0],
224+
'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.'
225+
);
226+
assert.strictEqual(printer.log.mock.calls[0].arguments[1], undefined);
227+
assert.strictEqual(
228+
printer.log.mock.calls[1].arguments[0],
229+
'Unable to open bootstrap url, open error'
230+
);
231+
assert.strictEqual(printer.log.mock.calls[1].arguments[1], LogLevel.DEBUG);
232+
assert.strictEqual(
233+
printer.log.mock.calls[2].arguments[0],
234+
`Open ${getBootstrapUrl(region)} in the browser.`
235+
);
236+
assert.strictEqual(printer.log.mock.calls[2].arguments[1], undefined);
190237
});
191238

192239
void it('when user does not have proper credentials throw user error', async () => {

packages/sandbox/src/file_watching_sandbox.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
BackendIdentifierConversions,
3838
} from '@aws-amplify/platform-core';
3939
import { LambdaFunctionLogStreamer } from './lambda_function_log_streamer.js';
40+
4041
/**
4142
* CDK stores bootstrap version in parameter store. Example parameter name looks like /cdk-bootstrap/<qualifier>/version.
4243
* The default value for qualifier is hnb659fds, i.e. default parameter path is /cdk-bootstrap/hnb659fds/version.
@@ -125,7 +126,23 @@ export class FileWatchingSandbox extends EventEmitter implements Sandbox {
125126
);
126127
// get region from an available sdk client;
127128
const region = await this.ssmClient.config.region();
128-
await this.open(getBootstrapUrl(region));
129+
const bootstrapUrl = getBootstrapUrl(region);
130+
try {
131+
await this.open(bootstrapUrl);
132+
} catch (e) {
133+
// If opening the link fails for any reason we fall back to
134+
// printing the url in the console.
135+
// This might happen:
136+
// - in headless environments
137+
// - if user does not have any app to open URL
138+
// - if browser crashes
139+
let logEntry = 'Unable to open bootstrap url';
140+
if (e instanceof Error) {
141+
logEntry = `${logEntry}, ${e.message}`;
142+
}
143+
this.printer.log(logEntry, LogLevel.DEBUG);
144+
this.printer.log(`Open ${bootstrapUrl} in the browser.`);
145+
}
129146
return;
130147
}
131148

0 commit comments

Comments
 (0)