pybiorythm

Error Handling

PyBiorythm uses a structured exception hierarchy for comprehensive error handling.

Author: Peter Rosemann (dkdndes@gmail.com)

Exception Hierarchy

BiorhythmError (base)
├── DateValidationError
├── ChartParameterError  
└── CalculationError

Exception Classes

BiorhythmError

Base exception for all biorhythm-related errors.

class BiorhythmError(Exception):
    """Base exception for all biorhythm errors."""
    pass

DateValidationError

Date validation errors for invalid dates or date ranges.

class DateValidationError(BiorhythmError):
    """Raised for invalid date inputs."""
    pass

Common scenarios:

ChartParameterError

Chart configuration errors for invalid parameters.

class ChartParameterError(BiorhythmError):
    """Raised for invalid chart parameters."""
    pass

Common scenarios:

CalculationError

Calculation errors during biorhythm computation.

class CalculationError(BiorhythmError):
    """Raised for calculation failures."""
    pass

Error Handling Examples

Date Validation

from biorythm import BiorhythmCalculator, DateValidationError
from datetime import datetime

try:
    calc = BiorhythmCalculator()
    # Future date will raise error
    future_date = datetime(2030, 1, 1)
    result = calc.calculate_biorhythm_values(future_date, datetime.now())
except DateValidationError as e:
    print(f"Date error: {e}")
    # Handle invalid date

Chart Parameters

from biorythm import BiorhythmCalculator, ChartParameterError

try:
    # Width too small
    calc = BiorhythmCalculator(width=5)
except ChartParameterError as e:
    print(f"Parameter error: {e}")
    # Use default parameters
    calc = BiorhythmCalculator()

Generic Error Handling

from biorythm import BiorhythmCalculator, BiorhythmError

try:
    calc = BiorhythmCalculator()
    # ... operations
except BiorhythmError as e:
    print(f"Biorhythm error: {e}")
    # Handle any biorhythm-related error
except Exception as e:
    print(f"Unexpected error: {e}")
    # Handle other errors

Best Practices

1. Specific Exception Handling

# Good - specific handling
try:
    calc.generate_chart(birthdate)
except DateValidationError:
    # Handle date errors specifically
    show_date_input_form()
except ChartParameterError:
    # Handle parameter errors
    use_default_parameters()

2. Error Logging

import logging

logger = logging.getLogger(__name__)

try:
    calc.generate_chart(birthdate)
except BiorhythmError as e:
    logger.error(f"Biorhythm calculation failed: {e}")
    raise

3. User-Friendly Messages

try:
    calc = BiorhythmCalculator(width=user_width)
except ChartParameterError as e:
    # Convert technical error to user message
    print(f"Chart width must be at least 12 characters. Please try again.")

Error Messages

DateValidationError Messages

ChartParameterError Messages

CalculationError Messages


Related: Core API Calculator API JSON Schema