首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
jsonrpc 自定义异常的错误 code
目前 jsonrpc 中抛出异常的错误 code 是写死的 `vendor/hyperf/json-rpc/src/CoreMiddleware.php` ```php protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request) { if ($dispatched->handler->callback instanceof Closure) { $response = call($dispatched->handler->callback); } else { [$controller, $action] = $this->prepareHandler($dispatched->handler->callback); $controllerInstance = $this->container->get($controller); if (!method_exists($controller, $action)) { // Route found, but the handler does not exist. return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::INTERNAL_ERROR); } $parameters = $this->parseParameters($controller, $action, $request->getParsedBody()); try { $response = $controllerInstance->{$action}(...$parameters); } catch (\Throwable $exception) { $response = $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::SERVER_ERROR, $exception); $this->responseBuilder->persistToContext($response); throw $exception; } } return $response; } ``` `ResponseBuilder::SERVER_ERROR` 常量写死了,当然我可以通过 `AppExceptionHandler` 去捕获这个异常,这时候`response body` 是一个`jsonrpc2.0` 协议的格式 ```json { "jsonrpc": "2.0", "id": "5e6b6a3672b73", "error": { "code": -32000, "message": "xxxx", "data": { "class": "App\\Exception\\ServiceException", "attributes": { "message": "xxxx", "code": 40001, "file": "/.../app/Rpc/V1/User.php", "line": 38 } } }, "context": [] } ``` **我目前的解决方法是强行 decode ,再 encode ** ,有没有更加简洁的方式 ------- 我看底层是通过 `ResponseBuilder` 类去处理的 `vendor/hyperf/json-rpc/src/TcpServer.php` ``` protected function initProtocol() { $protocol = 'jsonrpc'; if ($this->isLengthCheck()) { $protocol = 'jsonrpc-tcp-length-check'; } $this->protocol = new Protocol($this->container, $this->protocolManager, $protocol, $this->serverConfig); $this->packer = $this->protocol->getPacker(); $this->responseBuilder = make(ResponseBuilder::class, [ 'dataFormatter' => $this->protocol->getDataFormatter(), 'packer' => $this->packer, ]); } ```
发布于4年前 · 2 次浏览 · 来自
开发框架
明志
目前 jsonrpc 中抛出异常的错误 code 是写死的 `vendor/hyperf/json-rpc/src/CoreMiddleware.php` ```php protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request) { if ($dispatched->handler->callback instanceof Closure) { $response = call($dispatched->handler->callback); } else { [$controller, $action] = $this->prepareHandler($dispatched->handler->callback); $controllerInstance = $this->container->get($controller); if (!method_exists($controller, $action)) { // Route found, but the handler does not exist. return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::INTERNAL_ERROR); } $parameters = $this->parseParameters($controller, $action, $request->getParsedBody()); try { $response = $controllerInstance->{$action}(...$parameters); } catch (\Throwable $exception) { $response = $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::SERVER_ERROR, $exception); $this->responseBuilder->persistToContext($response); throw $exception; } } return $response; } ``` `ResponseBuilder::SERVER_ERROR` 常量写死了,当然我可以通过 `AppExceptionHandler` 去捕获这个异常,这时候`response body` 是一个`jsonrpc2.0` 协议的格式 ```json { "jsonrpc": "2.0", "id": "5e6b6a3672b73", "error": { "code": -32000, "message": "xxxx", "data": { "class": "App\\Exception\\ServiceException", "attributes": { "message": "xxxx", "code": 40001, "file": "/.../app/Rpc/V1/User.php", "line": 38 } } }, "context": [] } ``` **我目前的解决方法是强行 decode ,再 encode ** ,有没有更加简洁的方式 ------- 我看底层是通过 `ResponseBuilder` 类去处理的 `vendor/hyperf/json-rpc/src/TcpServer.php` ``` protected function initProtocol() { $protocol = 'jsonrpc'; if ($this->isLengthCheck()) { $protocol = 'jsonrpc-tcp-length-check'; } $this->protocol = new Protocol($this->container, $this->protocolManager, $protocol, $this->serverConfig); $this->packer = $this->protocol->getPacker(); $this->responseBuilder = make(ResponseBuilder::class, [ 'dataFormatter' => $this->protocol->getDataFormatter(), 'packer' => $this->packer, ]); } ```
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2020-03-14
明志
看了一遍协议:https://wiki.geekdream.com/Specification/json-rpc_2.0.html 大概明白为什么这样做的,我自己抛出的业务,可以再通过客户端做处理 `Hyperf\RpcClient\Exception\RequestException` ```php /** * [@var ](/u/3588)RequestException $throwable */ $result = [ 'code' => $throwable->getCode(), 'message' => [ 'text' => $throwable->getMessage(), 'action' => 'toast', ], 'result' => [ 'e' => get_class($throwable), 'data' => (array)$throwable->getThrowable()['attributes'], ] ]; ```
赞
1
回复
2020-03-14
明志
重写了自定义 `RpcValidationExceptionHandler`,用来处理指定的错误异常 ```php /** * @param Throwable $throwable * @param ResponseInterface $response * @return ResponseInterface */ public function handle(Throwable $throwable, ResponseInterface $response) { $this->stopPropagation(); /** * [[@var ](/u/3588)](/u/3588)ValidationException $throwable */ $body = $throwable->validator->errors()->first(); // $result = $this->serviceResponse->showError($body, ['data' => $throwable->validator->errors()->all()]); $container= ApplicationContext::getContainer(); /** * [[@var ](/u/3588)](/u/3588)ResponseBuilder $responseBuilder */ $responseBuilder = make(ResponseBuilder::class, [ 'dataFormatter' => ApplicationContext::getContainer()->get(DataFormatter::class), 'packer' => ApplicationContext::getContainer()->get(JsonEofPacker::class), ]); return $responseBuilder->buildErrorResponse(Context::get(ServerRequestInterface::class), ResponseBuilder::SERVER_ERROR, $throwable); } ```
赞
0
回复
微信公众号
热门内容
作者其它话题
- 关于 swoole 使用 systemctl 重启问题
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。