comparison core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\jsonapi\EventSubscriber;
4
5 use Drupal\jsonapi\JsonApiResource\ErrorCollection;
6 use Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel;
7 use Drupal\jsonapi\JsonApiResource\LinkCollection;
8 use Drupal\jsonapi\JsonApiResource\NullIncludedData;
9 use Drupal\jsonapi\ResourceResponse;
10 use Drupal\jsonapi\Routing\Routes;
11 use Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber as SerializationDefaultExceptionSubscriber;
12 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13 use Symfony\Component\HttpKernel\Exception\HttpException;
14
15 /**
16 * Serializes exceptions in compliance with the JSON:API specification.
17 *
18 * @internal JSON:API maintains no PHP API. The API is the HTTP API. This class
19 * may change at any time and could break any dependencies on it.
20 *
21 * @see https://www.drupal.org/project/jsonapi/issues/3032787
22 * @see jsonapi.api.php
23 */
24 class DefaultExceptionSubscriber extends SerializationDefaultExceptionSubscriber {
25
26 /**
27 * {@inheritdoc}
28 */
29 protected static function getPriority() {
30 return parent::getPriority() + 25;
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 protected function getHandledFormats() {
37 return ['api_json'];
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function onException(GetResponseForExceptionEvent $event) {
44 if (!$this->isJsonApiExceptionEvent($event)) {
45 return;
46 }
47 if (($exception = $event->getException()) && !$exception instanceof HttpException) {
48 $exception = new HttpException(500, $exception->getMessage(), $exception);
49 $event->setException($exception);
50 }
51
52 $this->setEventResponse($event, $exception->getStatusCode());
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 protected function setEventResponse(GetResponseForExceptionEvent $event, $status) {
59 /* @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
60 $exception = $event->getException();
61 $response = new ResourceResponse(new JsonApiDocumentTopLevel(new ErrorCollection([$exception]), new NullIncludedData(), new LinkCollection([])), $exception->getStatusCode(), $exception->getHeaders());
62 $response->addCacheableDependency($exception);
63 $event->setResponse($response);
64 }
65
66 /**
67 * Check if the error should be formatted using JSON:API.
68 *
69 * The JSON:API format is supported if the format is explicitly set or the
70 * request is for a known JSON:API route.
71 *
72 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $exception_event
73 * The exception event.
74 *
75 * @return bool
76 * TRUE if it needs to be formatted using JSON:API. FALSE otherwise.
77 */
78 protected function isJsonApiExceptionEvent(GetResponseForExceptionEvent $exception_event) {
79 $request = $exception_event->getRequest();
80 $parameters = $request->attributes->all();
81 return $request->getRequestFormat() === 'api_json' || (bool) Routes::getResourceTypeNameFromParameters($parameters);
82 }
83
84 }