PHP code example of jftecnologia / laravel-exceptions

1. Go to this page and download the library: Download jftecnologia/laravel-exceptions 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/ */

    

jftecnologia / laravel-exceptions example snippets


use JuniorFontenele\LaravelExceptions\Facades\LaravelException;

return Application::configure(basePath: dirname(__DIR__))
    // ... other configurations
    ->withExceptions(function (Exceptions $exceptions) {
        // ... other exception handlers
        
        // Register Laravel Exceptions handler (must be the last handler)
        LaravelException::handles($exceptions);
    })
    ->create();

use JuniorFontenele\LaravelExceptions\Facades\LaravelException;

return Application::configure(basePath: dirname(__DIR__))
    // ... other configurations
    ->withExceptions(function (Exceptions $exceptions) {
        // ... other exception handlers
        
        // Register Laravel Exceptions handler (must be the last handler)
        LaravelException::handles($exceptions);
    })
    ->create();

use JuniorFontenele\LaravelExceptions\Exceptions\AppException;

// Basic exception
throw new AppException(
    message: 'Internal system error',
    userMessage: 'An error occurred. Please try again.',
    statusCode: 500
);

// With additional context
throw new AppException(
    message: 'Payment processing failed',
    userMessage: 'Unable to process payment.',
    statusCode: 422,
    context: [
        'payment_id' => $paymentId,
        'amount' => $amount,
    ]
);

use JuniorFontenele\LaravelExceptions\Exceptions\Http\NotFoundHttpException;
use JuniorFontenele\LaravelExceptions\Exceptions\Http\UnauthorizedHttpException;

throw new NotFoundHttpException('Resource not found');
throw new UnauthorizedHttpException('Access denied');

use Illuminate\Support\Facades\Schedule;

Schedule::command('laravel-exceptions:clean --force')
    ->daily()
    ->onOneServer();

return [
    // Custom error view
    'view' => 'laravel-exceptions::error',
    
    // Convert unhandled exceptions to AppException
    'convert_exceptions' => env('LARAVEL_EXCEPTIONS_CONVERT_EXCEPTIONS', true),
    
    // Render custom exceptions even in debug mode
    'render_in_debug' => env('LARAVEL_EXCEPTIONS_RENDER_IN_DEBUG', false),
    
    // Days to keep exception records (used by clean command)
    'delete_records_older_than_days' => 365,
    
    // Automatic context providers
    'context_providers' => [
        AppExceptionContextProvider::class,
        AppContextProvider::class,
        HostContextProvider::class,
        UserContextProvider::class,
        ExceptionContextProvider::class,
        PreviousExceptionContextProvider::class,
    ],
    
    // Storage channels
    'channels' => [
        'database' => Database::class,
    ],
    
    // Channel-specific settings
    'channels_settings' => [
        'database' => [
            'table_name' => 'exceptions_log',
            'model' => Exception::class,
            'user_model' => config('auth.providers.users.model'),
            'user_model_table' => 'users',
        ],
    ],
    
    // Ignored exceptions (won't be logged or converted)
    'ignored_exceptions' => [
        AuthenticationException::class,
        ValidationException::class,
    ],
    
    // HTTP exception mappings
    'http_exceptions' => [
        400 => BadRequestHttpException::class,
        401 => UnauthorizedHttpException::class,
        403 => AccessDeniedHttpException::class,
        404 => NotFoundHttpException::class,
        // ... more status codes
    ],
];
bash
php artisan laravel-exceptions:install
bash
php artisan vendor:publish --tag="laravel-exceptions-assets"
bash
php artisan vendor:publish --tag="laravel-exceptions-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-exceptions-config"
bash
php artisan vendor:publish --tag="laravel-exceptions-views"
bash
php artisan make:app-exception PaymentFailedException