- Home
- Skills
- Jiatastic
- Open Python Skills
- Error Handling
error-handling_skill
- Python
2
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill jiatastic/open-python-skills --skill error-handling- SKILL.md7.4 KB
Overview
This skill provides practical error handling patterns for Python backends using FastAPI, Pydantic, and asyncio. It embraces the “Let it crash” philosophy: raise specific domain exceptions where errors occur and convert them to safe HTTP responses at application boundaries. The guidance covers domain exceptions, global handlers, validation errors, async background failures, and preventing stack trace leakage.
How this skill works
Define semantic domain exceptions and a consistent error response schema with Pydantic. Let exceptions bubble up from services and routes, then register FastAPI exception handlers to transform exceptions into JSON responses and safe status codes. Wrap third-party SDK errors into domain errors, log full tracebacks in a single generic handler, and run background tasks inside safe wrappers to capture and report failures.
When to use it
- Designing API error responses and consistent error schemas
- Handling RequestValidationError and preventing validation leaks
- Managing exceptions in async code and background tasks
- Creating custom exception hierarchies for domain semantics
- Wrapping third-party SDK failures and implementing retries
Best practices
- Raise low, catch high: throw in domain code, handle at API boundaries
- Create domain-specific exceptions with error codes and status codes
- Register centralized exception handlers (@app.exception_handler) for DomainError, HTTPException, validation and generic errors
- Never use bare except; catch specific exceptions and preserve original context with raise ... from
- Log full tracebacks internally and return minimal, safe messages to clients
- Wrap background tasks with try/except to log and send failures to DLQ/alerts
Example use cases
- Return a 404 JSON error via UserNotFoundError instead of returning None
- Convert Pydantic RequestValidationError into a 422 structured response without leaking details
- Wrap httpx failures in ExternalServiceError and retry transient failures with tenacity
- Add a global Exception handler to ensure no stack traces reach clients
- Run email sending in a safe_background_task that logs failures and posts to a dead-letter queue
FAQ
Only when you need to retry, transform third-party errors into domain errors, clean up resources, or add context; otherwise let them propagate to handlers.
How do I prevent stack traces in responses?
Register a generic @app.exception_handler(Exception) that logs the trace internally and returns a minimal internal_error JSON with status 500.