在 Restler 中使用自定义异常的正确方法是什么?


What's the right way to use custom exceptions in Restler?

让我们说,而不仅仅是输出:

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

我想输出:

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

还有没有办法捕获所有异常用于日志目的,例如?

使用 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"
      ]
    }
  }
}

信息:- 当 restler 在调试模式下运行时,将返回其他调试信息。可以使用Compose::$includeDebugInfo=false;将其关闭

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