Skip to content

Commit

Permalink
Refactored HttpException
Browse files Browse the repository at this point in the history
  • Loading branch information
dkraczkowski committed Sep 13, 2018
1 parent 4e4356c commit ccb5039
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Exception/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface HttpException extends NetworkException
{
public function asResponse(): ResponseInterface;
public function toResponse(): ResponseInterface;
}
2 changes: 1 addition & 1 deletion src/Exception/RouterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function invalidRoute($given): self
return $exception;
}

public function asResponse(): ResponseInterface
public function toResponse(): ResponseInterface
{
return Response::asText($this->getMessage(), $this->httpStatus);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/ErrorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

if ($exception instanceof HttpException) {
$response = $exception->asResponse();
$response = $exception->toResponse();
} else {
$response = Response::asText($exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Server/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function createOnRequestListener(): void
$psrResponse = $listener->onRequest($this->getClient($request->fd), $psrRequest, $psrResponse);
}
} catch (HttpException $exception) {
$psrResponse = $exception->asResponse();
$psrResponse = $exception->toResponse();
} catch (\Throwable $throwable) {
$this->logger->error($throwable->getMessage());
$psrResponse = Response::empty(Response::HTTP_INTERNAL_SERVER_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/CustomHttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(string $message = "", int $code = 0, Throwable $prev
parent::__construct($message, $code, $previous);
}

public function asResponse(): ResponseInterface
public function toResponse(): ResponseInterface
{
return Response::asJson([
'error_code' => $this->getCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface

$response = $middleware->process(Mockery::mock(ServerRequestInterface::class), $requestHandler);

self::assertSame($error->asResponse()->getStatusCode(), $response->getStatusCode());
self::assertSame((string) $error->asResponse()->getBody(), (string) $response->getBody());
self::assertSame($error->toResponse()->getStatusCode(), $response->getStatusCode());
self::assertSame((string) $error->toResponse()->getBody(), (string) $response->getBody());
}
}

0 comments on commit ccb5039

Please sign in to comment.