Cache API errors at the dispatcher level, so a more appropriate error page can be displayed

This commit is contained in:
Timothy Warren 2017-01-13 16:52:12 -05:00
parent 79c0c6cf90
commit 3e72a66297

View File

@ -18,6 +18,7 @@ namespace Aviat\AnimeClient;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Friend;
use GuzzleHttp\Exception\ServerException;
/**
* Basic routing/ dispatch
@ -129,9 +130,23 @@ class Dispatcher extends RoutingBase {
$params = $error_route['params'];
}
// Try to catch API errors in a presentable fashion
try
{
// Actually instantiate the controller
$this->call($controllerName, $actionMethod, $params);
}
catch (ServerException $e)
{
$response = $e->getResponse();
$this->call(AnimeClient::DEFAULT_CONTROLLER, AnimeClient::ERROR_MESSAGE_METHOD, [
$response->getStatusCode(),
'API Error',
'There was a problem getting data from an external source.',
(string) $response->getBody()
]);
}
}
/**
* Parse out the arguments for the appropriate controller for