-
Notifications
You must be signed in to change notification settings - Fork 425
refactor(shared): Update exported error typeguard functions from error.ts, align error class naming #7490
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
refactor(shared): Update exported error typeguard functions from error.ts, align error class naming #7490
Conversation
…r.ts, align error class naming Signed-off-by: Kenton Duprey <[email protected]>
🦋 Changeset detectedLatest commit: 6741930 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.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThis pull request refactors Clerk's error handling infrastructure by consolidating duplicate type guard implementations and aligning naming conventions. Type guards previously defined in helpers.ts are removed and replaced with imports from their respective error modules. Additionally, inconsistent "ClerkApi" naming patterns are standardized to "ClerkAPI" across type guards and class definitions. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
Comment |
Signed-off-by: Kenton Duprey <[email protected]>
@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: |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
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: 0
🧹 Nitpick comments (1)
packages/shared/src/errors/clerkApiError.ts (1)
9-35: Review type safety in generic parameter and constructor.Several type safety considerations:
Line 9: The generic parameter default is
any, which bypasses TypeScript's type checking. Per coding guidelines, preferunknownwhen the type is uncertain.Line 29: The metadata parsing uses
as unknown as Meta, which completely bypasses type safety. The parsed metadata structure may not actually match the expectedMetatype.Consider this safer approach:
-export class ClerkAPIError<Meta extends ClerkAPIErrorMeta = any> implements ClerkAPIErrorInterface { +export class ClerkAPIError<Meta extends ClerkAPIErrorMeta = ClerkAPIErrorMeta> implements ClerkAPIErrorInterface { static kind = 'ClerkAPIError'; readonly code: string; readonly message: string; readonly longMessage: string | undefined; readonly meta: Meta; constructor(json: ClerkAPIErrorJSON) { const parsedError = { code: json.code, message: json.message, longMessage: json.long_message, meta: { paramName: json.meta?.param_name, sessionId: json.meta?.session_id, emailAddresses: json.meta?.email_addresses, identifiers: json.meta?.identifiers, zxcvbn: json.meta?.zxcvbn, plan: json.meta?.plan, isPlanUpgradePossible: json.meta?.is_plan_upgrade_possible, - } as unknown as Meta, + } as Meta, }; this.code = parsedError.code; this.message = parsedError.message; this.longMessage = parsedError.longMessage; this.meta = parsedError.meta; } }This approach:
- Uses
ClerkAPIErrorMetaas the default instead ofany- Removes the double assertion (
as unknown as Meta)- Still allows flexibility for specific metadata types through the generic parameter
📜 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)
.changeset/sweet-singers-beg.md(1 hunks)packages/shared/src/error.ts(1 hunks)packages/shared/src/errors/clerkApiError.ts(2 hunks)packages/shared/src/errors/clerkApiResponseError.ts(1 hunks)packages/shared/src/errors/globalHookError.ts(2 hunks)packages/shared/src/errors/helpers.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (9)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
All code must pass ESLint checks with the project's configuration
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
**/*.{js,jsx,ts,tsx,json,md,yml,yaml}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
packages/**/src/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Follow established naming conventions (PascalCase for components, camelCase for variables)
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
packages/**/src/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
packages/**/src/**/*.{ts,tsx,js,jsx}: Maintain comprehensive JSDoc comments for public APIs
Use tree-shaking friendly exports
Validate all inputs and sanitize outputs
All public APIs must be documented with JSDoc
Use dynamic imports for optional features
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
Implement proper logging with different levels
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
**/*.ts?(x)
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/typescript.mdc)
**/*.{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
Implement type guards forunknowntypes using the patternfunction isType(value: unknown): value is Type
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details in classes
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Use mixins for shared behavior across unrelated classes in TypeScript
Use generic constraints with bounded type parameters like<T extends { id: string }>
Use utility types likeOmit,Partial, andPickfor data transformation instead of manual type construction
Use discriminated unions instead of boolean flags for state management and API responses
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation at the type level
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Document functions with JSDoc comments including @param, @returns, @throws, and @example tags
Create custom error classes that extend Error for specific error types
Use the Result pattern for error handling instead of throwing exceptions
Use optional chaining (?.) and nullish coalescing (??) operators for safe property access
Let TypeScript infer obvious types to reduce verbosity
Useconst assertionswithas constfor literal types
Usesatisfiesoperator for type checking without widening types
Declare readonly arrays and objects for immutable data structures
Use spread operator and array spread for immutable updates instead of mutations
Use lazy loading for large types...
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
**/*.{js,ts,jsx,tsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Use ESLint with custom configurations tailored for different package types
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
**/*.{js,ts,jsx,tsx,json,md,yml,yaml}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Use Prettier for code formatting across all packages
Files:
packages/shared/src/errors/clerkApiResponseError.tspackages/shared/src/errors/helpers.tspackages/shared/src/errors/clerkApiError.tspackages/shared/src/error.tspackages/shared/src/errors/globalHookError.ts
🪛 LanguageTool
.changeset/sweet-singers-beg.md
[grammar] ~5-~5: Use a hyphen to join words.
Context: ...patch --- Refactor internal Clerk error handling functions
(QB_NEW_EN_HYPHEN)
⏰ 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). (10)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
- GitHub Check: Integration Tests (elements, chrome)
🔇 Additional comments (6)
packages/shared/src/errors/helpers.ts (1)
1-4: LGTM! Type guard imports centralized.The refactoring successfully replaces the previously faulty in-file type guard implementations with imports from their dedicated modules, aligning with the PR's core objective to fix type assertion issues.
packages/shared/src/errors/clerkApiError.ts (2)
4-4: LGTM! Type naming aligned.The renaming from
ClerkApiErrorMetatoClerkAPIErrorMetamaintains consistency with the ClerkAPI* naming convention.
38-41: LGTM! Type guard renamed consistently.The type guard renaming from
isClerkApiErrortoisClerkAPIErroraligns with the ClerkAPI* naming convention.packages/shared/src/errors/globalHookError.ts (1)
1-1: LGTM! Import and usage updated consistently.The import and predicates object key have been updated to use
isClerkAPIResponseError, maintaining consistency with the renamed export. This is a breaking API change forClerkGlobalHookError, but the migration has already been completed throughout the codebase—all consumers are using the new nameisClerkAPIResponseError.packages/shared/src/error.ts (1)
3-5: LGTM! Type guard exports refactored correctly.The refactoring successfully consolidates type guard exports from their dedicated modules, addressing the core PR objective. Each error type now exports its own type guard:
isClerkAPIErrorfromclerkApiErrorisClerkAPIResponseErrorfromclerkApiResponseErrorisClerkErrorfromclerkErrorisClerkRuntimeErrorfromclerkRuntimeErrorThis modular approach replaces the previous faulty type checks in
helpers.ts.Verify that the new export structure doesn't break existing consumers:
#!/bin/bash # Description: Verify imports of error type guards across the codebase # Check for imports from @clerk/shared/errors or @clerk/shared/error rg -nP "import\s+.*\{\s*.*is(?:Clerk(?:API|Runtime)|Clerk).*\}" --type ts --type tsx -g '!packages/shared/**'Also applies to: 13-13
packages/shared/src/errors/clerkApiResponseError.ts (1)
49-60: Naming convention properly aligned - migration complete.The type guard renaming from
isClerkApiResponseErrortoisClerkAPIResponseErrorcorrectly aligns with the established ClerkAPI* naming convention. The implementation is correct, JSDoc is properly documented, and all usages across the codebase have been successfully updated to the new convention. The only historical reference to the old name appears in the changelog documenting the original functionality.
… todo for core 3 breaking change Signed-off-by: Kenton Duprey <[email protected]>
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.
Seems 👍
…rors-pkg-type-assertions-due-to-type
…rors-pkg-type-assertions-due-to-type
…r.ts, align error class naming (#7490)
Description
Currently, in
handleError()inclerk-js/src/ui/utils/errorHandler.tsthe function runs through each type of predefined error type we have and handles the error from there. If the error is aClerkRuntimeError, the intended handler is never reached due to type overlap betweenClerkAPIResponseErrorandClerkRuntimeError.Both interfaces extend
ClerkErrorwhich causesClerkRuntimeErrorerror types to be handled byhandleClerkApiErrorversushandleClerkRuntimeErrorgiven the order of the logic gates.We have recently added
createErrorTypeGuard()to properly check the error type against the provided interface, and utility functions were added to each error definition file (e.g.clerkRuntimeError.ts). We do have pre-existing functions inshared/src/errors/helpers.tshowever, they are not using the new type guard check functions provided from each error definition file.This removes the old/faulty error type checks from helpers.ts in favour of the new type guard functions, updates the exported functions in
shared/src/error.ts, and also aligns the naming pattern forisClerkApiResponseErrorandisClerkAPIResponseError.USER-4239
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.