[英]How to handle PDOException in Laravel 5
I've modified Exceptions/Handler.php to: 我已经将Exceptions / Handler.php修改为:
<?php namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Handler extends ExceptionHandler {
/**
* A list of the exception types that should not be reported.
*
* @var array
*/
protected $dontReport = [
'Symfony\Component\HttpKernel\Exception\HttpException'
];
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $e
* @return void
*/
public function report(Exception $e)
{
return parent::report($e);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException)
{
return response()->view('errors/404');
}
elseif ($e instanceof ErrorException) {
return response()->view('errors/404');
}
elseif ($e instanceof ModelNotFoundException) {
return response()->view('error_log(message)ors/404');
}
elseif($e instanceof PDOException)
{
return Redirect::to('install.php');
}
elseif($e instanceof QueryException)
{
return Redirect::to('install.php');
}
else return response()->view('errors/error');
}
trying to redirect to installation whenever application makes PDOException, but I'm just getting general error view (errors/error - from the last line of the code above). 尝试在应用程序出现PDOException时尝试重定向到安装,但是我只是得到一般的错误视图(错误/错误-来自上面代码的最后一行)。
Is there a way to cach specifically PDOException error? 有没有一种方法可以专门处理PDOException错误?
I think I've found the solution: 我想我已经找到了解决方案:
just put 刚放
use PDOException;
include use PDOException; 包括使用PDOException;
public function render($request, Exception $e)
{
if($e instanceof PDOException)
{
return response()->view('errors/404');
}
in Exception/Handler it worked for me 在异常/处理程序中对我有用
This is what worked for me in Laravel 5.3 (excerpt of my Handler.php
file located in /app/Exceptions/
): 这是我在Laravel 5.3(位于
/app/Exceptions/
Handler.php
文件的一部分)中起作用的内容:
public function report(Exception $e)
{
// Both this, and below, are needed
if($e instanceof \PDOException) {
return response()->view('errors/404');
}
return parent::report($e);
}
public function render($request, Exception $e)
{
if($e instanceof \PDOException) {
// TODO send me an email!
Session::flash('messageclass', 'danger');
Session::flash('message', trans('general.pdoexception'));
return redirect()->back();
}
return parent::render($request, $e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.