Skip to content

Conversation

@omimouni
Copy link
Contributor

@omimouni omimouni commented Jan 31, 2025

  • Use React Router’s useRouteError to get error details.
  • Create an ErrorPage component in pages/public/error.tsx.
  • Show useful error messages based on status codes. Add a button to help users go back or retry.

Summary by CodeRabbit

  • New Features

    • Added a custom error page for improved error handling in the application.
    • Implemented a user-friendly error display with navigation back to the home page.
  • Improvements

    • Enhanced routing error management with a dedicated error component.

- Use React Router’s useRouteError to get error details.
- Create an ErrorPage component in pages/public/error.tsx.
- Show useful error messages based on status codes.
Add a button to help users go back or retry.
@coderabbitai
Copy link

coderabbitai bot commented Jan 31, 2025

📝 Walkthrough

Walkthrough

The pull request introduces a new ErrorPage component in the client application to improve error handling during routing. By leveraging React Router's useRouteError hook, the component provides a user-friendly error interface that displays error details, status information, and a navigation button to return to the home page. The changes enhance the application's error presentation and user experience by implementing a custom error handling mechanism.

Changes

File Change Summary
apps/client/src/pages/public/error.tsx Added new ErrorPage component with error handling and routing logic
apps/client/src/router/index.tsx Imported ErrorPage and added errorElement prop to main route

Sequence Diagram

sequenceDiagram
    participant User
    participant Router
    participant ErrorPage
    
    User->>Router: Trigger route error
    Router->>ErrorPage: Render error component
    ErrorPage->>ErrorPage: Retrieve error details
    ErrorPage-->>User: Display error message
    User->>ErrorPage: Click home button
    ErrorPage->>Router: Navigate to home page
Loading

Possibly related issues

Poem

🐰 In the realm of routes, where errors might roam,
A rabbit's code creates a friendly home.
With messages clear and a path back in sight,
Our error page now shines so bright!
Hop back to safety, no need to despair. 🌈


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 docstrings to generate docstrings for this PR. (Beta)
  • @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

@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 (3)
apps/client/src/pages/public/error.tsx (3)

1-9: LGTM! Consider running autofix for import sorting.

The RouterError type definition is well-structured and includes all necessary fields for handling route errors.

Run ESLint autofix to sort the imports:

-import { useRouteError } from "react-router";
-import { Button } from "@reactive-resume/ui";
+import { Button } from "@reactive-resume/ui";
+import { useRouteError } from "react-router";
🧰 Tools
🪛 ESLint

[error] 1-2: Run autofix to sort these imports!

(simple-import-sort/imports)


11-13: Consider safer error type handling.

The type assertion as RouterError could fail if the error doesn't match the expected shape. Consider adding type guards for safer error handling.

-    const error = useRouteError() as RouterError;
+    const routeError = useRouteError();
+    const error: RouterError = {
+        status: 'status' in routeError ? (routeError.status as number) : 500,
+        statusText: 'statusText' in routeError ? (routeError.statusText as string) : undefined,
+        data: 'data' in routeError ? String(routeError.data) : 'Unknown error',
+        message: 'message' in routeError ? String(routeError.message) : undefined,
+    };

28-37: Enhance navigation options and add internationalization.

Consider adding a "Go back" option and internationalizing the button text.

+import { useNavigate } from "react-router";
+
 export const ErrorPage = () => {
+    const navigate = useNavigate();
+    const handleGoBack = () => navigate(-1);
+
     // ... rest of the component
     <div className="pt-2">
+        <div className="space-x-4">
+            <Button onClick={handleGoBack}>
+                <Trans>Go back</Trans>
+            </Button>
             <a
                 href={process.env.PUBLIC_URL}
                 className="inline-block"
             >
                 <Button>
-                    Go to home
+                    <Trans>Go to home</Trans>
                 </Button>
             </a>
+        </div>
     </div>
🧰 Tools
🪛 ESLint

[error] 33-35: String not marked for translation. Wrap it with .

(lingui/no-unlocalized-strings)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 92995d9 and f42b29e.

📒 Files selected for processing (2)
  • apps/client/src/pages/public/error.tsx (1 hunks)
  • apps/client/src/router/index.tsx (1 hunks)
🧰 Additional context used
🪛 ESLint
apps/client/src/pages/public/error.tsx

[error] 1-2: Run autofix to sort these imports!

(simple-import-sort/imports)


[error] 18-19: String not marked for translation. Wrap it with .

(lingui/no-unlocalized-strings)


[error] 33-35: String not marked for translation. Wrap it with .

(lingui/no-unlocalized-strings)

🔇 Additional comments (1)
apps/client/src/router/index.tsx (1)

23-23: LGTM! Error boundary is correctly implemented.

The ErrorPage is properly integrated as a global error boundary at the root level, which will catch and handle all routing errors in the application.

Also applies to: 26-26

🧰 Tools
🪛 ESLint

[error] 1-23: Run autofix to sort these imports!

(simple-import-sort/imports)

Comment on lines 14 to 26
return (
<main className="flex items-center justify-center min-h-screen p-4">
<div className="w-full max-w-sm space-y-6">
<h4 className="flex flex-col text-4xl font-bold text-white">
<span>
Error {error.status}
</span>
{error.statusText && <span className="text-base font-normal">{error.statusText}</span>}
</h4>

<p className="text-sm text-gray-500 break-words">
{error.data || error.message}
</p>
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add internationalization support and specific error messages.

The error display should support internationalization and provide specific messages for different HTTP status codes.

+import { Trans } from "@lingui/macro";
+
+const getErrorMessage = (status: number) => {
+  switch (status) {
+    case 404:
+      return "The page you're looking for doesn't exist.";
+    case 403:
+      return "You don't have permission to access this page.";
+    case 500:
+      return "An internal server error occurred.";
+    default:
+      return "An unexpected error occurred.";
+  }
+};

 <h4 className="flex flex-col text-4xl font-bold text-white">
   <span>
-    Error {error.status}
+    <Trans>Error {error.status}</Trans>
   </span>
-  {error.statusText && <span className="text-base font-normal">{error.statusText}</span>}
+  {error.statusText && <span className="text-base font-normal"><Trans>{error.statusText}</Trans></span>}
 </h4>

 <p className="text-sm text-gray-500 break-words">
-  {error.data || error.message}
+  <Trans>{error.data || error.message || getErrorMessage(error.status)}</Trans>
 </p>
📝 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
return (
<main className="flex items-center justify-center min-h-screen p-4">
<div className="w-full max-w-sm space-y-6">
<h4 className="flex flex-col text-4xl font-bold text-white">
<span>
Error {error.status}
</span>
{error.statusText && <span className="text-base font-normal">{error.statusText}</span>}
</h4>
<p className="text-sm text-gray-500 break-words">
{error.data || error.message}
</p>
return (
<main className="flex items-center justify-center min-h-screen p-4">
<div className="w-full max-w-sm space-y-6">
<h4 className="flex flex-col text-4xl font-bold text-white">
<span>
<Trans>Error {error.status}</Trans>
</span>
{error.statusText && <span className="text-base font-normal"><Trans>{error.statusText}</Trans></span>}
</h4>
<p className="text-sm text-gray-500 break-words">
<Trans>{error.data || error.message || getErrorMessage(error.status)}</Trans>
</p>
🧰 Tools
🪛 ESLint

[error] 18-19: String not marked for translation. Wrap it with .

(lingui/no-unlocalized-strings)

@omimouni omimouni closed this Jan 31, 2025
@omimouni omimouni reopened this Jan 31, 2025
…ustom messages

- Add LocaleProvider wrapper for i18n support
- Replace a tag with React Router Link components
- Add custom error messages for different HTTP status codes
- Implement proper translation support using Trans and t components
Copy link

@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: 2

♻️ Duplicate comments (1)
apps/client/src/pages/public/error.tsx (1)

14-29: ⚠️ Potential issue

Add internationalization support and fix switch case style.

The error messages need to be internationalized, and the switch cases should follow a consistent style with braces.

 const getErrorMessage = (status: number) => {
     switch (status) {
-        case 404:
-            return "The page you're looking for doesn't exist.";
-        case 403:
-            return "You don't have permission to access this page.";
-        case 500:
-            return "An internal server error occurred.";
-        case 401:
-            return "You are not authorized to access this page.";
-        case 400:
-            return "The request was invalid.";
-        default:
-            return "An unexpected error occurred.";
+        case 404: {
+            return t`The page you're looking for doesn't exist.`;
+        }
+        case 403: {
+            return t`You don't have permission to access this page.`;
+        }
+        case 500: {
+            return t`An internal server error occurred.`;
+        }
+        case 401: {
+            return t`You are not authorized to access this page.`;
+        }
+        case 400: {
+            return t`The request was invalid.`;
+        }
+        default: {
+            return t`An unexpected error occurred.`;
+        }
     }
 };
🧰 Tools
🪛 ESLint

[error] 16-16: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 17-17: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 18-18: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 19-19: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 20-20: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 21-21: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 22-22: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 23-23: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 24-24: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 25-25: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 26-26: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 27-27: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)

🧹 Nitpick comments (2)
apps/client/src/pages/public/error.tsx (2)

7-12: Consider making the data property optional in RouterError type.

The data property might not always be present in the error object, making it safer to mark it as optional.

 type RouterError = {
     status: number;
     statusText?: string;
-    data: string;
+    data?: string;
     message?: string;
 } & Error;

46-46: Remove commented code.

Remove the commented-out code as it's no longer needed and can cause confusion.

-                    {/* {error.data || error.message} */}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f42b29e and e438602.

📒 Files selected for processing (1)
  • apps/client/src/pages/public/error.tsx (1 hunks)
🧰 Additional context used
🪛 ESLint
apps/client/src/pages/public/error.tsx

[error] 2-5: Run autofix to sort these imports!

(simple-import-sort/imports)


[error] 16-16: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 17-17: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 18-18: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 19-19: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 20-20: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 21-21: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 22-22: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 23-23: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 24-24: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 25-25: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 26-26: Missing braces in case clause.

(unicorn/switch-case-braces)


[error] 27-27: String not marked for translation. Wrap it with t, <Trans>, or msg.

(lingui/no-unlocalized-strings)


[error] 40-40: Should be ${variable}, not ${object.property} or ${myFunction()}

(lingui/no-expression-in-message)


[error] 47-47: You couldn't translate just a variable, remove Trans or add some text inside

(lingui/no-single-variables-to-translate)


[error] 47-47: should not wrap a single element unnecessarily

(lingui/no-single-tag-to-translate)


[error] 47-47: Should be ${variable}, not ${object.property} or ${myFunction()}

(lingui/no-expression-in-message)

Repository owner deleted a comment from coderabbitai bot Jan 31, 2025
Repository owner deleted a comment from coderabbitai bot Jan 31, 2025
@AmruthPillai
Copy link
Owner

AmruthPillai commented Jan 31, 2025

@omimouni Thank you so much for your work on this PR! Do you perhaps have a screenshot of the error page so I can check it out before we merge this to main?

@omimouni
Copy link
Contributor Author

Hi @AmruthPillai, here are screenshot of the error page on both desktop & mobile

image image

@AmruthPillai
Copy link
Owner

Looks good, thank you for the quick response!

@AmruthPillai AmruthPillai merged commit 3d5b3db into AmruthPillai:main Jan 31, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants