Skip to content

Commit

Permalink
Added more error information on the call. Fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
mbonneau committed Jan 28, 2016
1 parent cc419c3 commit f9fbcb0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
19 changes: 12 additions & 7 deletions src/WampPost/WampPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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']);
Expand Down
30 changes: 26 additions & 4 deletions tests/Functional/WampPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use React\HttpClient\Response;
use Thruway\ClientSession;
use Thruway\Exception\WampErrorException;
use Thruway\Message\EventMessage;
use WampPost\WampPost;

Expand All @@ -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);
Expand Down Expand Up @@ -148,10 +152,6 @@ function testPublishBadUri()
$this->assertEvents([], $events);
}

public function testBadPath()
{
}

public function testNoTopicPublish()
{
/** @var Response $response */
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f9fbcb0

Please sign in to comment.