Write responsive web apps in full python
—
Exception and error handling classes for view lifecycle management, HTTP errors, and custom error responses with built-in error view support.
Exceptions for controlling view execution flow and lifecycle.
class StopReason(Exception):
"""Base exception for stopping view execution."""
class UserAbort(StopReason):
"""Raised when user aborts view execution (e.g., navigates away)."""
class ServerStop(StopReason):
"""Raised when server stops view execution."""Exception classes for handling HTTP error responses.
class ForbiddenError(Exception):
"""Raised for 403 Forbidden errors."""
class NotFoundError(Exception):
"""Raised for 404 Not Found errors."""
class ClientError(Exception):
"""Base class for general client errors."""Decorators for registering custom error handling views.
# Available on App class
def error_403_view(self):
"""Decorator for 403 error views."""
def error_404_view(self):
"""Decorator for 404 error views."""
def error_500_view(self):
"""Decorator for 500 error views."""from lona import App, View
from lona.exceptions import UserAbort, ServerStop
from lona.errors import NotFoundError, ForbiddenError
from lona.html import HTML, H1, P
app = App(__file__)
@app.route('/protected')
class ProtectedView(View):
def handle_request(self, request):
if not request.user:
raise ForbiddenError('Login required')
try:
# Interactive view that might be aborted
button = Button('Click me')
self.show(HTML(H1('Protected Area'), button))
self.await_click(button)
except UserAbort:
# User navigated away - cleanup if needed
self.cleanup_resources()
return
except ServerStop:
# Server is shutting down
return
# Custom error views
@app.error_404_view()
class NotFoundView(View):
def handle_request(self, request):
return HTML(
H1('Page Not Found'),
P('The requested page could not be found.')
)
@app.error_403_view()
class ForbiddenView(View):
def handle_request(self, request):
return HTML(
H1('Access Denied'),
P('You do not have permission to access this resource.')
)from typing import Optional, Union
ErrorMessage = str
HTTPStatus = int
ErrorContext = Optional[dict]Install with Tessl CLI
npx tessl i tessl/pypi-lona