Skip to content

Conversation

@sumaiazaman
Copy link
Contributor

Summary

This PR introduces consistent error handling patterns across Laravel components through a new standardized trait and updates to key framework classes.

Changes Made

New Error Handling Foundation

  • HandlesExceptions trait: Comprehensive error handling utility providing:
    • Consistent exception reporting with rich context
    • Fallback mechanisms when exception handlers fail
    • Configurable logging levels based on exception types
    • Standardized context building for better debugging

Updated Components

Queue System (SyncQueue)

  • Integrated standardized error handling
  • Added job-specific context to exception reports:
    • Job ID, name, queue, and attempt count
    • Enhanced debugging information for failed jobs

Database Layer (Connection)

  • Implemented consistent query exception reporting
  • Added database-specific context:
    • Connection name and transaction level
    • Query SQL and parameter bindings
    • Better error tracking for database issues

Key Features

Consistent Error Reporting

  • Unified approach to exception handling across components
  • Rich contextual information for all exceptions
  • Automatic fallback to direct logging when handlers fail

Enhanced Debugging

  • Standardized context building with relevant metadata
  • Exception-specific information preservation
  • Improved error tracking and monitoring capabilities

Flexible Configuration

  • Configurable logging levels per exception type
  • Support for custom exception handlers and loggers
  • Backward-compatible integration

Robust Fallback Mechanisms

  • Graceful degradation when exception handlers fail
  • Direct logging as backup reporting method
  • Prevents error handling failures from breaking applications

Benefits

  • Improved Debugging: Consistent, rich context across all components
  • Better Monitoring: Standardized error reporting for observability tools
  • Reduced Duplication: Centralized error handling logic
  • Enhanced Maintainability: Consistent patterns across the framework
  • Better Developer Experience: Clear, informative error messages

Backward Compatibility

All changes are fully backward compatible:

  • Existing exception handling continues to work unchanged
  • New trait provides additional functionality without breaking changes
  • Framework behavior remains consistent for end users

Testing

  • All existing tests pass
  • Error handling provides consistent behavior
  • Fallback mechanisms work correctly
  • Context information is properly captured

This enhancement establishes a foundation for consistent error handling patterns throughout the Laravel framework while maintaining full backward compatibility.

- Create HandlesExceptions trait for consistent error reporting
- Add standardized exception context building
- Implement consistent logging levels based on exception types
- Update SyncQueue to use standardized error handling with job context
- Update Database Connection to use standardized query exception reporting

Key improvements:
- Consistent exception reporting across all components
- Rich context information for better debugging
- Fallback mechanisms when exception handlers fail
- Configurable logging levels per exception type
- Better separation of concerns for error handling

Benefits:
- Improved debugging with consistent context
- Better error tracking and monitoring
- Reduced code duplication in error handling
- More maintainable exception management
- Enhanced observability across the framework

All changes maintain backward compatibility while providing
a foundation for consistent error handling patterns.
@sumaiazaman sumaiazaman changed the title Standardize error handling patterns across components [12.x] Standardize error handling patterns across components Nov 11, 2025
*
* @param \Throwable $exception
* @param mixed $default
* @param array $context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you want to narrow this down:

Suggested change
* @param array $context
* @param array<string, mixed> $context

or perhaps, if you want it to be flat

Suggested change
* @param array $context
* @param array<string, scalar> $context

Comment on lines +154 to +161
$levelMap = [
'InvalidArgumentException' => 'warning',
'LogicException' => 'error',
'RuntimeException' => 'error',
'BadMethodCallException' => 'error',
'OutOfBoundsException' => 'warning',
'UnexpectedValueException' => 'warning',
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't $levelMap be a good candidate for a class property?

Comment on lines +155 to +160
'InvalidArgumentException' => 'warning',
'LogicException' => 'error',
'RuntimeException' => 'error',
'BadMethodCallException' => 'error',
'OutOfBoundsException' => 'warning',
'UnexpectedValueException' => 'warning',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be state of the art to make the level an enum?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants