Ano*_*non 5 php error-handling 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);
}
Run Code Online (Sandbox Code Playgroud)
我已经包括以下内容:
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;
Run Code Online (Sandbox Code Playgroud)
此外,之前添加了以下内容:
protected $dontReport = [
AuthorizationException::class,
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
TokenMismatchException::class,
];
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个?我已经坚持了好几天了。任何帮助,将不胜感激。
原因是这些异常被框架排除,因此没有报告。请参阅此处以供参考。
由于定义排除的异常的属性是protected,因此您应该能够在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,
];
Run Code Online (Sandbox Code Playgroud)
您还必须use为所有类添加语句。
(请注意,这是 Laravel 5.6 的排除例外列表 - 如果您使用其他版本,您可能必须使用 gitblame 或其他分支来查找您的版本的正确列表。)