najibismail/laravel-exception-catcher

A powerful Laravel package that automatically catches exceptions and sends beautifully formatted, responsive email notifications to multiple recipients

v1.0.7 2025-08-19 05:44 UTC

This package is auto-updated.

Last update: 2025-08-19 05:47:04 UTC


README

Latest Version on Packagist Total Downloads License PHP Version

A powerful Laravel package that automatically catches exceptions and sends beautifully formatted, responsive email notifications to multiple recipients.

๐Ÿš€ Features

  • ๐Ÿšจ Automatic Exception Catching - Seamlessly integrates with Laravel's exception handler
  • ๐Ÿ“ง Multiple Recipients - Send alerts to multiple email addresses
  • ๐Ÿ“ฑ Responsive Email Design - Beautiful emails that work on all devices and email clients
  • โšก Rate Limiting - Prevents email spam with intelligent throttling (enabled by default)
  • ๐Ÿ”ง Configurable Filtering - Choose which exception types to report/skip
  • ๐Ÿ“Š Rich Information - Includes request data, stack trace, user info, and environment details
  • โš™๏ธ Queue Support - Async email sending for better performance
  • ๐Ÿงช Built-in Testing - Command line and web testing tools included
  • ๐ŸŽจ Stack Trace Enhancement - Color-coded, row-by-row stack traces with source type indicators

๐Ÿ“ฆ Installation

composer require najibismail/laravel-exception-catcher

The service provider will be automatically registered (Laravel 5.5+).

โšก Quick Start

  1. Publish configuration:

    php artisan vendor:publish --provider="NajibIsmail\LaravelExceptionCatcher\ExceptionCatcherServiceProvider" --tag="config"
  2. Configure emails in .env:

    EXCEPTION_CATCHER_TO_EMAIL="admin@yourapp.com"
    EXCEPTION_CATCHER_FROM_EMAIL="noreply@yourapp.com"
  3. Add to your Exception Handler (app/Exceptions/Handler.php):

    use NajibIsmail\LaravelExceptionCatcher\Traits\SendsExceptionEmails;
    
    class Handler extends ExceptionHandler
    {
        use SendsExceptionEmails;
        
        // The trait automatically handles exception reporting
        // No need to override the report() method
    }
  4. Test it:

    php artisan exception:test

โš™๏ธ Configuration

Edit config/exception-catcher.php to customize behavior:

return [
    'enabled' => env('EXCEPTION_CATCHER_ENABLED', true),
    
    'emails' => [
        'to' => explode(',', env('EXCEPTION_CATCHER_TO_EMAIL', 'admin@example.com')),
        'from' => env('EXCEPTION_CATCHER_FROM_EMAIL', 'noreply@example.com'),
        'from_name' => env('EXCEPTION_CATCHER_FROM_NAME', 'Exception Catcher'),
    ],
    
    'queue_enabled' => env('EXCEPTION_CATCHER_QUEUE_ENABLED', false),
    'include_stack_trace' => env('EXCEPTION_CATCHER_INCLUDE_STACK_TRACE', true),
    
    'rate_limiting' => [
        'enabled' => env('EXCEPTION_CATCHER_RATE_LIMITING_ENABLED', true),
        'max_emails_per_hour' => env('EXCEPTION_CATCHER_MAX_EMAILS_PER_HOUR', 10),
        'cache_key_prefix' => 'exception_catcher_',
    ],
    
    'skip_exceptions' => [
        Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
        'Illuminate\Auth\AuthenticationException',
        'Illuminate\Validation\ValidationException',
        'Symfony\Component\HttpKernel\Exception\HttpException',
    ],
    
    'include_request_data' => env('EXCEPTION_CATCHER_INCLUDE_REQUEST', true),
];

Environment Variables

Add these to your .env file:

# Exception Catcher Configuration
EXCEPTION_CATCHER_ENABLED=true
EXCEPTION_CATCHER_TO_EMAIL="admin@yourapp.com,dev@yourapp.com"
EXCEPTION_CATCHER_FROM_EMAIL="noreply@yourapp.com"
EXCEPTION_CATCHER_FROM_NAME="Your App Name"
EXCEPTION_CATCHER_QUEUE_ENABLED=false
EXCEPTION_CATCHER_INCLUDE_STACK_TRACE=true
EXCEPTION_CATCHER_INCLUDE_REQUEST=true

# Rate Limiting Configuration
EXCEPTION_CATCHER_RATE_LIMITING_ENABLED=true
EXCEPTION_CATCHER_MAX_EMAILS_PER_HOUR=10

# Laravel Mail Configuration  
MAIL_MAILER=smtp
MAIL_HOST=your-smtp-host
MAIL_PORT=587
MAIL_USERNAME=your-email
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls

๐Ÿ”ง Integration Methods

Method 1: Using the Trait (Recommended)

The trait automatically handles exception reporting. Simply add it to your Exception Handler:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use NajibIsmail\LaravelExceptionCatcher\Traits\SendsExceptionEmails;
use Throwable;

class Handler extends ExceptionHandler
{
    use SendsExceptionEmails;
    
    // The trait automatically overrides the report() method
    // No additional code needed
}

Method 2: Manual Integration

If you prefer manual control, you can manually call the exception emailer:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler
{
    public function report(Throwable $exception)
    {
        parent::report($exception);

        // Send exception email
        if (app()->bound('exception.emailer')) {
            app('exception.emailer')->handle($exception, request());
        }
    }
}

Method 3: Using the Facade

You can also send exception emails manually anywhere in your code:

use NajibIsmail\LaravelExceptionCatcher\Facades\ExceptionCatcher;

try {
    // Your code here
} catch (Exception $e) {
    ExceptionCatcher::handle($e, request());
    throw $e; // Re-throw if needed
}

๐Ÿงช Testing

Command Line Testing

php artisan exception:test [type]

Available Types: runtime, invalid, custom, db, fatal, validation, http

Web Route Testing

GET http://your-app.com/exception-test
GET http://your-app.com/exception-test/runtime

Results: Exception caught โ†’ Email sent โ†’ Beautiful responsive template

๐Ÿ“ง Email Features

  • Responsive Design - Adapts to desktop, tablet, and mobile
  • Email Client Support - Gmail, Outlook, Apple Mail, Thunderbird
  • Enhanced Stack Traces - Color-coded with source indicators (ORIGIN, APP, VENDOR)
  • Rich Information - Request data, user details, environment info
  • Automatic From Address - Uses configuration or Laravel's default mail settings
  • Queue Support - Optional async email sending for better performance

๐Ÿ” Troubleshooting

Not Receiving Emails?

  1. Check your Laravel mail configuration
  2. Verify recipient email addresses in config
  3. Check spam/junk folders
  4. Test Laravel mail with php artisan tinker:
    Mail::raw('Test', function($msg) {
        $msg->to('your-email@example.com')->subject('Test');
    });

Command Not Found?

  • Run composer dump-autoload
  • Verify package installation: composer show najibismail/laravel-exception-catcher

Web Routes Not Working?

  • Clear route cache: php artisan route:clear
  • Check if service provider is registered: php artisan route:list | grep exception-test

Rate Limiting Issues?

  • Check cache configuration in config/cache.php
  • Clear cache: php artisan cache:clear
  • Adjust rate limits in your .env file:
    EXCEPTION_CATCHER_RATE_LIMITING_ENABLED=true
    EXCEPTION_CATCHER_MAX_EMAILS_PER_HOUR=20  # Increase if needed
  • Or disable rate limiting temporarily:
    EXCEPTION_CATCHER_RATE_LIMITING_ENABLED=false

๐Ÿ—๏ธ Package Structure

packages/laravel-exception-catcher/
โ”œโ”€โ”€ .gitignore                          # Git ignore rules
โ”œโ”€โ”€ LICENSE                             # MIT License  
โ”œโ”€โ”€ README.md                           # This documentation
โ”œโ”€โ”€ composer.json                       # Package configuration
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ exception-catcher.php          # Configuration file
โ”œโ”€โ”€ resources/
โ”‚   โ””โ”€โ”€ views/
โ”‚       โ””โ”€โ”€ exception-email.blade.php  # Responsive email template
โ”œโ”€โ”€ routes/
โ”‚   โ””โ”€โ”€ web.php                        # Test routes
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ Console/Commands/
    โ”‚   โ””โ”€โ”€ TestExceptionCatcher.php    # Test command
    โ”œโ”€โ”€ ExceptionCatcherServiceProvider.php # Service provider
    โ”œโ”€โ”€ ExceptionEmailer.php            # Core logic
    โ”œโ”€โ”€ Facades/
    โ”‚   โ””โ”€โ”€ ExceptionCatcher.php        # Facade
    โ”œโ”€โ”€ Mail/
    โ”‚   โ””โ”€โ”€ ExceptionNotification.php   # Mailable class
    โ””โ”€โ”€ Traits/
        โ””โ”€โ”€ SendsExceptionEmails.php    # Handler trait

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๏ฟฝ Support

๏ฟฝ๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿท๏ธ Version

Current Version: 1.0.7
Laravel Compatibility: 8.x, 9.x, 10.x, 11.x
PHP Compatibility: 8.0+

Made with โค๏ธ for the Laravel community