Error Handling

Error Handling

Error Handling

Graceful Error Handling in UI Now

Building a seamless and professional SaaS application means ensuring that unexpected errors don’t disrupt your users' experience. UI Now takes care of this with high-level components that manage errors gracefully and efficiently.

Tutorial

1. JavaScript Errors

UI Now includes a robust error boundary at the application level using the /app/error.js component. This ensures that runtime JavaScript errors are caught and handled without crashing the entire application.

How It Works:

  • The /app/error.js component wraps your application, catching JavaScript errors that occur during rendering, lifecycle methods, or user interactions.

  • When an error occurs, the app:

    • Displays a user-friendly error message.

    • Logs the error for debugging purposes.

Benefits:

  • User-Friendly Experience: Instead of showing a broken interface, users see a clear message that something went wrong.

  • Developer Productivity: Errors are logged for quick debugging, minimizing downtime.

2. 404 Errors (Page Not Found)

When users navigate to a non-existent route, UI Now uses the /app/not-found.js component to handle 404 errors gracefully.

How It Works:

  • When a route is not found, the /app/not-found.js component automatically renders.

  • You can customize this component to include:

    • A friendly message explaining the missing page.

    • Links to guide users back to the homepage or other relevant sections.

Benefits:

  • Improved Navigation: Users are gently redirected to useful areas of your app instead of being left frustrated.

  • Custom Branding: Tailor the 404 page to match your app’s style and voice.


Example: /app/error.js

"use client";
export default function Error({ error, reset }) {
  return (
    <div className="flex flex-col items-center justify-center min-h-screen space-y-4">
      <h1 className="text-3xl font-bold">Something went wrong</h1>
      <p className="text-base-content/80">{error.message}</p>
      <button
        className="btn btn-primary"
        onClick={() => reset()}
      >
        Try Again
      </button>
    </div>
  );
}

What It Does:

  • Displays a friendly error message.

  • Provides a “Try Again” button to reset the error boundary and reattempt the action.


Example: /app/not-found.js

export default function NotFound(){ 
  return ( 
    <div className="flex flex-col items-center justify-center min-h-screen space-y-4"> 
      <h1 className="text-3xl font-bold">Page Not Found</h1> 
      <p className="text-base-content/80"> Oops! The page you’re looking for doesn’t exist. </p> 
      <a href="/" className="btn btn-primary"> Go to Homepage </a> 
    </div> 
  ); 
}

What It Does:

  • Displays a helpful message for missing pages.

  • Guides users back to the homepage or other key areas.

Why Use UI Now’s Error Handling?

  1. Better User Experience: Prevent broken interfaces or confusing errors from frustrating users.

  2. Customizable Components: Tailor error messages to reflect your app’s tone and branding.

  3. Seamless Recovery: Users can retry or navigate to relevant sections without friction.

  4. Efficient Debugging: Errors are contained and logged for quick resolution by developers.

By leveraging UI Now’s high-level error handling components, you can provide a polished, resilient, and user-friendly experience, even when things go wrong.