【问题标题】:How can I catch NotFoundHttpException in laravel to send a custom response?如何在 laravel 中捕获 NotFoundHttpException 以发送自定义响应?
【发布时间】:2019-05-19 15:36:10
【问题描述】:

我不明白为什么 laravel 没有捕获 NotFoundHttpException,但它确实捕获了所有其他异常。

我想根据异常类型向我的用户发送自定义 json 响应,这适用于除 NotFoundHttpException 之外的所有列出的异常:

namespace App\Exceptions;

use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Auth\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{

    public function render($request, Exception $exception)
    {
        switch (true) {
            case $exception instanceof QueryException:
                return response()->json('QueryException')

            case $exception instanceof NotFoundHttpException:
                return response()->json('NotFoundHttpException')

            case $exception instanceof AuthenticationException:
                return response()->json('AuthenticationException')

            default:
                return response()->json('Different Exception')
        }

        //return parent::render($request, $exception);
    }
}

当我请求创建 NotFoundHttpException 时,laravel 会使用默认的 switch case 选项进行响应,对此有什么建议吗?

【问题讨论】:

  • 尝试 instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  • @AhmedAboud,我已经试过了,还是不行。

标签: laravel exception


【解决方案1】:

Laravel 的异常处理器决定如何处理异常的抛出,ModelNotFoundException 默认被忽略。

检查 app/Exceptions/Handler.php 中的文件,该文件将扩展 Laravel 框架中的异常处理程序。

在此处查看文档:https://laravel.com/docs/5.8/errors#the-exception-handler

【讨论】:

  • 感谢您的回答!你是绝对正确的!在本地调试环境中工作时,Laravel 会忽略 ModelNotFoundException,而是呈现 NotFoundHttpException。
【解决方案2】:

我已经找到了解决这个问题的方法,我仍然不完全理解它是如何工作的,但它对我有用,让我解释一下:

当我在 .env 文件中有 APP_DEBUG=true 并在 Handler 类中使用默认渲染函数时,它向我抛出了一个“Symfony\Component\HttpKernel\Exception\NotFoundHttpException”。

但是当我执行 get_class($exception) 以确保它抛出相同的类时,由于某种原因它抛出了一个“Illuminate\Database\Eloquent\ModelNotFoundException”。所以我只是改变了我的代码来捕捉那个异常: 命名空间 App\Exceptions;

use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{

    public function render($request, Exception $exception)
    {
        switch (true) {
            case $exception instanceof QueryException:
                return response()->json('QueryException')

            case $exception instanceof ModelNotFoundException:
                return response()->json('ModelNotFoundException')

            case $exception instanceof AuthenticationException:
                return response()->json('AuthenticationException')

            default:
                return response()->json('Different Exception')
        }

        //return parent::render($request, $exception); //This throws a ModelNotFoundException
    }
}

这解决了问题,但我仍然不明白为什么它会以默认方式引发不同的异常。

当我向 localhost:8000/api/users/$id 发送 get 请求到不存在的用户 ID 时,这是引发异常的代码。

//UserController.php

public function show(User $user)
{
    return new UserResource($user);
}

【讨论】:

  • 因为查询没有返回任何内容
  • 你在使用 findOrFail() 吗?
  • @AhmedAboud,我使用的是路由模型绑定,可以查看文档here
猜你喜欢
  • 2019-05-01
  • 2018-01-27
  • 1970-01-01
  • 2018-08-26
  • 2017-06-19
  • 2018-06-08
  • 2015-06-06
  • 1970-01-01
相关资源
最近更新 更多