本文介绍了处理程序未捕获自动调整和令牌不匹配异常.拉拉韦尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试处理laravel引发的异常.我已经尝试了很多东西,但是似乎没有用.以下是我使用的语法:

I've been trying to handle the exception thrown by laravel since for a while. I've tried many things but it doesn't seems to be working. Following are my syntax i've used:

public function render($request, Exception $e)
{
    //404 page when a model is not found
    if ($e instanceof ModelNotFoundException) {
        return response()->view('errors.404', [], 404);
    }elseif ($e instanceof \AuthorizationException) {
        return response()->view('errors.403', [], 403);
    }elseif ($e instanceof TokenMismatchException) {
        Flash::error('Sorry, your session seems to have expired. Please try again.');
        return redirect('/');
    }elseif ($e instanceof \ErrorException) {
        return response()->view('errors.500', [], 500);
    }else {
        return response()->view('errors.500', [], 500);
    }
    // return parent::render($request, $e);
}

我包括以下内容:

use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;

此外,以前添加了以下内容:

Further, following things were added previously:

protected $dontReport = [
    AuthorizationException::class,
    HttpException::class,
    ModelNotFoundException::class,
    ValidationException::class,
    TokenMismatchException::class,
];

有人可以帮我吗?我已经坚持了好几天.任何帮助,将不胜感激.

Can anyone help me with this? I've stuck in this for days. Any help would be appreciated.

推荐答案

原因是这些异常已被框架排除,因此未报告.请参阅此处参考.

The cause is that these exceptions are excluded by the framework and are therefore not reported. See here for a reference.

由于定义排除的异常的属性是protected,因此您应该能够在app/Exceptions/Handler.php文件中覆盖它.您不应该删除所有这些异常,而只能删除您真正想要捕获的异常.因此,只需将以下几行添加到您的Handler.php:

As the property defining the excluded exceptions is protected, you should be able to override it in your app/Exceptions/Handler.php file. You should not remove all of these exceptions, but only the ones you really want to catch yourself. So simply add the following lines to your Handler.php:

/**
 * A list of the internal exception types that should not be reported.
 *
 * @var array
 */
protected $internalDontReport = [
    AuthenticationException::class,
    HttpException::class,
    HttpResponseException::class,
    ModelNotFoundException::class,
    ValidationException::class,
];

您还必须为所有类添加use语句.

You will also have to add the use statements for all of the classes.

(请注意,这是Laravel 5.6排除的例外列表-如果您使用的是其他版本,则可能必须使用git blame或其他分支才能找到适用于您的版本的正确列表.)

(Please be aware that this is the list of excluded exceptions for Laravel 5.6 - if you are using another version, you might have to use git blame or another branch to find the correct list for your version.)

这篇关于处理程序未捕获自动调整和令牌不匹配异常.拉拉韦尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:48