【发布时间】:2019-05-12 11:35:44
【问题描述】:
我正在使用 Laravel v 5.7.15。
我编写了一个验证助手来验证 API 请求 - 这很成功,并且之前我使用了 try/catch 来包围它。
我已经开始在处理程序中处理异常,但是我无法运行“渲染”函数 - 它直接进入“报告”并在我的修补程序控制台中抛出异常。
处理程序:(按要求提供完整类)
<?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);
如果有人能指出正确的方向,我将不胜感激。
【问题讨论】:
-
你能把你的完整课程贴在这里吗?
-
这通常发生在我们有一些无效的验证规则时
-
@MianHaseeb 完成!
-
hmmm,为什么在 getOrders 之前 $ 在这里
$this->validate($request, BlueparkValidatorArrays::$getOrders);?? -
@AhmedNourJamalEl-Din 它调用了一个名为 $getOrders 的数组 - 此调用工作正常,并从我的一个 Helpers 文件中正确处理验证
标签: laravel exception-handling laravel-5.7