Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

php - Laravel custom error handler for a specific method

I have laravel project and default error handler that works just fine.

Now i need import functionality that runs in queues. There is a big chance that some errors may accour during the process. So i want to create my custom handler for this method only. It should fire on all exception and critical errors. It also should pass specific argument so i know on which row of import has failed. The only solution i've found is How can I register custom error handler in laravel 5?

    /**
     * Do not forget to import them before using!
     */ 
    $this->app->bind(
        ExceptionHandler::class,
        CustomHandler::class
    );  

But this applies to whole project when i need it in specific method. More over i want to extend not overwrite already created handler.

I reied something like this right in my import function, but this doesn't seems to work.

    public function collection(Collection $rows)
{
        app()->bind(ExceptionHandler::class,
            TelegramExceptionHandler::class); 

        foreach ($orderRows as $row)
        {
            // write to Bb
            //Errors happens here
            //I want to know bumber of rows and traceback
        }

        //send msg to telegram
        event(new ImportComplete($event,count($orderRows)));
}

My Telegram Exception handler

    <?php
namespace AppExceptions;

use Exception;
use IlluminateAuthAuthenticationException;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use IlluminateSupportFacadesLog;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;

class TelegramExceptionHandler extends ExceptionHandler
{


    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        Log::info("ERROR111111");
        Log::info("ERROR111111");
        Log::info("ERROR111111");
        Log::info("ERROR111111");
        Log::info("ERROR111111");
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Exception  $exception
     * @return IlluminateHttpResponse
     */
    public function render($request, Exception $exception)
    {
        Log::info("ERROR222222");
        Log::info("ERROR222222");
        Log::info("ERROR222222");
        Log::info("ERROR222222");
        Log::info("ERROR222222");
        Log::info("ERROR222222");
        if ($exception instanceof NotFoundHttpException) {
            return response()->view('errors.404', [], 404);
        }
        return parent::render($request, $exception);
    }

    /**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  IlluminateAuthAuthenticationException  $exception
     * @return IlluminateHttpResponse
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        Log::info("ERROR333333");
        Log::info("ERROR333333");
        Log::info("ERROR333333");
        Log::info("ERROR333333");
        Log::info("ERROR333333");

        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
        return redirect()->guest('login');
    }


}

But in my log file there is no such logs. What am i doing wrong and how can i fix it?

question from:https://stackoverflow.com/questions/65935236/laravel-custom-error-handler-for-a-specific-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...