PHP code example of iporto / laravel-error-helper

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

    

iporto / laravel-error-helper example snippets


use IPorto\LaravelErrorHelper\Facades\Error;

// Exibir erro simples
Error::abort('Erro ao processar a solicitação');

// Exibir erro com dados adicionais
Error::abort('Erro ao processar o pagamento', [
    'errors' => [
        'code' => 'PAYMENT_FAILED',
        'reason' => 'Fundos insuficientes'
    ],
    'trace' => [
        'Tentativa 1 falhou',
        'Tentativa 2 falhou'
    ],
    'exception' => [
        'type' => 'PaymentGatewayException'
    ]
]);

// Alternativamente, você pode usar a classe ErrorHelper diretamente
use IPorto\LaravelErrorHelper\ErrorHelper;

ErrorHelper::abort('Mensagem de erro', $dadosAdicionais);



namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use IPorto\LaravelErrorHelper\CustomErrorException;

class Handler extends ExceptionHandler
{
    // Outras configurações existentes...

    public function render($request, Throwable $e)
    {
        if ($e instanceof CustomErrorException) {
            return response()->view('errors.500', [
                'exception' => $e,
                'extra' => $e->getExtraData(),
                'request' => $request
            ], 500);
        }

        return parent::render($request, $e);
    }
}

public function render($request, Throwable $e)
{
    if ($e instanceof CustomErrorException) {
        return response()->view('errors.basic-500', [
            'exception' => $e,
            'extra' => $e->getExtraData(),
            'request' => $request
        ], 500);
    }

    return parent::render($request, $e);
}



namespace App\Http\Controllers;

use IPorto\LaravelErrorHelper\Facades\Error;
use Exception;

class PaymentController extends Controller
{
    public function processPayment()
    {
        try {
            // Simula uma tentativa de pagamento...
            // throw new Exception("Gateway de pagamento indisponível");
            
            return redirect()->route('success');
        } catch (Exception $e) {
            // Usar o ErrorHelper para exibir erro personalizado
            Error::abort('Falha ao processar pagamento', [
                'errors' => [
                    'code' => 'PAYMENT_PROCESSOR_ERROR',
                    'message' => $e->getMessage()
                ],
                'exception' => [
                    'class' => get_class($e),
                    'code' => $e->getCode()
                ],
                'trace' => [
                    'request_id' => request()->header('X-Request-ID'),
                    'timestamp' => now()->toIso8601String()
                ]
            ]);
        }
    }
}
bash
php artisan vendor:publish --tag=laravel-error-helper-views