Skip to content

Commit 3c864e0

Browse files
udpate config validation logic
1 parent df53d40 commit 3c864e0

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,7 @@ The SDK performs validation of required configuration options when initializing
159159
- `clientSecret` (or `AUTH0_CLIENT_SECRET` environment variable), OR
160160
- `clientAssertionSigningKey` (or `AUTH0_CLIENT_ASSERTION_SIGNING_KEY` environment variable)
161161

162-
If any of these required options are missing, the SDK will throw a `ConfigurationError` with the code `missing_required_options` and details about which specific options are missing. The error includes:
163-
164-
- A list of missing options
165-
- Instructions on how to provide each missing option (via the correct environment variable or constructor parameter)
166-
167-
Example of handling configuration errors:
162+
If any of these required options are missing, the SDK will throw a `ConfigurationError` with the code `MISSING_REQUIRED_OPTIONS` and a detailed error message explaining which options are missing and how to provide them.
168163

169164
## Routes
170165

src/errors/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,30 @@ export class ConfigurationError extends SdkError {
170170
* Constructs a new `ConfigurationError` instance.
171171
*
172172
* @param code - The error code.
173-
* @param message - The error message.
174-
* @param missingOptions - Optional array of missing configuration option names.
173+
* @param missingOptions - Array of missing configuration option names.
174+
* @param envVarMapping - Optional mapping of option names to their environment variable names.
175175
*/
176-
constructor(code: string, message: string, missingOptions?: string[]) {
177-
super(message);
176+
constructor(
177+
code: string,
178+
missingOptions: string[] = [],
179+
envVarMapping: Record<string, string> = {}
180+
) {
181+
// Standard intro message explaining the issue
182+
let errorMessage =
183+
"Not all required options where provided when creating an instance of Auth0Client. Ensure to provide all missing options, either by passing it to the Auth0Client constructor, or by setting the corresponding environment variable.\n\n";
184+
185+
// Add specific details for each missing option
186+
missingOptions.forEach((key) => {
187+
if (key === "clientAuthentication") {
188+
errorMessage += `Missing: either 'clientSecret' (AUTH0_CLIENT_SECRET env var) or 'clientAssertionSigningKey' (AUTH0_CLIENT_ASSERTION_SIGNING_KEY env var)\n`;
189+
} else if (envVarMapping[key]) {
190+
errorMessage += `Missing: ${key}: Set ${envVarMapping[key]} env var or pass ${key} in options\n`;
191+
} else {
192+
errorMessage += `Missing: ${key}\n`;
193+
}
194+
});
195+
196+
super(errorMessage.trim());
178197
this.name = "ConfigurationError";
179198
this.code = code;
180199
this.missingOptions = missingOptions;

src/server/client.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ describe("Auth0Client", () => {
5454
expect(configError.missingOptions).toContain("appBaseUrl");
5555
expect(configError.missingOptions).toContain("secret");
5656

57-
// Check that error message contains correct environment variable names
57+
// Check that error message contains specific text
58+
expect(configError.message).toContain(
59+
"Not all required options where provided"
60+
);
5861
expect(configError.message).toContain(ENV_VARS.DOMAIN);
5962
expect(configError.message).toContain(ENV_VARS.CLIENT_ID);
6063
expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET);

src/server/client.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,6 @@ export class Auth0Client {
684684
}
685685

686686
if (missing.length) {
687-
// Create the error message for all missing options
688-
let errorMsg =
689-
`Missing mandatory configuration: ${missing.join(", ")}\n` +
690-
"Provide via constructor options or environment variables:\n";
691-
692687
// Map of option keys to their exact environment variable names
693688
const envVarNames: Record<string, string> = {
694689
domain: "AUTH0_DOMAIN",
@@ -697,19 +692,10 @@ export class Auth0Client {
697692
secret: "AUTH0_SECRET"
698693
};
699694

700-
// Generate specific error messages for each missing option using the exact env var names
701-
for (const key of missing) {
702-
if (key === "clientAuthentication") {
703-
errorMsg += `- Either provide 'clientSecret' (AUTH0_CLIENT_SECRET env var) or 'clientAssertionSigningKey' (AUTH0_CLIENT_ASSERTION_SIGNING_KEY env var)\n`;
704-
} else {
705-
errorMsg += `- ${key}: Set ${envVarNames[key]} env var or pass ${key} in options\n`;
706-
}
707-
}
708-
709695
throw new ConfigurationError(
710696
ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS,
711-
errorMsg.trim(),
712-
missing
697+
missing,
698+
envVarNames
713699
);
714700
}
715701

0 commit comments

Comments
 (0)