Skip to content

Commit

Permalink
Update error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Sep 6, 2024
1 parent 131c6b1 commit a1940d5
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 48 deletions.
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-provider',
);
});

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-provider',
);
}

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 @@ 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',
);
});

Expand Down Expand Up @@ -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',
);
});

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

0 comments on commit a1940d5

Please sign in to comment.