Skip to content

Commit a26ecae

Browse files
authored
chore(clerk-react,shared): Improve assertion error for requiring active organization (#6606)
1 parent 083820d commit a26ecae

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

.changeset/plain-bottles-press.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clerk/shared': patch
3+
'@clerk/clerk-react': patch
4+
---
5+
6+
Improve assertion error for requiring active organization.

packages/react/src/components/CheckoutButton.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { withClerk } from './withClerk';
3131
* <CheckoutButton
3232
* planId="plan_123"
3333
* planPeriod="month"
34-
* subscriberType="org"
34+
* for="organization"
3535
* onSubscriptionComplete={() => console.log('Subscription completed!')}
3636
* >
3737
* <button className="custom-button">Subscribe Now</button>
@@ -42,7 +42,7 @@ import { withClerk } from './withClerk';
4242
* ```
4343
*
4444
* @throws {Error} When rendered outside of a `<SignedIn />` component
45-
* @throws {Error} When `subscriberType="org"` is used without an active organization context
45+
* @throws {Error} When `for="organization"` is used without an active organization context
4646
*/
4747
export const CheckoutButton = withClerk(
4848
({ clerk, children, ...props }: WithClerkProp<React.PropsWithChildren<__experimental_CheckoutButtonProps>>) => {
@@ -59,11 +59,13 @@ export const CheckoutButton = withClerk(
5959
const { userId, orgId } = useAuth();
6060

6161
if (userId === null) {
62-
throw new Error('Ensure that `<CheckoutButton />` is rendered inside a `<SignedIn />` component.');
62+
throw new Error('Clerk: Ensure that `<CheckoutButton />` is rendered inside a `<SignedIn />` component.');
6363
}
6464

6565
if (orgId === null && _for === 'organization') {
66-
throw new Error('Wrap `<CheckoutButton for="organization" />` with a check for an active organization.');
66+
throw new Error(
67+
'Clerk: Wrap `<CheckoutButton for="organization" />` with a check for an active organization. Retrieve `orgId` from `useAuth()` and confirm it is defined. For SSR, see: https://clerk.com/docs/references/backend/types/auth-object#how-to-access-the-auth-object',
68+
);
6769
}
6870

6971
children = normalizeWithDefaultValue(children, 'Checkout');

packages/react/src/components/SubscriptionDetailsButton.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { withClerk } from './withClerk';
2626
* function OrganizationSubscriptionDetails() {
2727
* return (
2828
* <SubscriptionDetailsButton
29-
* for="org"
29+
* for="organization"
3030
* onSubscriptionCancel={() => console.log('Subscription canceled')}
3131
* >
3232
* <button>View Organization Subscription</button>
@@ -36,7 +36,7 @@ import { withClerk } from './withClerk';
3636
* ```
3737
*
3838
* @throws {Error} When rendered outside of a `<SignedIn />` component
39-
* @throws {Error} When `for="org"` is used without an active organization context
39+
* @throws {Error} When `for="organization"` is used without an active organization context
4040
*
4141
* @see https://clerk.com/docs/billing/overview
4242
*/
@@ -53,12 +53,14 @@ export const SubscriptionDetailsButton = withClerk(
5353
const { userId, orgId } = useAuth();
5454

5555
if (userId === null) {
56-
throw new Error('Ensure that `<SubscriptionDetailsButton />` is rendered inside a `<SignedIn />` component.');
56+
throw new Error(
57+
'Clerk: Ensure that `<SubscriptionDetailsButton />` is rendered inside a `<SignedIn />` component.',
58+
);
5759
}
5860

5961
if (orgId === null && _for === 'organization') {
6062
throw new Error(
61-
'Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization.',
63+
'Clerk: Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization. Retrieve `orgId` from `useAuth()` and confirm it is defined. For SSR, see: https://clerk.com/docs/references/backend/types/auth-object#how-to-access-the-auth-object',
6264
);
6365
}
6466

packages/react/src/components/__tests__/CheckoutButton.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ describe('CheckoutButton', () => {
6262
for='organization'
6363
/>,
6464
),
65-
).toThrow('Wrap `<CheckoutButton for="organization" />` with a check for an active organization.');
65+
).toThrow(
66+
'Wrap `<CheckoutButton for="organization" />` with a check for an active organization. Retrieve `orgId` from `useAuth()` and confirm it is defined.',
67+
);
6668
});
6769

6870
it('renders successfully with authenticated user', () => {

packages/react/src/components/__tests__/SubscriptionDetailsButton.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('SubscriptionDetailsButton', () => {
5656

5757
// Expect the component to throw an error when for="organization"
5858
expect(() => render(<SubscriptionDetailsButton for='organization' />)).toThrow(
59-
'Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization.',
59+
'Wrap `<SubscriptionDetailsButton for="organization" />` with a check for an active organization. Retrieve `orgId` from `useAuth()` and confirm it is defined.',
6060
);
6161
});
6262

packages/shared/src/react/hooks/useCheckout.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export const useCheckout = (options?: Params): __experimental_UseCheckoutReturn
8484
}
8585

8686
if (forOrganization === 'organization' && !organization) {
87-
throw new Error('Clerk: Wrap your flow with a check for an active organization');
87+
throw new Error(
88+
'Clerk: Ensure your flow checks for an active organization. Retrieve `orgId` from `useAuth()` and confirm it is defined. For SSR, see: https://clerk.com/docs/references/backend/types/auth-object#how-to-access-the-auth-object',
89+
);
8890
}
8991

9092
const manager = useMemo(

0 commit comments

Comments
 (0)