private function createErrorResponse(int $code, Socket $socket) : \Generator
{
try {
yield from $this->log->log(Log::NOTICE, 'Error reading request from %s:%d (Status Code: %d)', $socket->getRemoteAddress(), $socket->getRemotePort(), $code);
$response = $this->handler->onError($code, $socket);
if ($response instanceof \Generator) {
$response = (yield from $response);
} elseif ($response instanceof Awaitable) {
$response = (yield $response);
}
if (!$response instanceof Response) {
throw new InvalidResultError(sprintf('A %s object was not returned from %::onError().', Response::class, get_class($this->handler)), $response);
}
} catch (Throwable $exception) {
yield from $this->log->log(Log::ERROR, "Uncaught exception when creating response to an error from %s:%d on %s:%d in file %s on line %d: %s", $socket->getRemoteAddress(), $socket->getRemotePort(), $socket->getLocalAddress(), $socket->getLocalPort(), $exception->getFile(), $exception->getLine(), $exception->getMessage());
$response = (yield from $this->createDefaultErrorResponse(500));
}
return $response;
}