From a1940d5db53fe3d2ec17f72ebd17d9574dbbf1a3 Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Fri, 6 Sep 2024 17:50:48 -0600 Subject: [PATCH 1/2] Update error messages --- src/CroctProvider.test.tsx | 5 +++-- src/config/appId.test.ts | 14 ++++++++------ src/config/appId.ts | 9 +++++---- src/config/context.test.ts | 6 ++++-- src/config/context.ts | 5 ++++- src/config/cookie.test.ts | 8 ++++---- src/config/cookie.ts | 4 ++-- src/config/security.test.ts | 16 +++++++++------- src/config/security.ts | 15 +++++++++++---- src/config/timeout.test.ts | 4 ++-- src/config/timeout.ts | 2 +- src/middleware.test.ts | 2 +- src/server/anonymize.test.ts | 2 +- src/server/anonymize.ts | 2 +- src/server/evaluate.test.ts | 6 +++--- src/server/evaluate.ts | 6 +++--- src/server/fetchContent.test.ts | 2 +- src/server/fetchContent.ts | 2 +- src/server/identify.test.ts | 2 +- src/server/identify.ts | 2 +- 20 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/CroctProvider.test.tsx b/src/CroctProvider.test.tsx index 7c54dd9..6969a79 100644 --- a/src/CroctProvider.test.tsx +++ b/src/CroctProvider.test.tsx @@ -41,8 +41,9 @@ describe('', () => { process.env.NEXT_PUBLIC_CROCT_APP_ID = ''; expect(() => render()).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', ); }); diff --git a/src/config/appId.test.ts b/src/config/appId.test.ts index 26feb34..e7f0c8e 100644 --- a/src/config/appId.test.ts +++ b/src/config/appId.test.ts @@ -7,8 +7,9 @@ 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', ); }); @@ -16,8 +17,9 @@ describe('getAppId', () => { 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', ); }); @@ -25,8 +27,8 @@ describe('getAppId', () => { 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.', ); }); diff --git a/src/config/appId.ts b/src/config/appId.ts index 023706c..67d4eef 100644 --- a/src/config/appId.ts +++ b/src/config/appId.ts @@ -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.', ); } diff --git a/src/config/context.test.ts b/src/config/context.test.ts index 60ce5aa..d431266 100644 --- a/src/config/context.test.ts +++ b/src/config/context.test.ts @@ -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-provider', + ); }); it('should return a full request context', () => { diff --git a/src/config/context.ts b/src/config/context.ts index b83f344..83cefbe 100644 --- a/src/config/context.ts +++ b/src/config/context.ts @@ -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-provider', + ); } const context: RequestContext = { diff --git a/src/config/cookie.test.ts b/src/config/cookie.test.ts index 056e69f..9c63a10 100644 --- a/src/config/cookie.test.ts +++ b/src/config/cookie.test.ts @@ -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.', ); }); @@ -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.', ); }); }); @@ -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.', ); }); @@ -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.', ); }); }); diff --git a/src/config/cookie.ts b/src/config/cookie.ts index 9f94f46..94ca349 100644 --- a/src/config/cookie.ts +++ b/src/config/cookie.ts @@ -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.', ); } @@ -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.', ); } diff --git a/src/config/security.test.ts b/src/config/security.test.ts index cfa76d9..0405c89 100644 --- a/src/config/security.test.ts +++ b/src/config/security.test.ts @@ -31,7 +31,7 @@ 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?', ); }); @@ -39,7 +39,7 @@ describe('security', () => { 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.'); }); }); @@ -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.'); }); }); diff --git a/src/config/security.ts b/src/config/security.ts index 151bf87..e4baa7d 100644 --- a/src/config/security.ts +++ b/src/config/security.ts @@ -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.'); } } @@ -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.', ); } @@ -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; diff --git a/src/config/timeout.test.ts b/src/config/timeout.test.ts index 1df8817..e32c203 100644 --- a/src/config/timeout.test.ts +++ b/src/config/timeout.test.ts @@ -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`.', ); }); @@ -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`.', ); }); }); diff --git a/src/config/timeout.ts b/src/config/timeout.ts index ea1b005..1fac971 100644 --- a/src/config/timeout.ts +++ b/src/config/timeout.ts @@ -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`.', ); } diff --git a/src/middleware.test.ts b/src/middleware.test.ts index 591848b..869143b 100644 --- a/src/middleware.test.ts +++ b/src/middleware.test.ts @@ -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.', ); }); diff --git a/src/server/anonymize.test.ts b/src/server/anonymize.test.ts index aa5a31a..bfe430e 100644 --- a/src/server/anonymize.test.ts +++ b/src/server/anonymize.test.ts @@ -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', ); }); }); diff --git a/src/server/anonymize.ts b/src/server/anonymize.ts index 81a60ea..8f685b3 100644 --- a/src/server/anonymize.ts +++ b/src/server/anonymize.ts @@ -10,7 +10,7 @@ export async function anonymize(context?: RouteContext): Promise { } 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', ); } diff --git a/src/server/evaluate.test.ts b/src/server/evaluate.test.ts index 6b4c58f..c02bbb8 100644 --- a/src/server/evaluate.test.ts +++ b/src/server/evaluate.test.ts @@ -193,7 +193,7 @@ describe('evaluation', () => { 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', ); }); @@ -344,8 +344,8 @@ describe('evaluation', () => { }); 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', ); }); diff --git a/src/server/evaluate.ts b/src/server/evaluate.ts index eb62cbe..cfd22e0 100644 --- a/src/server/evaluate.ts +++ b/src/server/evaluate.ts @@ -24,7 +24,7 @@ export function evaluate(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', ), ); } @@ -70,8 +70,8 @@ export function cql(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', ), ); } diff --git a/src/server/fetchContent.test.ts b/src/server/fetchContent.test.ts index b1946fc..8b4e52a 100644 --- a/src/server/fetchContent.test.ts +++ b/src/server/fetchContent.test.ts @@ -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', ); }); diff --git a/src/server/fetchContent.ts b/src/server/fetchContent.ts index 13903b5..8bf1209 100644 --- a/src/server/fetchContent.ts +++ b/src/server/fetchContent.ts @@ -27,7 +27,7 @@ export function fetchContent( 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', ), ); } diff --git a/src/server/identify.test.ts b/src/server/identify.test.ts index 0580d8f..08d5154 100644 --- a/src/server/identify.test.ts +++ b/src/server/identify.test.ts @@ -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', ); }); }); diff --git a/src/server/identify.ts b/src/server/identify.ts index 2bd569f..c2e1b00 100644 --- a/src/server/identify.ts +++ b/src/server/identify.ts @@ -10,7 +10,7 @@ export async function identify(userId: string, route?: RouteContext): Promise Date: Fri, 6 Sep 2024 18:08:28 -0600 Subject: [PATCH 2/2] Update error messages --- src/config/context.test.ts | 2 +- src/config/context.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/context.test.ts b/src/config/context.test.ts index d431266..92907e2 100644 --- a/src/config/context.test.ts +++ b/src/config/context.test.ts @@ -39,7 +39,7 @@ describe('getRequestContext', () => { 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? ' - + 'For help, see: https://croct.help/sdk/nextjs/missing-provider', + + 'For help, see: https://croct.help/sdk/nextjs/missing-middleware', ); }); diff --git a/src/config/context.ts b/src/config/context.ts index 83cefbe..8cd9638 100644 --- a/src/config/context.ts +++ b/src/config/context.ts @@ -24,7 +24,7 @@ export function getRequestContext(headers: HeaderReader, cookies: CookieReader): if (clientId === null) { 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-provider', + + 'For help, see: https://croct.help/sdk/nextjs/missing-middleware', ); }