rpc exception

This commit is contained in:
dongwei 2019-01-17 14:10:18 +08:00
parent 4418e004ad
commit 86fbc11fd9
5 changed files with 24 additions and 22 deletions

View File

@ -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;
}
}

View File

@ -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);
});

View File

@ -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
]);

View File

@ -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
]);
}
}

View File

@ -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)