我正在使用Laravel v 5.7.15。
我写了一个验证助手来验证API请求-成功运行,并且在我使用try / catch包围它之前。
我已经着手处理处理程序中的异常,但是我无法运行功能“ render”-它直接进入“ report”并在我的修补程序控制台中引发异常。
处理程序:(按要求提供完整课程)
<?php
namespace App\Exceptions;
use Illuminate\Validation\ValidationException;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Log;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* @param Exception $exception
* @return mixed|void
* @throws Exception
*/
public function report(Exception $exception)
{
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
dd($exception);
$log = new Log();
$log->status = 2;
// Validate API incoming data
if ($exception instanceOf ValidationException) {
foreach ($exception->errors() as $error) {
// collect multiple validation errors
$message[] = implode('', $error);
}
$message = implode('', $message);
$log->message = $message;
$log->save();
$response = [
'message' => $message,
'status' => 400,
];
} else {
$response = [
'message' => $exception->getMessage(),
'status' => '500',
];
}
return parent::render($request, $exception);
}
}
这不会失败并转储,但是我可以在报告功能中使用dd,并且可以正常工作。该文件的其余部分保持不变,除了文件顶部的包含。
这是我在控制器中调用验证器的方式:
$this->validate($request, BlueparkValidatorArrays::$getOrders);
如果有人能指出我正确的方向,我将不胜感激。
答案 0 :(得分:1)
这可能是由您的日志配置问题引起的。
对parent::report($exception);
的调用从laravel源代码运行以下代码:
public function report(Exception $e)
{
...
try {
$logger = $this->container->make(LoggerInterface::class);
} catch (Exception $ex) {
throw $e; // throw the original exception
}
...
}
请注意throw $e
,而不是throw $ex
。因此,如果创建记录器实现失败,则将 抛出正在处理的原始异常。
要对此进行测试,请在报告功能中注释掉parent::report($exception);
,然后查看是否按预期方式调用render()
。
如果是,则您的日志配置不起作用。确保您对日志位置具有正确的权限,并且您的.env
文件不会覆盖任何laravel的日志记录设置。参见How to debug Laravel error 500 with no logs, no information