- 2.1: packs/ -> archive/packs/ - 2.2: site/ -> archive/site/ - 2.3: src/lab/ -> archive/lab/ - 2.4: examples/ -> archive/examples-legacy/ (SSI-based)
82 lines
1.9 KiB
Markdown
82 lines
1.9 KiB
Markdown
# ASW Flask/FastAPI Error Pack
|
|
|
|
Drop-in styled error responses for Flask and FastAPI applications.
|
|
|
|
## Usage
|
|
|
|
```python
|
|
# Copy asw_errors.py to your project directory, then:
|
|
|
|
from flask import Flask
|
|
from asw_errors import register_asw_errors
|
|
|
|
app = Flask(__name__)
|
|
register_asw_errors(app)
|
|
```
|
|
|
|
```python
|
|
from fastapi import FastAPI
|
|
from asw_errors import register_asw_errors
|
|
|
|
app = FastAPI()
|
|
register_asw_errors(app)
|
|
```
|
|
|
|
## What you get
|
|
|
|
ASW-styled HTML error pages for all HTTP error codes:
|
|
|
|
| Code | Status |
|
|
|------|--------|
|
|
| 400 | Bad Request |
|
|
| 401 | Unauthorized |
|
|
| 403 | Forbidden |
|
|
| 404 | Not Found |
|
|
| 405 | Method Not Allowed |
|
|
| 408 | Request Timeout |
|
|
| 409 | Conflict |
|
|
| 410 | Gone |
|
|
| 415 | Unsupported Media Type |
|
|
| 422 | Unprocessable Entity |
|
|
| 429 | Too Many Requests |
|
|
| 500 | Internal Server Error |
|
|
| 502 | Bad Gateway |
|
|
| 503 | Service Unavailable |
|
|
| 504 | Gateway Timeout |
|
|
|
|
For FastAPI, `RequestValidationError` (422) is also caught separately.
|
|
|
|
## Linking asw.css
|
|
|
|
By default, styles are inlined — no external dependencies needed. If your app already serves `asw.css`, link it instead:
|
|
|
|
```python
|
|
register_asw_errors(app, css_url="/static/asw.css")
|
|
```
|
|
|
|
The pages will then use your full theme including custom fonts.
|
|
|
|
## Custom app name
|
|
|
|
The nav bar shows your app's name. Override it:
|
|
|
|
```python
|
|
register_asw_errors(app, app_name="My Service")
|
|
```
|
|
|
|
Flask auto-detects from `app.name`; FastAPI from `app.title`. Both fall back to sensible defaults.
|
|
|
|
## Standalone HTML generation
|
|
|
|
The `error_html()` function is also exported if you need to generate pages directly:
|
|
|
|
```python
|
|
from asw_errors import error_html
|
|
|
|
html = error_html(404, path="/missing/route")
|
|
html = error_html(500, css_url="/static/asw.css", app_name="My API")
|
|
```
|
|
|
|
## No dependencies at module level
|
|
|
|
Flask and FastAPI are imported lazily inside `register_asw_errors()` — so this file can live in a project that uses either framework without both installed.
|