doutuo2829 2013-10-11 11:29
浏览 11
已采纳

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

Lets say insted of just output:

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

I would like to output:

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

Also is there a way we can catch all the exceptions for log purposes for example?

  • 写回答

1条回答 默认 最新

  • douzhao4071 2013-10-14 03:37
    关注

    Use RestException to throw the exception and use the details parameter (an array) to add additional details

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

    gives me the following

    {
      "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:- additional debug information is returned when restler is running in debug mode. It can be turned off by using Compose::$includeDebugInfo=false;

    Note:- Make sure you are using Restler 3.0 RC4 or higher

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了