【问题标题】:What's the right way to use custom exceptions in Restler?在 Restler 中使用自定义异常的正确方法是什么?
【发布时间】:2023-04-10 12:46:02
【问题描述】:

让我们说只是输出的 insted:

{
  "error": 
  {
    "code": 500,
    "message": "Some internal error"
  }
}

我想输出:

{
  "error": 
  {
    "code": 500,
    "message": "Some internal error",
    "error_code" : 1050
  }
}

还有一种方法可以捕获所有异常以用于日志目的吗?

【问题讨论】:

    标签: php restler


    【解决方案1】:

    使用 RestException 抛出异常并使用 details 参数(一个数组)添加额外的细节

    throw new RestException(400, 'invalid user', array('error_code' => 12002));
    

    给我以下内容

    {
      "error": {
        "code": 400,
        "message": "Bad Request: invalid user",
        "error_code": 12002
      },
      "debug": {
        "source": "Say.php:5 at call stage",
        "stages": {
          "success": [
            "get",
            "route",
            "negotiate",
            "validate"
          ],
          "failure": [
            "call",
            "message"
          ]
        }
      }
    }
    

    Info:-restler 在调试模式下运行时会返回额外的调试信息。 可以使用Compose::$includeDebugInfo=false;关闭它

    注意:-确保您使用的是 Restler 3.0 RC4 或更高版本

    【讨论】:

    • 还有一种简单的方法来记录异常以进行调试吗?假设我想在我的应用程序中找到错误的调用,从 api 记录所有内容要容易得多
    • 您可以使用 iCompose 接口编写自己的 compose 类并从那里记录异常
    猜你喜欢
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多