【问题标题】:Laravel Exceptions Handler not workLaravel 异常处理程序不起作用
【发布时间】:2015-10-06 06:36:14
【问题描述】:

环境:Laravel 5.1、PHP 5.6.10

我尝试实现App\Exceptions\Handler::render() 以响应 JSON 格式的错误消息。

app/Exceptions/Handler.php如下:

// ignore..

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    } elseif ($e instanceof AbstractException) {
        return response()->apiJsonError(
                $e->getMessage(),
                $e->getErrors(),
                $e->statusCode());
    }

    // ignore...
}

在控制器中,也抛出异常:

if (ArrayUtil::isIndexExceed($list, $maxIndex)) {
    // Index exceeds
    throw new App\Exceptions\ExceedingIndexException;
}

但是,当错误发生时,不会调用 Handler::render()。响应是ExceedingIndexException 堆栈。

以下部分为异常类

我的自定义异常类,ExceedingIndexException

namespace App\Exceptions;

use App\Http\Responses\Error;
use App\Exceptions\AbstractException;

class ExceedingIndexException extends AbstractException
{
    public function __construct()
    {
        $message = 'Unable to execute';
        $error = new Error('exceeding_index_value');
        $statusCode = 400;

        parent::__construct($statusCode, $error, $message);
    }
}

ExceedingIndexException 类继承 AbstractException

namespace App\Exceptions;

abstract class AbstractException extends \Exception
{
    protected $statusCode;
    protected $errors;

    public function __construct(
        $statusCode, $errors, $message, $code = 0, \Exception $previous = null) {
        parent::__construct($message, $code, $previous);

        $this->statusCode = $statusCode;
        $this->errors = $errors;
    }

    public function getStatusCode() 
    {
        return $this->statusCode;
    }

    public function getErrors() 
    {
        return $this->errors;
    }
}

解决方案

我发现我的项目依赖于 Dingo API for RESTful API。因为,Dingo 也支持并注册了自己的异常处理程序,App\Exceptions\Handler 不会被调用。

我尝试使用Custom Exception Responses in Dingo API 作为我的异常处理程序来响应 JSON 格式的错误。它对我有用。

【问题讨论】:

  • 我认为这个问题与您位于控制器内部的ExceedingIndexException 无关。除了ModelNotFoundException 触发AbstractException 之前应该发生什么错误?

标签: php laravel exception laravel-5.1


【解决方案1】:

实际上,您可以将 Dingo API 错误处理程序替换为自定义错误处理程序。获取https://github.com/KIVagant/api/blob/develop/src/Exception/Handler.php 的副本 并将其保存到 YourApp\Exceptions\Api\V1\Handler.php。添加接口 Dingo\Api\Contract\Debug\ExceptionHandler 到它, 然后按照Exceptions now can return any additional data中的说明进行操作。

use Dingo\Api\Contract\Debug\ExceptionHandler as DingoExceptionHandler;
class Handler implements ExceptionHandler, DingoExceptionHandler {

替换错误处理程序,例如在boot()中。

// Resolve YourApp\Exceptions\Api\V1\Handler ifself
$this->app->alias('api.exception', 'YourApp\Exceptions\Api\V1\Handler');

$this->app->singleton('api.exception', function ($app) {
    return new \YourApp\Exceptions\Api\V1\Handler($app['Illuminate\Contracts\Debug\ExceptionHandler'],
            $app['config']['api.errorFormat'], $app['config']['api.debug']);
});

不要忘记设置错误格式。您可以在 boot() 中设置错误格式,例如:

// Set up error format
$this->app['api.exception']->setErrorFormat(...)

【讨论】:

    【解决方案2】:

    确实,Dingo API 接管了异常处理。

    在 Lumen Exception 处理程序不处理任何事情的情况下,您会遇到类似的问题,可能是某些包接管了处理。在这种情况下,它是 Dingo API。

    为 Dingo API 的异常注册您的自定义响应: https://github.com/dingo/api/wiki/Errors-And-Error-Responses#custom-exception-responses

    【讨论】:

    • 请在帖子中包含链接中的重要部分
    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2017-10-07
    • 2018-07-08
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多