【问题标题】:Exceptions will not render Laravel 5.7异常不会渲染 Laravel 5.7
【发布时间】: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-&gt;validate($request, BlueparkValidatorArrays::$getOrders);

如果有人能指出正确的方向,我将不胜感激。

【问题讨论】:

  • 你能把你的完整课程贴在这里吗?
  • 这通常发生在我们有一些无效的验证规则时
  • @MianHaseeb 完成!
  • hmmm,为什么在 getOrders 之前 $ 在这里 $this-&gt;validate($request, BlueparkValidatorArrays::$getOrders);??
  • @AhmedNourJamalEl-Din 它调用了一个名为 $getOrders 的数组 - 此调用工作正常,并从我的一个 Helpers 文件中正确处理验证

标签: laravel exception-handling laravel-5.7


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-03
    • 2012-04-19
    • 2019-05-11
    • 1970-01-01
    • 2014-09-25
    • 2016-10-06
    • 2018-07-29
    • 2012-04-24
    相关资源
    最近更新 更多