JsonRPC返回失败的结果

xiewen_kevin

Walkor,远程请求一次JsonRPC的时候 ,比如用户登陆 登陆成功正确返回是
{
"code": 0,
"msg": "ok",
"data": ""
}

那么,如果密码输入不正确登陆失败呢?如何返回{
"code": 1,
"msg": "error",
"data": ""
}

3802 1 0
1个回答

walkor

方法我想到2种。

1、可以不抛exception,把结果放到data里面,不管是用户存在还是不存在。

2、也可以自己改下rpc框架/Applications/JsonRpc/start.php,加个业务异常分支。业务异常不上报错误。

// 调用类的方法
try
{
    $ret = call_user_func_array(array($class, $method), $param_array);
    StatisticClient::report($class, $method, 1, 0, '', $statistic_address);
    // 发送数据给客户端,调用成功,data下标对应的元素即为调用结果
    return $connection->send(array('code'=>0, 'msg'=>'ok', 'data'=>$ret));
}
    // 业务异常,
catch(BusinessException $e)
{
    $code = $e->getCode() ? $e->getCode() : 500;
    StatisticClient::report($class, $method, 1, $code, $e, $statistic_address);
    return $connection->send(array('code'=>$code, 'msg'=>$e->getMessage(), 'data'=>$e));
}
    // 有异常
catch(Exception $e)
{
    // 发送数据给客户端,发生异常,调用失败
    $code = $e->getCode() ? $e->getCode() : 500;
    StatisticClient::report($class, $method, $success, $code, $e, $statistic_address);
    return $connection->send(array('code'=>$code, 'msg'=>$e->getMessage(), 'data'=>$e));
}
年代过于久远,无法发表回答
🔝