Commit 2008066c authored by 杨培's avatar 杨培

支持接口异常的传递

parent 1c0eecdb
...@@ -12,7 +12,7 @@ require __DIR__.'/_init.php'; ...@@ -12,7 +12,7 @@ require __DIR__.'/_init.php';
Member::setDebug(); Member::setDebug();
//$info = Member::getMemberInfo(12); $info = Member::getMemberInfo(2);
$info = Member::getSyncByMobile('18019580609'); //$info = Member::getSyncByMobile('13000000002');
//$info = Member::getSyncByUid(40); //$info = Member::getSyncByUid(514);
print_r($info); print_r($info);
...@@ -12,5 +12,5 @@ require __DIR__.'/_init.php'; ...@@ -12,5 +12,5 @@ require __DIR__.'/_init.php';
Member::setDebug(); Member::setDebug();
$name = uniqid('dev_'); $name = uniqid('dev_');
$info = Member::register($name, '123456', $name, ['mobile'=>'16000000000']); $info = Member::register($name, '123456', $name, ['mobile'=>'16000000001']);
print_r($info); print_r($info);
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: pader * User: pader
* Date: 2017/6/27 * Date: 2017/7/6
* Time: 16:44 * Time: 11:46
*/ */
namespace internalApi\base; namespace internalApi\base;
class ServerException extends \Exception class ApiException extends \Exception
{ {
} }
\ No newline at end of file
...@@ -97,7 +97,7 @@ class Client ...@@ -97,7 +97,7 @@ class Client
* @param string $api * @param string $api
* @param array $params * @param array $params
* @return mixed * @return mixed
* @throws ClientException API 请求失败时,会抛出此异常 * @throws ClientException|ApiException 请求过程出错会抛出ClientException,若源API抛异常,则会抛出ApiException
*/ */
protected static function call($api, $params=[]) protected static function call($api, $params=[])
{ {
...@@ -109,22 +109,30 @@ class Client ...@@ -109,22 +109,30 @@ class Client
list($httpCode, $response) = $instance->request($url, $params); list($httpCode, $response) = $instance->request($url, $params);
if ($httpCode == 200 && $response) {
$data = @json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ClientException('接口返回数据异常: ' . $response);
}
return $data;
}
if ($httpCode == 403 && substr($response, 0, 6) === 'error:') { if ($httpCode == 403 && substr($response, 0, 6) === 'error:') {
$message = substr($response, 6); $message = substr($response, 6);
throw new ClientException("接口 $api 调用失败: " . $message); throw new ClientException("接口 $api 调用失败: " . $message);
} elseif ($httpCode === 0) {
throw new ClientException('接口请求异常: '.$response, $httpCode);
} elseif ($httpCode != 200 || $response === false) {
throw new ClientException('接口响应异常: '.$response, $httpCode);
} }
$data = @json_decode($response, true); if ($httpCode == 500 && substr($response, 0, 10) === 'exception:') {
$e = substr($response, 10);
$e = json_decode($e, true);
throw new ApiException("{$e['class']}: {$e['message']}", $e['code']);
}
if (json_last_error() !== JSON_ERROR_NONE) { if ($httpCode === 0) {
throw new ClientException('接口返回数据异常: ' . $response); throw new ClientException('接口请求异常: '.$response, $httpCode);
} }
return $data; throw new ClientException('接口响应异常: '.$response, $httpCode);
} }
/** /**
......
...@@ -9,6 +9,6 @@ ...@@ -9,6 +9,6 @@
namespace internalApi\base; namespace internalApi\base;
class ClientException extends \ErrorException class ClientException extends \Exception
{ {
} }
\ No newline at end of file
...@@ -94,7 +94,11 @@ class Server ...@@ -94,7 +94,11 @@ class Server
} }
} }
$data = call_user_func_array($callable, $params); try {
$data = call_user_func_array($callable, $params);
} catch (\Exception $e) {
$this->responseException($e);
}
if ($return) { if ($return) {
return $data; return $data;
...@@ -104,6 +108,24 @@ class Server ...@@ -104,6 +108,24 @@ class Server
} }
} }
/**
* @param \Exception $e
*/
public function responseException($e)
{
header('HTTP/1.0 500 Internal Server Error');
$exception = [
'class' => get_class($e),
'code' => $e->getCode(),
'message' => $e->getMessage(),
'line' => $e->getFile(),
'file' => $e->getFile(),
'traceString' => $e->getTraceAsString()
];
echo 'exception:'.json_encode($exception, JSON_UNESCAPED_UNICODE);
exit;
}
public function responseError($message) public function responseError($message)
{ {
header('HTTP/1.0 403 Forbidden'); header('HTTP/1.0 403 Forbidden');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment