Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update error messages #46

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/CroctProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ describe('<CroctProvider />', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = '';

expect(() => render(<CroctProvider />)).toThrow(
'Croct\'s application ID is missing. '
+ 'Did you forget to set the NEXT_PUBLIC_CROCT_APP_ID environment variable?',
'Croct\'s Application ID is missing. '
+ 'Did you forget to set the `NEXT_PUBLIC_CROCT_APP_ID` environment variable? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-environment-variable',
);
});

Expand Down
14 changes: 8 additions & 6 deletions src/config/appId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ describe('getAppId', () => {

it('should throw an error when the application ID is missing', () => {
expect(() => getAppId()).toThrow(
'Croct\'s application ID is missing. '
+ 'Did you forget to set the NEXT_PUBLIC_CROCT_APP_ID environment variable?',
'Croct\'s Application ID is missing. '
+ 'Did you forget to set the `NEXT_PUBLIC_CROCT_APP_ID` environment variable? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-environment-variable',
);
});

it('should throw an error when the application ID is empty', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = '';

expect(() => getAppId()).toThrow(
'Croct\'s application ID is missing. '
+ 'Did you forget to set the NEXT_PUBLIC_CROCT_APP_ID environment variable?',
'Croct\'s Application ID is missing. '
+ 'Did you forget to set the `NEXT_PUBLIC_CROCT_APP_ID` environment variable? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-environment-variable',
);
});

it('should throw an error when the application ID is invalid', () => {
process.env.NEXT_PUBLIC_CROCT_APP_ID = 'invalid';

expect(() => getAppId()).toThrow(
'Croct\'s application ID is invalid. '
+ 'Please check the NEXT_PUBLIC_CROCT_APP_ID environment variable.',
'Croct\'s Application ID is invalid. '
+ 'Please check the `NEXT_PUBLIC_CROCT_APP_ID` environment variable.',
);
});

Expand Down
9 changes: 5 additions & 4 deletions src/config/appId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ export function getAppId(): string {

if (appId === '') {
throw new Error(
'Croct\'s application ID is missing. '
+ 'Did you forget to set the NEXT_PUBLIC_CROCT_APP_ID environment variable?',
'Croct\'s Application ID is missing. '
+ 'Did you forget to set the `NEXT_PUBLIC_CROCT_APP_ID` environment variable? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-environment-variable',
);
}

if (!PATTERN.test(appId)) {
throw new Error(
'Croct\'s application ID is invalid. '
+ 'Please check the NEXT_PUBLIC_CROCT_APP_ID environment variable.',
'Croct\'s Application ID is invalid. '
+ 'Please check the `NEXT_PUBLIC_CROCT_APP_ID` environment variable.',
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/config/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ describe('getRequestContext', () => {
const appId = '00000000-0000-0000-0000-000000000000';

it('should throw an error when the client ID is missing', () => {
expect(() => getRequestContext(new Headers(), createCookieJar()))
.toThrow('Croct\'s client ID is missing. Did you forget to configure Croct\'s middleware?');
expect(() => getRequestContext(new Headers(), createCookieJar())).toThrow(
'Croct\'s Client ID is missing. Did you forget to configure Croct\'s middleware? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-middleware',
);
});

it('should return a full request context', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/config/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export function getRequestContext(headers: HeaderReader, cookies: CookieReader):
const clientId = headers.get(Header.CLIENT_ID);

if (clientId === null) {
throw new Error('Croct\'s client ID is missing. Did you forget to configure Croct\'s middleware?');
throw new Error(
'Croct\'s Client ID is missing. Did you forget to configure Croct\'s middleware? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-middleware',
);
}

const context: RequestContext = {
Expand Down
8 changes: 4 additions & 4 deletions src/config/cookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('cookie', () => {

expect(() => getClientIdCookieOptions()).toThrow(
'Croct\'s cookie duration must be a positive integer, got \'invalid\'. '
+ 'Please check the NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION environment variable.',
+ 'Please check the `NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION` environment variable.',
);
});

Expand All @@ -78,7 +78,7 @@ describe('cookie', () => {

expect(() => getClientIdCookieOptions()).toThrow(
'Croct\'s cookie duration must be a positive integer, got \'-1\'. '
+ 'Please check the NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION environment variable.',
+ 'Please check the `NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION` environment variable.',
);
});
});
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('cookie', () => {

expect(() => getUserTokenCookieOptions()).toThrow(
'Croct\'s cookie duration must be a positive integer, got \'invalid\'. '
+ 'Please check the NEXT_PUBLIC_CROCT_USER_TOKEN_COOKIE_DURATION environment variable.',
+ 'Please check the `NEXT_PUBLIC_CROCT_USER_TOKEN_COOKIE_DURATION` environment variable.',
);
});

Expand All @@ -182,7 +182,7 @@ describe('cookie', () => {

expect(() => getUserTokenCookieOptions()).toThrow(
'Croct\'s cookie duration must be a positive integer, got \'-1\'. '
+ 'Please check the NEXT_PUBLIC_CROCT_USER_TOKEN_COOKIE_DURATION environment variable.',
+ 'Please check the `NEXT_PUBLIC_CROCT_USER_TOKEN_COOKIE_DURATION` environment variable.',
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/config/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getClientIdCookieOptions(): CookieOptions {
if (Number.isNaN(parsedDuration) || parsedDuration <= 0) {
throw new Error(
`Croct's cookie duration must be a positive integer, got '${duration}'. `
+ 'Please check the NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION environment variable.',
+ 'Please check the `NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION` environment variable.',
);
}

Expand All @@ -37,7 +37,7 @@ export function getUserTokenCookieOptions(): CookieOptions {
if (Number.isNaN(parsedDuration) || parsedDuration <= 0) {
throw new Error(
`Croct's cookie duration must be a positive integer, got '${duration}'. `
+ 'Please check the NEXT_PUBLIC_CROCT_USER_TOKEN_COOKIE_DURATION environment variable.',
+ 'Please check the `NEXT_PUBLIC_CROCT_USER_TOKEN_COOKIE_DURATION` environment variable.',
);
}

Expand Down
16 changes: 9 additions & 7 deletions src/config/security.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe('security', () => {
it('should throw an error if the API key is not set', () => {
expect(() => getApiKey()).toThrow(
'Croct\'s API key is missing. '
+ 'Did you forget to set the CROCT_API_KEY environment variable?',
+ 'Did you forget to set the `CROCT_API_KEY` environment variable?',
);
});

it('should throw an error if the API key is invalid', () => {
process.env.CROCT_API_KEY = 'invalid';

expect(() => getApiKey())
.toThrow('Croct\'s API key is invalid. Please check the CROCT_API_KEY environment variable.');
.toThrow('Croct\'s API key is invalid. Please check the `CROCT_API_KEY` environment variable.');
});
});

Expand All @@ -59,21 +59,23 @@ describe('security', () => {

expect(() => getAuthenticationKey()).toThrow(
'Croct\'s API key does not have a private key. '
+ 'Please generate an API key with authenticate permissions and update '
+ 'the CROCT_API_KEY environment variable.',
+ 'Please generate an API key with authenticate permissions and update '
+ 'the `CROCT_API_KEY` environment variable.',
);
});

it('should throw an error if the API key is not set', () => {
expect(() => getAuthenticationKey())
.toThrow('Croct\'s API key is missing. Did you forget to set the CROCT_API_KEY environment variable?');
expect(() => getAuthenticationKey()).toThrow(
'Croct\'s API key is missing. Did you forget to set the `CROCT_API_KEY` environment variable? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-environment-variable',
);
});

it('should throw an error if the API key is invalid', () => {
process.env.CROCT_API_KEY = 'invalid';

expect(() => getAuthenticationKey())
.toThrow('Croct\'s API key is invalid. Please check the CROCT_API_KEY environment variable.');
.toThrow('Croct\'s API key is invalid. Please check the `CROCT_API_KEY` environment variable.');
});
});

Expand Down
15 changes: 11 additions & 4 deletions src/config/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ export function getApiKey(): ApiKey {
const apiKey = process.env.CROCT_API_KEY;

if (apiKey === undefined) {
throw new Error('Croct\'s API key is missing. Did you forget to set the CROCT_API_KEY environment variable?');
throw new Error(
'Croct\'s API key is missing. '
+ 'Did you forget to set the `CROCT_API_KEY` environment variable? '
+ 'For help, see: https://croct.help/sdk/nextjs/missing-environment-variable',
);
}

try {
return ApiKey.parse(apiKey);
} catch {
throw new Error('Croct\'s API key is invalid. Please check the CROCT_API_KEY environment variable.');
throw new Error('Croct\'s API key is invalid. Please check the `CROCT_API_KEY` environment variable.');
}
}

Expand All @@ -24,7 +28,7 @@ export function getAuthenticationKey(): ApiKey {
throw new Error(
'Croct\'s API key does not have a private key. '
+ 'Please generate an API key with authenticate permissions and update '
+ 'the CROCT_API_KEY environment variable.',
+ 'the `CROCT_API_KEY` environment variable.',
);
}

Expand All @@ -46,7 +50,10 @@ export function getTokenDuration(): number {
const parsedDuration = Number.parseInt(duration, 10);

if (Number.isNaN(parsedDuration) || parsedDuration <= 0) {
throw new Error('The token duration must be a positive integer.');
throw new Error(
'The token duration must be a positive integer. '
+ 'Please check the `CROCT_TOKEN_DURATION` environment variable.',
);
}

return parsedDuration;
Expand Down
4 changes: 2 additions & 2 deletions src/config/timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('getDefaultFetchTimeout', () => {

expect(() => getDefaultFetchTimeout()).toThrow(
"Croct's default fetch timeout must be a non-negative integer, got 'invalid'. "
+ 'Please check the environment variable NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT.',
+ 'Please check the environment variable `NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT`.',
);
});

Expand All @@ -50,7 +50,7 @@ describe('getDefaultFetchTimeout', () => {

expect(() => getDefaultFetchTimeout()).toThrow(
"Croct's default fetch timeout must be a non-negative integer, got '-1'. "
+ 'Please check the environment variable NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT.',
+ 'Please check the environment variable `NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT`.',
);
});
});
2 changes: 1 addition & 1 deletion src/config/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getDefaultFetchTimeout(): number | undefined {
if (Number.isNaN(milliseconds) || milliseconds < 0) {
throw new Error(
`Croct's default fetch timeout must be a non-negative integer, got '${timeout}'. `
+ 'Please check the environment variable NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT.',
+ 'Please check the environment variable `NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT`.',
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('middleware', () => {

await expect(withCroct()(request, fetchEvent)).rejects.toThrow(
`Croct's cookie duration must be a positive integer, got '${duration}'.`
+ ' Please check the NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION environment variable.',
+ ' Please check the `NEXT_PUBLIC_CROCT_CLIENT_ID_COOKIE_DURATION` environment variable.',
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/server/anonymize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('anonymize', () => {

await expect(anonymize).rejects.toThrow(
'anonymize() requires specifying the `route` parameter outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/anonymize-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
);
});
});
2 changes: 1 addition & 1 deletion src/server/anonymize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function anonymize(context?: RouteContext): Promise<void> {
} catch {
throw new Error(
'anonymize() requires specifying the `route` parameter outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/anonymize-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@

await expect(evaluate('true')).rejects.toThrow(
'evaluate() requires specifying the `route` option outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/evaluate-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
);
});

Expand Down Expand Up @@ -233,12 +233,12 @@
logger?.warn('warning');
logger?.error('error');

expect(console.info).not.toHaveBeenCalled();

Check warning on line 236 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
expect(console.debug).not.toHaveBeenCalled();

Check warning on line 237 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
expect(console.warn).toHaveBeenCalledWith('warning');

Check warning on line 238 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
expect(console.error).toHaveBeenCalledWith('error');

Check warning on line 239 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

expect(console.log).not.toHaveBeenCalled();

Check warning on line 241 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
});

it('should log all messages if the debug mode is enabled', async () => {
Expand All @@ -264,12 +264,12 @@
logger?.warn('warning');
logger?.error('error');

expect(console.info).toHaveBeenCalledWith('info');

Check warning on line 267 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
expect(console.debug).toHaveBeenCalledWith('debug');

Check warning on line 268 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
expect(console.warn).toHaveBeenCalledWith('warning');

Check warning on line 269 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
expect(console.error).toHaveBeenCalledWith('error');

Check warning on line 270 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

expect(console.log).not.toHaveBeenCalled();

Check warning on line 272 in src/server/evaluate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
});

it('should use the base endpoint URL from the environment', async () => {
Expand Down Expand Up @@ -344,8 +344,8 @@
});

await expect(cql`true`).rejects.toThrow(
'The cql tag function can only be used with App Router. '
+ 'For help, see https://croct.help/sdk/nextjs/cql-missing-context',
'cql() can only be used with App Router. '
+ 'For help, see https://croct.help/sdk/nextjs/missing-route-context',
);
});

Expand Down
6 changes: 3 additions & 3 deletions src/server/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function evaluate<T extends JsonValue>(query: string, options: Evaluation
return Promise.reject(
new Error(
'evaluate() requires specifying the `route` option outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/evaluate-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
),
);
}
Expand Down Expand Up @@ -70,8 +70,8 @@ export function cql<T extends JsonValue>(fragments: TemplateStringsArray, ...arg
if (!isAppRouter()) {
return Promise.reject(
new Error(
'The cql tag function can only be used with App Router. '
+ 'For help, see https://croct.help/sdk/nextjs/cql-missing-context',
'cql() can only be used with App Router. '
+ 'For help, see https://croct.help/sdk/nextjs/missing-route-context',
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/fetchContent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('fetchContent', () => {

await expect(fetchContent('slot-id')).rejects.toThrow(
'fetchContent() requires specifying the `route` option outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/fetch-content-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/server/fetchContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function fetchContent<I extends VersionedSlotId, C extends JsonObject>(
return Promise.reject(
new Error(
'fetchContent() requires specifying the `route` option outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/fetch-content-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/identify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('identify', () => {

await expect(() => identify('foo')).rejects.toThrow(
'identify() requires specifying the `route` parameter outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/identify-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
);
});
});
2 changes: 1 addition & 1 deletion src/server/identify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function identify(userId: string, route?: RouteContext): Promise<vo
} catch {
throw new Error(
'identify() requires specifying the `route` parameter outside app routes. '
+ 'For help, see: https://croct.help/sdk/nextjs/identify-route-context',
+ 'For help, see: https://croct.help/sdk/nextjs/missing-route-context',
);
}

Expand Down