python-error-handling

Implements custom exceptions, exception chaining, structured logging, and retry patterns for Python applications.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill python-error-handling-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-error-handling
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/python/error-handling
Command: npx skills add https://github.com/asarchami/dotfiles --skill python-error-handling-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires structlog.

What problem does it solve? Python codebases often suffer from inconsistent error handling: bare except clauses, lost exception context, unmanaged resources, and unstructured logs that make production failures hard to diagnose. This Skill provides idiomatic patterns for building a coherent error handling strategy. ## Core Features & Use Cases - Custom Exception Hierarchies: Define a package-level base exception with derived types like ValidationError, ConfigurationError, and APIError for precise error classification. - Exception Chaining & Context Managers: Preserve original tracebacks with raise X from e, suppress noise with from None, and guarantee resource cleanup via @contextmanager decorators. - Structured Logging & Resilience Patterns: Configure structlog or stdlib logging with contextual fields, implement retry-with-backoff decorators for transient failures, and use a Result type (Ok/Err) for explicit error returns. - Use Case: When building a payment processing service, use this Skill to wrap external API failures in a custom APIError with chained context, log failures with structured order metadata, and retry transient errors with exponential backoff. ## Quick Start Review my Python service's error handling and refactor it to use a custom exception hierarchy with chained exceptions, structured logging, and retry logic for transient failures.

Frequently Asked Questions about python-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create custom exceptions in Python?

Define a base exception class inheriting from Exception for your package, then derive specific exceptions like ValidationError or APIError from it. This lets callers catch all package errors with one except clause or handle specific cases individually.

How to preserve the original traceback when re-raising exceptions in Python?

Use exception chaining with `raise NewError(...) from e` inside the except block to keep the original exception as the cause. Use `raise NewError(...) from None` when the original context is irrelevant noise, such as converting a ValueError into a domain validation error.

structlog vs standard logging in Python, which should I use?

structlog produces structured, key-value log events ideal for machine parsing and production observability, while stdlib logging with basicConfig is simpler for small scripts. This Skill shows both: structlog for contextual fields like order_id, and logging.basicConfig for quick setup.

How do I retry failed operations with backoff in Python?

Wrap the function in a retry decorator that catches transient exceptions, sleeps with exponential backoff (delay multiplied by 2 to the attempt power), and re-raises the last error after exhausting attempts. Only retry errors classified as transient.

When should I use a Result type instead of raising exceptions in Python?

Use a Result type (Ok/Err dataclasses) when errors are expected control flow, such as validation pipelines, making failure paths explicit in type signatures. Reserve exceptions for truly exceptional conditions like infrastructure failures.

Why should sensitive data be excluded from Python exception logs?

Logging passwords, tokens, or credentials in error messages creates security leaks since logs are often aggregated and widely accessible. The Skill's checklist requires scrubbing sensitive fields and logging only safe structured context like identifiers and error types.