Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle Exception in callback function
  • Loading branch information
legionth committed Mar 30, 2017
commit 909b06ccda46eea46eb8d7df674c1130a5deee94
19 changes: 14 additions & 5 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,29 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
);

$callback = $this->callback;
$promise = \React\Promise\resolve($callback($request));
$promise = new Promise(function ($resolve, $reject) use ($callback, $request) {
$resolve($callback($request));
});

$that = $this;
$promise->then(
function ($response) use ($that, $conn, $request) {
if (!$response instanceof ResponseInterface) {
$that->emit('error', array(new \InvalidArgumentException('Invalid response type')));
$message = 'The response callback is expected to resolve with an object implementing Psr\Http\Message\ResponseInterface, but resolved with "%s" instead.';
$message = sprintf($message, is_object($response) ? get_class($response) : gettype($response));
$exception = new \RuntimeException($message);

$that->emit('error', array($exception));
return $that->writeError($conn, 500);
}
$that->handleResponse($conn, $response, $request->getProtocolVersion());
},
function ($ex) use ($that, $conn) {
$that->emit('error', array($ex));
function ($error) use ($that, $conn) {
$message = 'The response callback is expected to resolve with an object implementing Psr\Http\Message\ResponseInterface, but rejected with "%s" instead.';
$message = sprintf($message, is_object($error) ? get_class($error) : gettype($error));
$exception = new \RuntimeException($message, null, $error instanceof \Exception ? $error : null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed \Throwable support here, opened #155.


$that->emit('error', array($exception));
return $that->writeError($conn, 500);
}
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must also handle a rejected promise returned from $callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Added something to cover this. Have a look 👍

Expand All @@ -264,7 +274,6 @@ public function writeError(ConnectionInterface $conn, $code)
$response = new Response(
$code,
array(
'Content-Length' => strlen($message),
'Content-Type' => 'text/plain'
),
$message
Expand Down
108 changes: 108 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,11 @@ public function testReturnInvalidTypeWillResultInError()
return "invalid";
});

$exception = null;
$server->on('error', function (\Exception $ex) use (&$exception) {
$exception = $ex;
});

$buffer = '';
$this->connection
->expects($this->any())
Expand All @@ -1678,6 +1683,7 @@ function ($data) use (&$buffer) {
$this->connection->emit('data', array($data));

$this->assertContains("HTTP/1.1 500 Internal Server Error\r\n", $buffer);
$this->assertInstanceOf('RuntimeException', $exception);
}

public function testResolveWrongTypeInPromiseWillResultInError()
Expand Down Expand Up @@ -1806,6 +1812,108 @@ function ($data) use (&$buffer) {
$this->assertNotContains("Transfer-Encoding", $buffer);
}

public function testReturnRequestWillBeHandled()
{
$server = new Server($this->socket, function (RequestInterface $request) {
return new Response();
});

$buffer = '';
$this->connection
->expects($this->any())
->method('write')
->will(
$this->returnCallback(
function ($data) use (&$buffer) {
$buffer .= $data;
}
)
);

$this->socket->emit('connection', array($this->connection));

$data = "GET / HTTP/1.0\r\n\r\n";

$data = $this->createGetRequest();

$this->connection->emit('data', array($data));

$this->assertContains("HTTP/1.1 200 OK\r\n", $buffer);
}

public function testExceptionThrowInCallBackFunctionWillResultInErrorMessage()
{
$server = new Server($this->socket, function (RequestInterface $request) {
throw new \Exception('hello');
});

$exception = null;
$server->on('error', function (\Exception $ex) use (&$exception) {
$exception = $ex;
});

$buffer = '';
$this->connection
->expects($this->any())
->method('write')
->will(
$this->returnCallback(
function ($data) use (&$buffer) {
$buffer .= $data;
}
)
);

$this->socket->emit('connection', array($this->connection));

$data = "GET / HTTP/1.0\r\n\r\n";

$data = $this->createGetRequest();

$this->connection->emit('data', array($data));

$this->assertInstanceOf('RuntimeException', $exception);
$this->assertContains("HTTP/1.1 500 Internal Server Error\r\n", $buffer);
$this->assertEquals('hello', $exception->getPrevious()->getMessage());
}

public function testRejectOfNonExceptionWillResultInErrorMessage()
{
$server = new Server($this->socket, function (RequestInterface $request) {
return new Promise(function ($resolve, $reject) {
$reject('Invalid type');
});
});

$exception = null;
$server->on('error', function (\Exception $ex) use (&$exception) {
$exception = $ex;
});

$buffer = '';
$this->connection
->expects($this->any())
->method('write')
->will(
$this->returnCallback(
function ($data) use (&$buffer) {
$buffer .= $data;
}
)
);

$this->socket->emit('connection', array($this->connection));

$data = "GET / HTTP/1.0\r\n\r\n";

$data = $this->createGetRequest();

$this->connection->emit('data', array($data));

$this->assertContains("HTTP/1.1 500 Internal Server Error\r\n", $buffer);
$this->assertInstanceOf('RuntimeException', $exception);
}

private function createGetRequest()
{
$data = "GET / HTTP/1.1\r\n";
Expand Down