Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dispatcher error messages #57

Merged
merged 3 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions phpstan.neon
Expand Up @@ -19,6 +19,10 @@ parameters:
- '#^Return typehint of method Apitte\\Core\\Http\\ApiResponse::getEntity\(\) has invalid type Apitte\\Negotiation\\Http\\AbstractEntity.$#'
- '#^Parameter \$entity of method Apitte\\Core\\Http\\ApiResponse::withEntity\(\) has invalid typehint type Apitte\\Negotiation\\Http\\AbstractEntity.$#'
- '#^Call to static method from\(\) on an unknown class Apitte\\Negotiation\\Http\\#'
- '#^Class Apitte\\Negotiation\\Http\\ArrayEntity not found.$#'

# Maybe will cause application fail - DecoratorManager::decorateResponse could return null, but DecoratedDispatcher is not always able to handle it.
- '#Method Apitte\\Core\\Dispatcher\\DecoratedDispatcher::handle\(\) should return Psr\\Http\\Message\\ResponseInterface but returns Psr\\Http\\Message\\ResponseInterface|null.#'

# DecoratedDispatcher
- '#Call to an undefined method Psr\\Http\\Message\\ResponseInterface::withEntity\(\)#'
2 changes: 1 addition & 1 deletion src/Dispatcher/CoreDispatcher.php
Expand Up @@ -49,7 +49,7 @@ protected function handle(ServerRequestInterface $request, ResponseInterface $re

// Validate if response is ResponseInterface
if (!($response instanceof ResponseInterface)) {
throw new InvalidStateException(sprintf('Handler returned response must implement "%s"', ResponseInterface::class));
throw new InvalidStateException(sprintf('Endpoint returned response must implement "%s"', ResponseInterface::class));
}

return $response;
Expand Down
42 changes: 31 additions & 11 deletions src/Dispatcher/DecoratedDispatcher.php
Expand Up @@ -95,19 +95,15 @@ protected function handle(ServerRequestInterface $request, ResponseInterface $re
// if result is scalar convert it manually to ScalarEntity,
// if result is IResponseEntity convert it manually to MappingEntity,
// otherwise use result as response
if (is_array($result)) {
$response = $response->withEntity(ArrayEntity::from($result));
} elseif (is_scalar($result)) {
$response = $response->withEntity(ScalarEntity::from($result));
} elseif ($result instanceof IResponseEntity) {
$response = $response->withEntity(MappingEntity::from($result));
if (is_array($result) || is_scalar($result) || $result instanceof IResponseEntity) {
$response = $this->negotiate($result, $response);
} else {
$response = $result;
}
// Validate if response is ResponseInterface
if (!($result instanceof ResponseInterface)) {
throw new InvalidStateException(sprintf('Endpoint returned response must implement "%s"', ResponseInterface::class));
}

// Validate if response is ResponseInterface
if (!($response instanceof ResponseInterface)) {
throw new InvalidStateException(sprintf('Handler returned response must implement "%s"', ResponseInterface::class));
$response = $result;
}

try {
Expand All @@ -120,4 +116,28 @@ protected function handle(ServerRequestInterface $request, ResponseInterface $re
return $response;
}

/**
* @param mixed $result
* @param ApiResponse|ResponseInterface $response
*/
protected function negotiate($result, ResponseInterface $response): ApiResponse
{
if (!class_exists(ArrayEntity::class)) {
throw new InvalidStateException(sprintf(
'If you want return anything else than "%s" from your api endpoint then install "apitte/negotiation".',
ResponseInterface::class
));
}

if (is_array($result)) {
$response = $response->withEntity(ArrayEntity::from($result));
} elseif (is_scalar($result)) {
$response = $response->withEntity(ScalarEntity::from($result));
} elseif ($result instanceof IResponseEntity) {
$response = $response->withEntity(MappingEntity::from($result));
}

return $response;
}

}
2 changes: 1 addition & 1 deletion src/Dispatcher/JsonDispatcher.php
Expand Up @@ -26,7 +26,7 @@ protected function handle(ServerRequestInterface $request, ResponseInterface $re

// Validate if response is ResponseInterface
if (!($response instanceof ResponseInterface)) {
throw new InvalidStateException(sprintf('Handler returned response must implement "%s"', ResponseInterface::class));
throw new InvalidStateException(sprintf('Endpoint returned response must implement "%s"', ResponseInterface::class));
}

return $response;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/Dispatcher/CoreDispatcher.phpt
Expand Up @@ -35,7 +35,7 @@ test(function (): void {

Assert::exception(function () use ($dispatcher, $request, $response): void {
$dispatcher->dispatch($request, $response);
}, InvalidStateException::class, sprintf('Handler returned response must implement "%s"', ResponseInterface::class));
}, InvalidStateException::class, sprintf('Endpoint returned response must implement "%s"', ResponseInterface::class));
});

// Request not matched, throw exception
Expand Down
15 changes: 14 additions & 1 deletion tests/cases/Dispatcher/DecoratedDispatcher.phpt
Expand Up @@ -25,6 +25,7 @@ use Tests\Fixtures\Decorator\ThrowExceptionFromContextResponseDecorator;
use Tests\Fixtures\Handler\ErroneousHandler;
use Tests\Fixtures\Handler\FakeNullHandler;
use Tests\Fixtures\Handler\FakeResponseHandler;
use Tests\Fixtures\Handler\ReturnFooBarHandler;
use Tests\Fixtures\Router\FakeRouter;

//TODO - EarlyReturnResponseException
Expand Down Expand Up @@ -63,7 +64,19 @@ test(function (): void {

Assert::exception(function () use ($dispatcher, $request, $response): void {
$dispatcher->dispatch($request, $response);
}, InvalidStateException::class, sprintf('Handler returned response must implement "%s"', ResponseInterface::class));
}, InvalidStateException::class, sprintf('Endpoint returned response must implement "%s"', ResponseInterface::class));
});

// Match request, use invalid handler, throw exception
test(function (): void {
$request = Psr7ServerRequestFactory::fromSuperGlobal();
$response = Psr7ResponseFactory::fromGlobal();

$dispatcher = new DecoratedDispatcher(new FakeRouter(true), new ReturnFooBarHandler(), new DecoratorManager());

Assert::exception(function () use ($dispatcher, $request, $response): void {
$dispatcher->dispatch($request, $response);
}, InvalidStateException::class, sprintf('If you want return anything else than "%s" from your api endpoint then install "apitte/negotiation".', ResponseInterface::class));
});

// Match request, decorate request, throw exception, return response from exception
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/Dispatcher/JsonDispatcher.phpt
Expand Up @@ -47,7 +47,7 @@ test(function (): void {
$dispatcher = new JsonDispatcher(new FakeRouter(true), new FakeNullHandler());
Assert::exception(function () use ($dispatcher, $request, $response): void {
$dispatcher->dispatch($request, $response);
}, InvalidStateException::class, sprintf('Handler returned response must implement "%s"', ResponseInterface::class));
}, InvalidStateException::class, sprintf('Endpoint returned response must implement "%s"', ResponseInterface::class));
});

// Not matched, use fallback, write error to response body
Expand Down