From 86fbc11fd9c567f0f55111152c680dc9c8dcab65 Mon Sep 17 00:00:00 2001 From: dongwei <838456736@qq.com> Date: Thu, 17 Jan 2019 14:10:18 +0800 Subject: [PATCH] rpc exception --- src/Client.php | 12 ++++++------ src/Providers/ClientServiceProvider.php | 5 ++++- src/Server/JsonRpcMethod.php | 8 +++++++- src/Server/JsonRpcServer.php | 15 +++++++-------- src/Server/JsonRpcServerDoc.php | 6 ------ 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Client.php b/src/Client.php index acc58d0..8c7e6a2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -41,16 +41,15 @@ class Client { $default = [ 'app' => '***', - 'log_path'=> "/logs/rpc_monitor_".date("Ymd").".log", + 'log_path' => "/logs/rpc_monitor_" . date("Ymd") . ".log", 'log_formatter' => \JsonRpc\Logging\LogstashFormatter::class, ]; $this->config = array_merge($default, $config); - $stream = new StreamHandler(app()->storagePath().$this->config['log_path']); - app('log')->info('test json-rpc config', $this->config); + $stream = new StreamHandler(app()->storagePath() . $this->config['log_path']); $stream->setFormatter(new $this->config['log_formatter']()); $logger = new Logger('RPC.LOGGER'); $logger->pushHandler($stream); - $this->id = app('request')->header('X-Request-Id')?:"no-x-request-id"; + $this->id = app('request')->header('X-Request-Id') ?: "no-x-request-id"; $this->logger = $logger; } @@ -112,6 +111,8 @@ class Client $headers = [ 'client_app' => $this->config['app'], + 'client_host' => gethostname(), + 'client_addr' => $_SERVER['SERVER_ADDR'], ]; $resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [ 'headers' => $headers, @@ -126,7 +127,6 @@ class Client if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) { throw new RpcServerException($body['error']['message'], $body['error']['code']); } - $this->logger->info('MONITOR',compact("payload", "body", "headers")); return $body['result']; } catch (\InvalidArgumentException $e) { @@ -141,7 +141,7 @@ class Client protected function id() { // return $this->id.'-'.time(); - return $this->id; + return $this->id; } } \ No newline at end of file diff --git a/src/Providers/ClientServiceProvider.php b/src/Providers/ClientServiceProvider.php index 3acac7e..454668c 100644 --- a/src/Providers/ClientServiceProvider.php +++ b/src/Providers/ClientServiceProvider.php @@ -4,6 +4,7 @@ namespace JsonRpc\Providers; use Illuminate\Support\ServiceProvider; use JsonRpc\Client; +use JsonRpc\Exception\RpcServerException; class ClientServiceProvider extends ServiceProvider @@ -19,7 +20,9 @@ class ClientServiceProvider extends ServiceProvider { $this->app->configure('rpc'); $config = config('rpc'); - + if (!is_array($config)) { + throw new RpcServerException("Application's Rpc Config Undefind", 500); + } $this->app->singleton('rpc', function () use ($config) { return new Client($config); }); diff --git a/src/Server/JsonRpcMethod.php b/src/Server/JsonRpcMethod.php index a460436..b5abd77 100644 --- a/src/Server/JsonRpcMethod.php +++ b/src/Server/JsonRpcMethod.php @@ -24,13 +24,19 @@ class JsonRpcMethod ]); } + /** + * + * @param $code + * @param $msg + * @return static + */ public function error($code, $msg) { return JsonResponse::create([ 'jsonrpc' => '2.0', 'error' => [ 'code' => $code, - 'message' => $msg, + 'message' => is_array($msg) ? json_encode($msg) : $msg, ], 'id' => $this->id ]); diff --git a/src/Server/JsonRpcServer.php b/src/Server/JsonRpcServer.php index c51c107..01f59a9 100644 --- a/src/Server/JsonRpcServer.php +++ b/src/Server/JsonRpcServer.php @@ -13,6 +13,7 @@ class JsonRpcServer const Rpc_Error_NOT_FOUND = -32601; const Rpc_Error_Invalid_Params = -32602; const Rpc_Error_Internal_Error = -32603; + const Rpc_Success = 0; const ErrorMsg = [ @@ -20,6 +21,7 @@ class JsonRpcServer self::Rpc_Error_Parse_Error => 'Json parse error', self::Rpc_Error_Invalid_Request => 'Invalid request', self::Rpc_Error_Invalid_Params => 'Invalid params', + self::Rpc_Success => 'Success' ]; /** @@ -37,9 +39,9 @@ class JsonRpcServer public function handler() { -// if ($this->request->getContentType() != 'json') { -// return $this->error(self::Rpc_Error_Invalid_Request); -// } + if ($this->request->getContentType() != 'json') { + return $this->error(self::Rpc_Error_Invalid_Request); + } try { @@ -50,9 +52,8 @@ class JsonRpcServer } else { list($method, $params, $id) = $this->parseJson($this->request->getContent()); } - app('log')->info('rpc ser', [$method, $params, $id, $this->request->header('client_app')]); + list($class, $function) = $this->parseMethodWithMap($method); -// dump($class,$function);exit; if (!class_exists($class) || !method_exists($class, $function)) { return $this->error(self::Rpc_Error_NOT_FOUND); @@ -62,7 +63,7 @@ class JsonRpcServer return $this->error(self::Rpc_Error_Invalid_Params); } - + app('log')->info('rpc ser', [$method, $params, $id, $class, $this->request->header('client_app')]); $ret = call_user_func_array([(new $class($id, $this->request)), $function], $params); return $ret; @@ -139,6 +140,4 @@ class JsonRpcServer 'id' => $id ]); } - - } \ No newline at end of file diff --git a/src/Server/JsonRpcServerDoc.php b/src/Server/JsonRpcServerDoc.php index 14af898..1dc1f6a 100644 --- a/src/Server/JsonRpcServerDoc.php +++ b/src/Server/JsonRpcServerDoc.php @@ -29,9 +29,6 @@ class JsonRpcDoc public function render() { - /** - * @var $view Factory - */ $config = [ 'class' => $this->methods(), 'filter_method' => [], @@ -39,9 +36,6 @@ class JsonRpcDoc $api = new BootstrapApiDoc($config); $doc = $api->getHtml(); exit($doc); -// \View::addExtension('html','php'); -// app('view')->addExtension('html', 'php'); -// echo app('view')->file(__DIR__."/../Sami/document/index.html"); } protected function desc($class, $method)