wotapi-8.x-1.x-dev/src/EventSubscriber/DefaultExceptionSubscriber.php
src/EventSubscriber/DefaultExceptionSubscriber.php
<?php
namespace Drupal\wotapi\EventSubscriber;
use Drupal\wotapi\WotApiResource\ErrorCollection;
use Drupal\wotapi\WotApiResource\WotApiDocumentTopLevel;
use Drupal\wotapi\WotApiResource\LinkCollection;
use Drupal\wotapi\ResourceResponse;
use Drupal\wotapi\Routing\Routes;
use Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber as SerializationDefaultExceptionSubscriber;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* Serializes exceptions in compliance with the WOT:API specification.
*
* @internal WOT:API maintains no PHP API. The API is the HTTP API. This class
* may change at any time and could break any dependencies on it.
*/
class DefaultExceptionSubscriber extends SerializationDefaultExceptionSubscriber {
/**
* {@inheritdoc}
*/
protected static function getPriority() {
return parent::getPriority() + 25;
}
/**
* {@inheritdoc}
*/
protected function getHandledFormats() {
return ['api_json'];
}
/**
* {@inheritdoc}
*/
public function onException(ExceptionEvent $event) {
if (!$this->isWotApiExceptionEvent($event)) {
return;
}
$exception = $event->getThrowable();
if (!$exception instanceof HttpException) {
$exception = new HttpException(500, $exception->getMessage(), $exception);
$event->setThrowable($exception);
}
$this->setEventResponse($event, $exception->getStatusCode());
}
/**
* {@inheritdoc}
*/
protected function setEventResponse(ExceptionEvent $event, $status) {
/* @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
$exception = $event->getThrowable();
// if ($exception instanceof HttpExceptionInterface && $event->getThrowable() === $exception) {
// $exception->setStatusCode($status);
// }
$response = new ResourceResponse(new WotApiDocumentTopLevel(new ErrorCollection([$exception]), new LinkCollection([])), $exception->getStatusCode(), $exception->getHeaders());
$response->addCacheableDependency($exception);
$event->setResponse($response);
}
/**
* Check if the error should be formatted using WOT:API.
*
* The WOT:API format is supported if the format is explicitly set or the
* request is for a known WOT:API route.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $exception_event
* The exception event.
*
* @return bool
* TRUE if it needs to be formatted using WOT:API. FALSE otherwise.
*/
protected function isWotApiExceptionEvent(ExceptionEvent $exception_event) {
$request = $exception_event->getRequest();
$parameters = $request->attributes->all();
return $request->getRequestFormat() === 'api_json' || (bool) Routes::getResourceTypeNameFromParameters($parameters);
}
}
