Mercurial > hg > isophonics-drupal-site
comparison core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\serialization\EventSubscriber; | 3 namespace Drupal\serialization\EventSubscriber; |
4 | 4 |
5 use Drupal\Core\Cache\CacheableDependencyInterface; | |
6 use Drupal\Core\Cache\CacheableResponse; | |
5 use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase; | 7 use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase; |
6 use Symfony\Component\HttpFoundation\Response; | 8 use Symfony\Component\HttpFoundation\Response; |
7 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | 9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
8 use Symfony\Component\Serializer\SerializerInterface; | 10 use Symfony\Component\Serializer\SerializerInterface; |
9 | 11 |
49 /** | 51 /** |
50 * {@inheritdoc} | 52 * {@inheritdoc} |
51 */ | 53 */ |
52 protected static function getPriority() { | 54 protected static function getPriority() { |
53 // This will fire after the most common HTML handler, since HTML requests | 55 // This will fire after the most common HTML handler, since HTML requests |
54 // are still more common than HTTP requests. | 56 // are still more common than HTTP requests. But it has a lower priority |
55 return -75; | 57 // than \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber::on4xx(), so |
58 // that this also handles the 'json' format. Then all serialization formats | |
59 // (::getHandledFormats()) are handled by this exception subscriber, which | |
60 // results in better consistency. | |
61 return -70; | |
56 } | 62 } |
57 | 63 |
58 /** | 64 /** |
59 * Handles all 4xx errors for all serialization failures. | 65 * Handles all 4xx errors for all serialization failures. |
60 * | 66 * |
65 /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */ | 71 /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */ |
66 $exception = $event->getException(); | 72 $exception = $event->getException(); |
67 $request = $event->getRequest(); | 73 $request = $event->getRequest(); |
68 | 74 |
69 $format = $request->getRequestFormat(); | 75 $format = $request->getRequestFormat(); |
70 $content = ['message' => $event->getException()->getMessage()]; | 76 $content = ['message' => $exception->getMessage()]; |
71 $encoded_content = $this->serializer->serialize($content, $format); | 77 $encoded_content = $this->serializer->serialize($content, $format); |
72 $headers = $exception->getHeaders(); | 78 $headers = $exception->getHeaders(); |
73 | 79 |
74 // Add the MIME type from the request to send back in the header. | 80 // Add the MIME type from the request to send back in the header. |
75 $headers['Content-Type'] = $request->getMimeType($format); | 81 $headers['Content-Type'] = $request->getMimeType($format); |
76 | 82 |
77 $response = new Response($encoded_content, $exception->getStatusCode(), $headers); | 83 // If the exception is cacheable, generate a cacheable response. |
84 if ($exception instanceof CacheableDependencyInterface) { | |
85 $response = new CacheableResponse($encoded_content, $exception->getStatusCode(), $headers); | |
86 $response->addCacheableDependency($exception); | |
87 } | |
88 else { | |
89 $response = new Response($encoded_content, $exception->getStatusCode(), $headers); | |
90 } | |
91 | |
78 $event->setResponse($response); | 92 $event->setResponse($response); |
79 } | 93 } |
80 | 94 |
81 } | 95 } |