Skip to content

Commit ce47f52

Browse files
committed
throw exception directly during input validation
1 parent 1020a06 commit ce47f52

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/load.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,10 @@ export async function load(
2929
appConfigOptions?: AzureAppConfigurationOptions
3030
): Promise<AzureAppConfiguration> {
3131
const startTimestamp = Date.now();
32-
try {
33-
return await loadPromise(connectionStringOrEndpoint, credentialOrOptions, appConfigOptions);
34-
} catch (error) {
35-
// load() method is called in the application's startup code path.
36-
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
37-
// Knowing the intended usage of the provider in startup code path, we mitigate back-to-back crash loops from overloading the server with requests by waiting a minimum time to propagate fatal errors.
38-
const delay = MinDelayForUnhandedError - (Date.now() - startTimestamp);
39-
if (delay > 0) {
40-
await new Promise((resolve) => setTimeout(resolve, delay));
41-
}
42-
throw error;
43-
}
44-
}
45-
46-
async function loadPromise(
47-
connectionStringOrEndpoint: string | URL,
48-
credentialOrOptions?: TokenCredential | AzureAppConfigurationOptions,
49-
appConfigOptions?: AzureAppConfigurationOptions
50-
): Promise<AzureAppConfiguration> {
5132
let client: AppConfigurationClient;
5233
let options: AzureAppConfigurationOptions | undefined;
34+
35+
// input validation
5336
if (typeof connectionStringOrEndpoint === "string" && !instanceOfTokenCredential(credentialOrOptions)) {
5437
const connectionString = connectionStringOrEndpoint;
5538
options = credentialOrOptions as AzureAppConfigurationOptions;
@@ -77,9 +60,20 @@ async function loadPromise(
7760
throw new Error("A connection string or an endpoint with credential must be specified to create a client.");
7861
}
7962

80-
const appConfiguration = new AzureAppConfigurationImpl(client, options);
81-
await appConfiguration.load();
82-
return appConfiguration;
63+
try {
64+
const appConfiguration = new AzureAppConfigurationImpl(client, options);
65+
await appConfiguration.load();
66+
return appConfiguration;
67+
} catch (error) {
68+
// load() method is called in the application's startup code path.
69+
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
70+
// Knowing the intended usage of the provider in startup code path, we mitigate back-to-back crash loops from overloading the server with requests by waiting a minimum time to propagate fatal errors.
71+
const delay = MinDelayForUnhandedError - (Date.now() - startTimestamp);
72+
if (delay > 0) {
73+
await new Promise((resolve) => setTimeout(resolve, delay));
74+
}
75+
throw error;
76+
}
8377
}
8478

8579
function instanceOfTokenCredential(obj: unknown) {

0 commit comments

Comments
 (0)