Skip to content

Commit dbfd502

Browse files
fix: make configuration validation not throw (#2034)
Co-authored-by: Frederik Prijck <frederik.prijck@okta.com>
1 parent 82f8246 commit dbfd502

File tree

4 files changed

+18
-132
lines changed

4 files changed

+18
-132
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +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 a detailed error message explaining which options are missing and how to provide them.
162+
If any of these required options are missing, the SDK will issue a warning with a detailed message explaining which options are missing and how to provide them.
163163

164164
## Routes
165165

src/errors/index.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -144,58 +144,3 @@ export class AccessTokenForConnectionError extends SdkError {
144144
this.cause = cause;
145145
}
146146
}
147-
148-
/**
149-
* Enum representing error codes related to configuration.
150-
*/
151-
export enum ConfigurationErrorCode {
152-
/**
153-
* Missing required configuration options.
154-
*/
155-
MISSING_REQUIRED_OPTIONS = "missing_required_options"
156-
}
157-
158-
/**
159-
* Error class representing a configuration error.
160-
* Extends the `SdkError` class.
161-
*/
162-
export class ConfigurationError extends SdkError {
163-
/**
164-
* The error code associated with the configuration error.
165-
*/
166-
public code: string;
167-
public missingOptions?: string[];
168-
169-
/**
170-
* Constructs a new `ConfigurationError` instance.
171-
*
172-
* @param code - The error code.
173-
* @param missingOptions - Array of missing configuration option names.
174-
* @param envVarMapping - Optional mapping of option names to their environment variable names.
175-
*/
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: clientAuthentication: Set either AUTH0_CLIENT_SECRET env var or AUTH0_CLIENT_ASSERTION_SIGNING_KEY env var, or pass clientSecret or clientAssertionSigningKey in options\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());
197-
this.name = "ConfigurationError";
198-
this.code = code;
199-
this.missingOptions = missingOptions;
200-
}
201-
}

src/server/client.test.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22

3-
import { ConfigurationError, ConfigurationErrorCode } from "../errors/index.js";
43
import { Auth0Client } from "./client.js";
54

65
describe("Auth0Client", () => {
@@ -37,73 +36,6 @@ describe("Auth0Client", () => {
3736
});
3837

3938
describe("constructor validation", () => {
40-
it("should throw ConfigurationError when all required options are missing", () => {
41-
expect(() => new Auth0Client()).toThrow(ConfigurationError);
42-
43-
try {
44-
new Auth0Client();
45-
} catch (error) {
46-
const configError = error as ConfigurationError;
47-
expect(configError).toBeInstanceOf(ConfigurationError);
48-
expect(configError.code).toBe(
49-
ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS
50-
);
51-
expect(configError.missingOptions).toContain("domain");
52-
expect(configError.missingOptions).toContain("clientId");
53-
expect(configError.missingOptions).toContain("clientAuthentication");
54-
expect(configError.missingOptions).toContain("appBaseUrl");
55-
expect(configError.missingOptions).toContain("secret");
56-
57-
// Check that error message contains specific text
58-
expect(configError.message).toContain(
59-
"Not all required options where provided"
60-
);
61-
expect(configError.message).toContain(ENV_VARS.DOMAIN);
62-
expect(configError.message).toContain(ENV_VARS.CLIENT_ID);
63-
expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET);
64-
expect(configError.message).toContain(
65-
ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY
66-
);
67-
expect(configError.message).toContain(ENV_VARS.APP_BASE_URL);
68-
expect(configError.message).toContain(ENV_VARS.SECRET);
69-
}
70-
});
71-
72-
it("should throw ConfigurationError when some required options are missing", () => {
73-
// Provide some but not all required options
74-
const options = {
75-
domain: "example.auth0.com",
76-
clientId: "client_123"
77-
};
78-
79-
try {
80-
new Auth0Client(options);
81-
} catch (error) {
82-
const configError = error as ConfigurationError;
83-
expect(configError).toBeInstanceOf(ConfigurationError);
84-
expect(configError.code).toBe(
85-
ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS
86-
);
87-
// These should be missing
88-
expect(configError.missingOptions).toContain("clientAuthentication");
89-
expect(configError.missingOptions).toContain("appBaseUrl");
90-
expect(configError.missingOptions).toContain("secret");
91-
// These should not be in the missing list
92-
expect(configError.missingOptions).not.toContain("domain");
93-
expect(configError.missingOptions).not.toContain("clientId");
94-
95-
// Error message should only contain instructions for missing options
96-
expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET);
97-
expect(configError.message).toContain(
98-
ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY
99-
);
100-
expect(configError.message).toContain(ENV_VARS.APP_BASE_URL);
101-
expect(configError.message).toContain(ENV_VARS.SECRET);
102-
expect(configError.message).not.toContain(`Set ${ENV_VARS.DOMAIN}`);
103-
expect(configError.message).not.toContain(`Set ${ENV_VARS.CLIENT_ID}`);
104-
}
105-
});
106-
10739
it("should accept clientSecret as authentication method", () => {
10840
// Set required environment variables with clientSecret
10941
process.env[ENV_VARS.DOMAIN] = "env.auth0.com";

src/server/client.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import {
77
AccessTokenError,
88
AccessTokenErrorCode,
99
AccessTokenForConnectionError,
10-
AccessTokenForConnectionErrorCode,
11-
ConfigurationError,
12-
ConfigurationErrorCode
10+
AccessTokenForConnectionErrorCode
1311
} from "../errors";
1412
import {
1513
AccessTokenForConnectionOptions,
@@ -694,11 +692,22 @@ export class Auth0Client {
694692
secret: "AUTH0_SECRET"
695693
};
696694

697-
throw new ConfigurationError(
698-
ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS,
699-
missing,
700-
envVarNames
701-
);
695+
// Standard intro message explaining the issue
696+
let errorMessage =
697+
"WARNING: 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";
698+
699+
// Add specific details for each missing option
700+
missing.forEach((key) => {
701+
if (key === "clientAuthentication") {
702+
errorMessage += `Missing: clientAuthentication: Set either AUTH0_CLIENT_SECRET env var or AUTH0_CLIENT_ASSERTION_SIGNING_KEY env var, or pass clientSecret or clientAssertionSigningKey in options\n`;
703+
} else if (envVarNames[key]) {
704+
errorMessage += `Missing: ${key}: Set ${envVarNames[key]} env var or pass ${key} in options\n`;
705+
} else {
706+
errorMessage += `Missing: ${key}\n`;
707+
}
708+
});
709+
710+
console.error(errorMessage.trim());
702711
}
703712

704713
// Prepare the result object with all validated options

0 commit comments

Comments
 (0)