Error Page
The ErrorPage component provides beautiful, full-page error displays for common HTTP errors (404, 500, 403) and custom error scenarios. It returns a tuple of (Title, Content) ready for FastHTML route rendering, with sensible defaults and full customization support.
Goal
By the end of this guide, you'll be able to create professional error pages that handle both standard HTTP errors and backend exceptions, with zero HTML/CSS knowledge required.
Quick Start
Here's the simplest way to create a 404 error page.
Visual Examples & Use Cases
1. Standard HTTP Errors
ErrorPage comes with beautiful defaults for the most common HTTP errors.
Page Not Found
The page you're looking for doesn't exist or has been moved.
Server Error
Something went wrong on our end. We're working to fix it.
Access Denied
You don't have permission to access this resource.
2. Backend Error Messages
The killer feature: pass backend exceptions directly to the error page.
Server Error
Database connection timeout after 30 seconds
Real-World Example:
@app.post("/profile/save")
def save_profile(req):
try:
user = req.session.get("user")
if not user:
return ErrorPage(403, message="Please log in to continue")
# ... save logic ...
db.commit()
return hx_redirect("/profile")
except DatabaseError as e:
return ErrorPage(500, message=f"Database error: {e}")
except ValidationError as e:
return ErrorPage(400, message=f"Invalid data: {e}")
3. Custom Error Pages
Create branded error pages for your app.
Practical Functionality
Hiding the Error Code
Sometimes you don't want to show the big error number.
Custom Action Buttons
Change where the button goes or what it says.
# Go to dashboard instead of home
ErrorPage(
404,
action_text="Back to Dashboard",
action_href="/dashboard"
)
# Hide the button entirely
ErrorPage(500, action_text=None)
Premium Feature Paywall
Use 403 errors creatively for feature gating.
@app.get("/premium-feature")
@require_auth()
def premium_feature(req):
user = req.session.get("user")
if not user.is_premium:
return ErrorPage(
403,
title="Premium Feature",
message="Upgrade your plan to access this feature",
action_text="View Plans",
action_href="/pricing"
)
return render_premium_feature()
Integration Patterns
With FastHTML Routes
ErrorPage returns a tuple for direct route rendering.
from fasthtml.common import *
from faststrap import ErrorPage
app = FastHTML()
@app.get("/404")
def not_found():
# Returns (Title(...), Div(...))
return ErrorPage(404)
@app.get("/500")
def server_error():
return ErrorPage(500)
With Exception Handlers
Create a global error handler.
@app.exception_handler(404)
def handle_404(req, exc):
return ErrorPage(404)
@app.exception_handler(500)
def handle_500(req, exc):
# Log the error
logger.error(f"Server error: {exc}")
# Show user-friendly message in production
if app.debug:
return ErrorPage(500, message=str(exc))
else:
return ErrorPage(500) # Use default message
With HTMX Requests
Detect HTMX requests and return inline errors instead.
@app.get("/data")
def get_data(req):
try:
data = fetch_data()
return render_data(data)
except Exception as e:
# Check if it's an HTMX request
if req.headers.get("HX-Request"):
# Return inline error dialog
from faststrap import ErrorDialog
return ErrorDialog(message=str(e))
else:
# Return full error page
return ErrorPage(500, message=str(e))
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
code |
int |
Required | HTTP error code (404, 500, 403, or custom) |
title |
str \| None |
Auto | Custom error title (uses default if None) |
message |
str \| None |
Auto | Custom error message (supports backend errors) |
icon |
str \| None |
Auto | Bootstrap icon name (e.g., "exclamation-triangle") |
action_text |
str \| None |
"Go Home" | Text for action button (None to hide) |
action_href |
str |
"/" | URL for action button |
show_code |
bool |
True |
Whether to display large error code |
**kwargs |
Any |
- | Additional HTML attributes for container |
Default Messages
| Code | Title | Message | Icon |
|---|---|---|---|
| 404 | Page Not Found | The page you're looking for doesn't exist... | exclamation-triangle |
| 500 | Server Error | Something went wrong on our end... | x-circle |
| 403 | Access Denied | You don't have permission... | shield-lock |
| Other | Error | An error occurred. | exclamation-circle |
Best Practices
✅ Do This
# Pass backend errors in development
if app.debug:
return ErrorPage(500, message=str(exception))
# Use semantic error codes
return ErrorPage(403) # Not authorized
return ErrorPage(404) # Not found
return ErrorPage(500) # Server error
# Customize action buttons
return ErrorPage(404, action_text="Search", action_href="/search")
❌ Don't Do This
# Don't expose sensitive errors in production
return ErrorPage(500, message=str(database_connection_string))
# Don't use wrong error codes
return ErrorPage(404) # When user lacks permission (use 403)
return ErrorPage(500) # For validation errors (use 400)
faststrap.components.feedback.error_page.ErrorPage(code, title=None, message=None, icon=None, action_text='Go Home', action_href='/', show_code=True, **kwargs)
Full-page error display for 404, 500, 403, and custom errors.
Renders a centered, styled error page with icon, title, message, and action button. Supports custom backend error messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
ErrorCodeType | int
|
HTTP error code (404, 500, 403, or custom) |
required |
title
|
str | None
|
Custom error title (uses default if None) |
None
|
message
|
str | None
|
Custom error message (uses default if None, supports backend errors) |
None
|
icon
|
str | None
|
Bootstrap icon name (uses default if None) |
None
|
action_text
|
str | None
|
Text for action button (None to hide button) |
'Go Home'
|
action_href
|
str
|
URL for action button |
'/'
|
show_code
|
bool
|
Whether to display the error code |
True
|
**kwargs
|
Any
|
Additional HTML attributes for the container |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[Any, ...]
|
Tuple of (Title, ErrorPage Div) for FastHTML page rendering |
Example
Default 404 page:
ErrorPage(404)
Custom 500 with backend error:
ErrorPage(500, message="Database connection timeout")
Custom 403 with custom action:
ErrorPage( ... 403, ... title="Premium Feature", ... message="Upgrade your plan to access this feature", ... action_text="View Plans", ... action_href="/pricing" ... )
Custom error code:
ErrorPage( ... 418, ... title="I'm a teapot", ... message="This server refuses to brew coffee", ... icon="cup-hot" ... )
Hide action button:
ErrorPage(500, action_text=None)
Note
Returns a tuple for use in FastHTML routes:
For backend errors, pass the error message directly:
Source code in src/faststrap/components/feedback/error_page.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |