-
Notifications
You must be signed in to change notification settings - Fork 405
feat(clerk-js): Type errors.global as ClerkGlobalHookError
#7174
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
Conversation
🦋 Changeset detectedLatest commit: ccb61e3 The changes in this PR will be included in the next version bump. This PR includes changesets to release 22 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughAdds createClerkGlobalHookError to wrap ClerkError objects with predicate helpers, re-exports ClerkError/createClerkGlobalHookError, and narrows several error typings from Changes
Sequence DiagramsequenceDiagram
autonumber
actor User
participant Signals as SignalsHandler
participant Parser as errorsToParsedErrors
participant GlobalWrap as createClerkGlobalHookError
User->>Signals: Trigger sign-in/sign-up error
activate Signals
Signals->>Parser: pass error (ClerkError | null)
activate Parser
alt hasFieldErrors
Parser->>Parser: map field errors -> fields & raw
Parser-->>Signals: { raw, fields, global: [] }
else no field errors
Parser->>GlobalWrap: wrap error
activate GlobalWrap
GlobalWrap->>GlobalWrap: attach predicates (isClerkApiResponseError / isClerkRuntimeError)
GlobalWrap-->>Parser: ClerkGlobalHookError
Parser->>Parser: populate raw & global with wrapped error
Parser-->>Signals: { raw, fields: [], global: [ClerkGlobalHookError] }
deactivate GlobalWrap
end
deactivate Parser
Signals->>Signals: emit signInErrorSignal / signUpErrorSignal
deactivate Signals
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Disabled knowledge base sources:
📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
packages/shared/src/errors/globalHookError.ts (1)
10-10: Add explicit return type annotation.The function lacks an explicit return type annotation, which violates the coding guideline for public APIs.
Apply this diff:
-export function createClerkGlobalHookError(error: ClerkError) { +export function createClerkGlobalHookError(error: ClerkError): ClerkGlobalHookError {Based on coding guidelines: "Always define explicit return types for functions, especially public APIs."
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
packages/shared/src/errors/globalHookError.ts(1 hunks)packages/shared/src/types/state.ts(4 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels
Files:
packages/shared/src/errors/globalHookError.tspackages/shared/src/types/state.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/shared/src/errors/globalHookError.tspackages/shared/src/types/state.ts
packages/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/shared/src/errors/globalHookError.tspackages/shared/src/types/state.ts
packages/**/*.{ts,tsx,d.ts}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Packages should export TypeScript types alongside runtime code
Files:
packages/shared/src/errors/globalHookError.tspackages/shared/src/types/state.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoidanytype - preferunknownwhen type is uncertain, then narrow with type guards
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Preferreadonlyfor properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Useconst assertionsfor literal types:as const
Usesatisfiesoperator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports:import type { ... } from ...
Noanytypes without justification
Proper error handling with typed errors
Consistent use ofreadonlyfor immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)
Files:
packages/shared/src/errors/globalHookError.tspackages/shared/src/types/state.ts
**/*.{js,ts,tsx,jsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.
Files:
packages/shared/src/errors/globalHookError.tspackages/shared/src/types/state.ts
🧬 Code graph analysis (2)
packages/shared/src/errors/globalHookError.ts (1)
packages/shared/src/errors/clerkError.ts (1)
ClerkError(36-74)
packages/shared/src/types/state.ts (1)
packages/shared/src/errors/globalHookError.ts (1)
ClerkGlobalHookError(23-23)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
- GitHub Check: Formatting | Dedupe | Changeset
- GitHub Check: Build Packages
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: semgrep-cloud-platform/scan
- GitHub Check: semgrep-cloud-platform/scan
| export function createClerkGlobalHookError(error: ClerkError) { | ||
| const predicates = { | ||
| isClerkApiResponseError, | ||
| isClerkRuntimeError, | ||
| } as const; | ||
|
|
||
| for (const [name, fn] of Object.entries(predicates)) { | ||
| Object.assign(error, { [name]: fn }); | ||
| } | ||
|
|
||
| return error as ClerkError & typeof predicates; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify predicate binding and avoid mutating the input parameter.
The function mutates the input error object by attaching predicates to it, which violates immutability principles. More critically, the attached predicates (isClerkApiResponseError, isClerkRuntimeError) are likely type guards that expect an error parameter (e.g., (error: unknown) => error is ClerkApiResponseError). Attaching them unbound creates a confusing API where consumers would need to call error.isClerkApiResponseError(error), which is redundant.
If the intent is to provide predicate methods that check the error itself, bind them by wrapping in closures:
export function createClerkGlobalHookError(error: ClerkError) {
const predicates = {
- isClerkApiResponseError,
- isClerkRuntimeError,
+ isClerkApiResponseError: () => isClerkApiResponseError(error),
+ isClerkRuntimeError: () => isClerkRuntimeError(error),
} as const;
- for (const [name, fn] of Object.entries(predicates)) {
- Object.assign(error, { [name]: fn });
- }
-
- return error as ClerkError & typeof predicates;
+ return Object.assign({}, error, predicates) as ClerkError & typeof predicates;
}This eliminates mutation and provides a cleaner API where error.isClerkApiResponseError() checks the error itself.
Based on coding guidelines: "Prefer readonly properties for immutable data structures."
🤖 Prompt for AI Agents
In packages/shared/src/errors/globalHookError.ts around lines 10 to 21, the
current implementation mutates the incoming error by assigning unbound predicate
functions to it; instead, return a new object that preserves the original error
properties (don’t mutate the input) and add readonly predicate methods that are
zero-argument closures which call the original type-guard predicates with the
original error value (e.g., () => isClerkApiResponseError(origError)); ensure
these added properties are non-writable/read-only (use Object.defineProperty or
equivalent) and return the result typed as ClerkError & typeof predicates so
consumers can call error.isClerkApiResponseError() without passing the error and
immutability is preserved.
Co-authored-by: Nikos Douvlis <nikosdouvlis@gmail.com>
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/elements
@clerk/clerk-expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/clerk-react
@clerk/react-router
@clerk/remix
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/themes
@clerk/types
@clerk/upgrade
@clerk/vue
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (6)
packages/clerk-js/src/core/events.ts(2 hunks)packages/clerk-js/src/core/signals.ts(5 hunks)packages/clerk-js/src/core/state.ts(2 hunks)packages/shared/src/error.ts(2 hunks)packages/shared/src/errors/clerkApiError.ts(2 hunks)packages/shared/src/errors/globalHookError.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/shared/src/errors/globalHookError.ts
🧰 Additional context used
📓 Path-based instructions (6)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels
Files:
packages/clerk-js/src/core/state.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/clerk-js/src/core/signals.tspackages/clerk-js/src/core/events.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/clerk-js/src/core/state.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/clerk-js/src/core/signals.tspackages/clerk-js/src/core/events.ts
packages/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/clerk-js/src/core/state.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/clerk-js/src/core/signals.tspackages/clerk-js/src/core/events.ts
packages/**/*.{ts,tsx,d.ts}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Packages should export TypeScript types alongside runtime code
Files:
packages/clerk-js/src/core/state.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/clerk-js/src/core/signals.tspackages/clerk-js/src/core/events.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoidanytype - preferunknownwhen type is uncertain, then narrow with type guards
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Preferreadonlyfor properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Useconst assertionsfor literal types:as const
Usesatisfiesoperator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports:import type { ... } from ...
Noanytypes without justification
Proper error handling with typed errors
Consistent use ofreadonlyfor immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)
Files:
packages/clerk-js/src/core/state.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/clerk-js/src/core/signals.tspackages/clerk-js/src/core/events.ts
**/*.{js,ts,tsx,jsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.
Files:
packages/clerk-js/src/core/state.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/clerk-js/src/core/signals.tspackages/clerk-js/src/core/events.ts
🧬 Code graph analysis (3)
packages/clerk-js/src/core/state.ts (1)
packages/shared/src/error.ts (1)
ClerkError(5-5)
packages/clerk-js/src/core/signals.ts (2)
packages/shared/src/types/state.ts (1)
Errors(72-86)packages/shared/src/errors/globalHookError.ts (1)
createClerkGlobalHookError(10-21)
packages/clerk-js/src/core/events.ts (1)
packages/shared/src/error.ts (1)
ClerkError(5-5)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
- GitHub Check: Formatting | Dedupe | Changeset
- GitHub Check: Build Packages
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: semgrep-cloud-platform/scan
- GitHub Check: semgrep-cloud-platform/scan
| // At this point, we know that `error` is a ClerkAPIResponseError and that it has no field errors. | ||
| parsedErrors.raw = [error]; | ||
| parsedErrors.global = [createClerkGlobalHookError(error)]; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore the raw API errors array
Errors.raw is documented (packages/shared/src/types/state.ts Line 74) as “The raw, unparsed errors from the Clerk API.” Storing the aggregate ClerkAPIResponseError here drops the per-error payload consumers rely on. Keep raw as the API error list.
Apply this diff to preserve the original payload:
- parsedErrors.raw = [error];
+ parsedErrors.raw = [...error.errors];
parsedErrors.global = [createClerkGlobalHookError(error)];🤖 Prompt for AI Agents
In packages/clerk-js/src/core/signals.ts around lines 88 to 91, parsedErrors.raw
is being set to the aggregate ClerkAPIResponseError object instead of the
original per-error payload; change the assignment to preserve the API error list
by setting parsedErrors.raw to the error.errors array (or an empty array
fallback if needed) so consumers receive the raw, unparsed error entries, while
keeping parsedErrors.global as createClerkGlobalHookError(error).
errors.global as ClerkGlobalHookErrorerrors.global as ClerkGlobalHookError
Description
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit