From f9fbcb0154dfecacb5b515e37bbfc327e5a48f03 Mon Sep 17 00:00:00 2001 From: Matt Bonneau Date: Thu, 28 Jan 2016 16:22:45 -0500 Subject: [PATCH] Added more error information on the call. Fixes #8 --- src/WampPost/WampPost.php | 19 ++++++++++++------- tests/Functional/WampPostTest.php | 30 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/WampPost/WampPost.php b/src/WampPost/WampPost.php index 73618cb..45ffb24 100644 --- a/src/WampPost/WampPost.php +++ b/src/WampPost/WampPost.php @@ -8,6 +8,7 @@ use React\Socket\Server; use Thruway\CallResult; use Thruway\Common\Utils; +use Thruway\Message\ErrorMessage; use Thruway\Peer\Client; class WampPost extends Client { @@ -129,18 +130,22 @@ private function handleCallHttpRequest($request, $response) { $this->getSession()->call($json->procedure, $args, $argsKw, $options)->then( /** @param CallResult $result */ function (CallResult $result) use ($response) { - $responseObj = new \stdClass(); - $responseObj->result = "SUCCESS"; - $responseObj->args = $result->getArguments(); - $responseObj->argsKw = $result->getArgumentsKw(); + $responseObj = new \stdClass(); + $responseObj->result = "SUCCESS"; + $responseObj->args = $result->getArguments(); + $responseObj->argsKw = $result->getArgumentsKw(); $responseObj->details = $result->getDetails(); $response->writeHead(200, ['Content-Type' => 'application/json', 'Connection' => 'close']); $response->end(json_encode($responseObj)); }, - function ($result) use ($response) { - $responseObj = new \stdClass(); - $responseObj->result = "ERROR"; + function (ErrorMessage $msg) use ($response) { + $responseObj = new \stdClass(); + $responseObj->result = "ERROR"; + $responseObj->error_uri = $msg->getErrorURI(); + $responseObj->error_args = $msg->getArguments(); + $responseObj->error_argskw = $msg->getArgumentsKw(); + $responseObj->error_details = $msg->getDetails(); // maybe return an error code here $response->writeHead(200, ['Content-Type' => 'application/json', 'Connection' => 'close']); diff --git a/tests/Functional/WampPostTest.php b/tests/Functional/WampPostTest.php index 8f1cf22..d41dcd6 100644 --- a/tests/Functional/WampPostTest.php +++ b/tests/Functional/WampPostTest.php @@ -4,6 +4,7 @@ use React\HttpClient\Response; use Thruway\ClientSession; +use Thruway\Exception\WampErrorException; use Thruway\Message\EventMessage; use WampPost\WampPost; @@ -19,6 +20,9 @@ private function runTestWith($method, $url, array $headers = [], $protocolVersio $wampPost->on('open', function (ClientSession $session) use (&$opened, $router) { $opened = true; + $session->register("procedure.that.errors", function () { + throw new WampErrorException("my.custom.error", [4,5,6], (object)["x"=>"y"], (object)["y"=>"z"]); + }); }); $router->addInternalClient($wampPost); @@ -148,10 +152,6 @@ function testPublishBadUri() $this->assertEvents([], $events); } - public function testBadPath() - { - } - public function testNoTopicPublish() { /** @var Response $response */ @@ -210,4 +210,26 @@ public function testGet() $this->assertEvents([], $events); } + + public function testCallWithError() + { + /** @var Response $response */ + list($response, $responseBody, $events) = $this->runTestWith( + "POST", + "http://127.0.0.1:18181/call", + [], + '1.0', + json_encode( + [ + "procedure" => "procedure.that.errors", + "args" => [1,2,3] + ] + ) + ); + + $this->assertEquals($responseBody, "78\r\n{\"result\":\"ERROR\",\"error_uri\":\"my.custom.error\",\"error_args\":[4,5,6],\"error_argskw\":{\"x\":\"y\"},\"error_details\":{\"y\":\"z\"}}\r\n0\r\n\r\n"); + $this->assertEquals(200, $response->getCode()); + + $this->assertEvents([], $events); + } } \ No newline at end of file