Skip to content

fix(clerk-js): Pass metadata as variables for error localization #6167

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anagstef
Copy link
Member

@anagstef anagstef commented Jun 20, 2025

Description

When using localization on unstable_errors we pass no variables, although in some cases FAPI returns values in the meta property. This PR allows to use the following 2 identifiers that are included in the error object coming from FAPI in the localized error message:

  • identifiers: This is an array of identifiers
  • email_addresses: This is an array of email addresses

Example error from FAPI:

{
  "code": "not_allowed_access",
  "meta": {
    "identifiers": [
      "[email protected]"
    ]
  },
  "message": "Access not allowed.",
  "long_message": "[email protected] is not allowed to access this application."
}

Example localization value provided by the user:

localization={{
  unstable__errors: {
    not_allowed_access: '{{identifiers}} is not allowed to access this page!!!!!',
  },
}}

Resulting in the following error:

[email protected] is not allowed to access this page!!!!!

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • New Features
    • Enhanced error localization to include additional metadata, allowing error messages to display more detailed and dynamic information such as identifiers and email addresses.

Copy link

changeset-bot bot commented Jun 20, 2025

🦋 Changeset detected

Latest commit: 61a42ae

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@clerk/clerk-js Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch

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

Copy link

vercel bot commented Jun 20, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 20, 2025 4:20pm

Copy link
Contributor

coderabbitai bot commented Jun 20, 2025

📝 Walkthrough
## Walkthrough

A new changeset file was added to document a patch update for the `@clerk/clerk-js` package, describing changes related to passing metadata as variables for error localization. In the source code, the `translateError` function within `useLocalizations` was updated to extract additional metadata fields (`identifiers` and `emailAddresses`) from the error's `meta` object. These fields are used to construct a variables object, which is then passed to the translation function to support dynamic token replacement in localized error messages.

## Suggested labels

`clerk-js`

## Suggested reviewers

- aeliox
- tmilewski

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5ee3b24 and 61a42ae.

📒 Files selected for processing (1)
  • packages/clerk-js/src/ui/localization/makeLocalizable.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/clerk-js/src/ui/localization/makeLocalizable.tsx
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: Build Packages
  • GitHub Check: Analyze (javascript-typescript)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

🧹 Nitpick comments (1)
packages/clerk-js/src/ui/localization/makeLocalizable.tsx (1)

92-92: Consider adding type safety for destructured metadata fields.

The destructuring assumes identifiers and emailAddresses exist in the meta object, but there's no validation of their structure or type.

-const { paramName = '', identifiers, emailAddresses } = meta || {};
+const { paramName = '', identifiers = [], emailAddresses = [] } = meta || {};
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ce6a5c and 5ee3b24.

📒 Files selected for processing (2)
  • .changeset/twenty-games-repair.md (1 hunks)
  • packages/clerk-js/src/ui/localization/makeLocalizable.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: Build Packages
  • GitHub Check: Formatting | Dedupe | Changeset
  • GitHub Check: semgrep/ci
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (2)
packages/clerk-js/src/ui/localization/makeLocalizable.tsx (1)

101-102: LGTM! Metadata variables are correctly passed to localization function.

The implementation correctly passes the metadata variables to both the parameterized and non-parameterized error key lookups, enabling dynamic token replacement in localized error messages as intended.

.changeset/twenty-games-repair.md (1)

1-6: LGTM! Changeset follows standard format and accurately describes the changes.

The changeset correctly identifies this as a patch-level change for @clerk/clerk-js and provides a clear, concise description of the enhancement.


if (!code) {
return '';
}

const metaArgsAsVariables = { identifiers: identifiers?.[0], emailAddresses: emailAddresses?.[0] };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle edge cases for array access and improve variable naming.

The current implementation has several potential issues:

  1. No validation that identifiers and emailAddresses are arrays
  2. Silent failure if arrays are empty (returns undefined)
  3. Variable names don't match the original field names, which could cause confusion
-const metaArgsAsVariables = { identifiers: identifiers?.[0], emailAddresses: emailAddresses?.[0] };
+const metaArgsAsVariables = {
+  identifiers: Array.isArray(identifiers) && identifiers.length > 0 ? identifiers[0] : undefined,
+  emailAddresses: Array.isArray(emailAddresses) && emailAddresses.length > 0 ? emailAddresses[0] : undefined,
+};

Alternatively, if you want to maintain consistency with the original field names from the PR objectives:

-const metaArgsAsVariables = { identifiers: identifiers?.[0], emailAddresses: emailAddresses?.[0] };
+const metaArgsAsVariables = {
+  identifiers: Array.isArray(identifiers) && identifiers.length > 0 ? identifiers[0] : undefined,
+  email_addresses: Array.isArray(emailAddresses) && emailAddresses.length > 0 ? emailAddresses[0] : undefined,
+};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const metaArgsAsVariables = { identifiers: identifiers?.[0], emailAddresses: emailAddresses?.[0] };
const metaArgsAsVariables = {
identifiers: Array.isArray(identifiers) && identifiers.length > 0
? identifiers[0]
: undefined,
emailAddresses: Array.isArray(emailAddresses) && emailAddresses.length > 0
? emailAddresses[0]
: undefined,
};
🤖 Prompt for AI Agents
In packages/clerk-js/src/ui/localization/makeLocalizable.tsx at line 98, the
code accesses the first element of identifiers and emailAddresses without
checking if they are arrays or if they contain elements, which can cause silent
failures or errors. To fix this, add validation to confirm identifiers and
emailAddresses are arrays and have at least one element before accessing the
first item. Also, rename the variables to match the original field names for
clarity and consistency.

Copy link

pkg-pr-new bot commented Jun 20, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@6167

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@6167

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@6167

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@6167

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@6167

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@6167

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@6167

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@6167

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@6167

@clerk/express

npm i https://pkg.pr.new/@clerk/express@6167

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@6167

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@6167

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@6167

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@6167

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@6167

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@6167

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@6167

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@6167

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@6167

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@6167

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@6167

@clerk/types

npm i https://pkg.pr.new/@clerk/types@6167

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@6167

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@6167

commit: 61a42ae

@@ -89,15 +89,17 @@ export const useLocalizations = () => {
}

const { code, message, longMessage, meta } = (error || {}) as ClerkAPIError;
const { paramName = '' } = meta || {};
const { paramName = '', identifiers, emailAddresses } = meta || {};
Copy link
Member

@tmilewski tmilewski Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the example, not_allowed_access: '{{identifiers}} is not allowed to access this page!!!!!' - would this be confusing there are multiple identifiers (or emailsAddresses) returned.

@anagstef anagstef marked this pull request as draft June 20, 2025 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants