PHP code example of adaptit-darshan / exception-notifier

1. Go to this page and download the library: Download adaptit-darshan/exception-notifier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adaptit-darshan / exception-notifier example snippets




use Damku999\ExceptionNotifier\JsonExceptionHandler;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->render(function (Throwable $e) {
            return app(JsonExceptionHandler::class)->handle($e);
        });
    })->create();

return [
    // Enable/disable globally
    'enabled' => env('EXCEPTION_EMAIL_ENABLED', false),

    // Silent mode in local environment
    'silent_in_local' => env('EXCEPTION_EMAIL_SILENT_LOCAL', true),

    // Email recipients
    'recipients' => array_filter(array_map('trim', explode(',', env('EXCEPTION_EMAIL_TO', '')))),

    // Fallback recipients if none specified
    'fallback_recipients' => ['[email protected]'],

    // Rate limiting
    'max_emails_per_signature_per_hour' => env('EXCEPTION_EMAIL_MAX_PER_HOUR', 10),
    'rate_limit_window' => env('EXCEPTION_EMAIL_RATE_WINDOW', 3600),

    // Ignored exceptions (won't send emails)
    'ignored_exceptions' => [
        \Illuminate\Validation\ValidationException::class,
        \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
        \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException::class,
        \Illuminate\Auth\AuthenticationException::class,
    ],

    // Critical exceptions (bypass rate limits)
    'critical_exceptions' => [
        \Illuminate\Database\QueryException::class,
    ],

    // Bot user agents to ignore
    'ignored_bots' => [
        'googlebot', 'bingbot', 'crawler', 'spider', 'bot',
    ],

    // Include context data in emails
    '

// Any uncaught exception will trigger an email notification
throw new \Exception('Something went wrong!');

// Validation exceptions are ignored by default (configurable)
throw ValidationException::withMessages(['email' => 'Invalid email']);

// Database exceptions are marked as critical (always sent)
DB::table('non_existent')->get(); // Triggers critical email

use Damku999\ExceptionNotifier\Facades\ExceptionNotifier;

try {
    // Your code
} catch (\Throwable $e) {
    ExceptionNotifier::notify($e);

    // Continue with your error handling
}

use Damku999\ExceptionNotifier\Facades\ExceptionNotifier;

// Check if rate limit exceeded for specific exception
$signature = ExceptionNotifier::generateSignature($exception);
$exceeded = ExceptionNotifier::isRateLimitExceeded($signature);

// Get current count for signature
$count = ExceptionNotifier::getRateLimitCount($signature);

// Get all rate limit statuses
$statuses = ExceptionNotifier::getRateLimitStatus();

// In your AppServiceProvider or config
config([
    'exception_notifier.branding' => [
        'email_logo' => 'images/logo.png',
        'primary_color' => '#007bff',
        'text_color' => '#333333',
        'footer_text' => 'Your Company Name',
        'support_email' => '[email protected]',
    ],
]);

// In JsonExceptionHandler
try {
    $this->notifierService->notify($e);
} catch (Throwable $notificationError) {
    // Silently fail if notification fails
    Log::error('Exception notification failed', [
        'error' => $notificationError->getMessage(),
    ]);
}

'ignored_bots' => [
    'googlebot', 'bingbot', 'slurp', 'crawler', 'spider',
    'bot', 'facebookexternalhit', 'twitterbot', 'whatsapp',
    'telegram', 'curl', 'wget',
],
bash
php artisan vendor:publish --tag="exception-notifier-config"
bash
php artisan vendor:publish --tag="exception-notifier-views"
bash
php artisan vendor:publish --tag="exception-notifier-migrations"
php artisan migrate
bash
php artisan exception:rate-limit-status

┌──────────────────────────────────────────────────────────────┬───────┬─────┬──────────┐
│ Exception Signature                                          │ Count │ Max │ TTL (s)  │
├──────────────────────────────────────────────────────────────┼───────┼─────┼──────────┤
│ Exception:app/Http/Controllers/UserController.php:45         │ 8     │ 10  │ 2847     │
│ QueryException:app/Models/User.php:123                       │ 15    │ 10  │ 1523     │
└──────────────────────────────────────────────────────────────┴───────┴─────┴──────────┘
bash
php artisan exception:clear-rate-limits
bash
php artisan exception:clear-rate-limits --signature="Exception:app/Http/Controllers/UserController.php:45"
bash
php artisan exception:test
bash
php artisan exception:test --type=critical
bash
php artisan vendor:publish --tag="exception-notifier-views"

Format: ExceptionClass:FilePath:LineNumber
Example: Exception:app/Http/Controllers/UserController.php:45