Skip to main content

Structured error handling framework for Python

Project description

PyErrorSchema

PyErrorSchema is a structured error handling framework for Python that helps developers create consistent, schema-based error messages across applications.

Installation

The source code is currently hosted on GitHub at PyErrorSchema.

Binary installers for the latest released version are available at the Python Package Index (PyPI).

pip install pyerrorschema

Key Features

  • Structured Error Schemas: Define consistent error formats across your application
  • Pre-defined Error Types: Includes common error types like database errors, file errors, validation errors
  • Framework Integrations: Specialized support for FastAPI with location tracking
  • Exception Mapping: Automatically map Python exceptions to appropriate error schemas
  • Hierarchical Error Organization: Group related errors with specialized subclasses
  • Customizable Error Messages: Format user-friendly error messages with context information
  • Frontend/Backend Message Separation: Support for different messages for end-users vs developers (in FastAPI version)

Basic Usage

from pyerrorschema import ErrorSchema

# Create a basic error
error = ErrorSchema.database_error(
    msg="Failed to connect to the database"
)

# Convert to dictionary for API responses
error_dict = error.to_dict()
print(error_dict)
# Output: {'type': 'database_error', 'msg': 'Database error: failed to connect to the database'}

FastAPI Integration

from pyerrorschema import FastAPIErrorSchema

# Create a FastAPI-specific error
error = FastAPIErrorSchema.validation_error(
    msg="Invalid input data",
    ui_msg="Please check your input and try again",  # User-friendly message
    loc=["body", "user", "email"],  # Location of the error
    input={"email": "invalid-email"}  # Input that caused the error
)

# Convert to dictionary for API responses
print(error.to_dict())
# Output: {'type': 'validation_error', 'msg': 'Invalid input data', 'loc': ['body', 'user', 'email'], 'input': {'email': 'invalid-email'}}

Working with File Errors

from pyerrorschema import ErrorSchema

# Use specialized File error class
file_error = ErrorSchema.File.not_found(path="/path/to/config.json")
print(file_error.msg)
# Output: "File 'config.json' not found."

# Automatically detects directories
dir_error = ErrorSchema.File.not_found(path="/path/to/data/")
print(dir_error.msg)
# Output: "Directory 'data/' not found."

Exception Mapping

from pyerrorschema import ErrorSchema

try:
    # Some code that might raise an exception
    with open("nonexistent_file.txt", "r") as f:
        content = f.read()
except Exception as e:
    # Map the exception to an error schema
    error = ErrorSchema.from_exception(e)
    print(error.to_dict())
    # Output: {'type': 'file_error', 'msg': 'File error: [Errno 2] No such file or directory: 'nonexistent_file.txt''}

Error Groups

Sometimes you need to collect multiple errors into a single group for easier management or to send them all in a single API response. You can use ErrGroup or its FastAPI-specific variant FastAPIErrGroup to aggregate error schemas. These groups work very similarly to Python lists, offering convenient methods to append, extend, or clear errors.

For example, you can create a group of FastAPI error schemas as follows:

from pyerrorschema import FastAPIErrorSchema, FastAPIErrGroup

err_group = FastAPIErrGroup()
err_group.append(FastAPIErrorSchema.File.not_found(path="test.text"))
err_group.append(FastAPIErrorSchema.DB.no_results("querying the database"))

print(err_group)

Which will output:

FastAPIErrGroup(
    FastAPIErrorSchema(
        type='file_error',
        msg="File error: file 'test.text' not found.",
        ui_msg="File 'test.text' not found.",
        loc=[],
        input={}
    ),
    FastAPIErrorSchema(
        type='database_error',
        msg='Database error: no results found while querying the database.',
        ui_msg='No results found while querying the database.',
        loc=[],
        input={}
    )
)

The usage of groups is similar to that of a list:

  • .append: Append a single error schema instance to the group.
  • .extend: Append a list of error schema instances into the group.
  • .clear: Remove all error schemas from the group.
  • Index Access and Assignment: Access or update individual errors by index.

Available Error Types

PyErrorSchema provides a range of built-in error types:

  • database_error: For database operation failures
  • file_error: For file I/O issues
  • runtime_error: For general runtime problems
  • timeout_error: For timeouts
  • parse_error: For parsing failures
  • value_error: For invalid values
  • validation_error: For input validation issues (FastAPI)
  • docker_error: For Docker-related issues (FastAPI)

Each error type has specialized factory methods that create appropriate error messages with context.

Why Use PyErrorSchema?

  • Consistency: Ensure a consistent error format across your application
  • Maintainability: Centralize error handling logic
  • Flexibility: Adapt to different frameworks and use cases
  • User Experience: Create better error messages for your users
  • Developer Experience: Make debugging easier with structured error information

License

This project is licensed under the BSD License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyerrorschema-2.1.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyerrorschema-2.1.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file pyerrorschema-2.1.0.tar.gz.

File metadata

  • Download URL: pyerrorschema-2.1.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for pyerrorschema-2.1.0.tar.gz
Algorithm Hash digest
SHA256 871c91ebca5e9ecc7092314619528e702b38e988f1621909891ef0318ca707a4
MD5 8493d6854918faa3f6507a18bc3b8b06
BLAKE2b-256 4ddf22dadd3bc3eff051b7fea139c2ce260a27075c8eddc3a5cd8305fd149568

See more details on using hashes here.

File details

Details for the file pyerrorschema-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyerrorschema-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for pyerrorschema-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ab3b9e7ef1eaa9cc4e24b9d4da03cb5e62fa6751c549cac9866630121485ed5
MD5 e5030d0dd3dbd2316e64e24edf49734c
BLAKE2b-256 eb54b5ff8169f87e915e11472551ced59553ffc41d9f382f44abd31255d45fdf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page