微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 处理程序没有捕获自动化和令牌不匹配异常. Laravel

我一直试图处理laravel抛出的异常一段时间.我尝试过很多东西,但似乎没有用.以下是我使用的语法:

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;

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

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

谁能帮我这个?我已经坚持了几天.任何帮助,将不胜感激.

解决方法:

原因是框架排除了这些例外情况,因此不予报告.请参阅here获取参考.

由于定义排除异常的属性受到保护,您应该能够在app / Exceptions / Handler.PHP文件中覆盖它.您不应该删除所有这些异常,而只删除您真正想要捕获的异常.只需将以下行添加到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语句.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐