Commit d25fcbae authored by Crell's avatar Crell
Browse files

Convert exception handling from multiple listeners to a single subscriber...

Convert exception handling from multiple listeners to a single subscriber class for all response types.
parent 849e5bf3
......@@ -67,8 +67,6 @@ public function __construct(EventDispatcherInterface $dispatcher, ControllerReso
// Either way, treat it as a server-level error and return an HTTP 500.
// By default, this will be an HTML-type response because that's a decent
// best guess if we don't know otherwise.
$this->dispatcher->addSubscriber(new ExceptionListener(function(Exception $e) {
return new Response('A fatal error occurred: ' . $e->getMessage(), 500);
}));
$this->dispatcher->addSubscriber(new ExceptionListener(array(new ExceptionController(), 'execute')));
}
}
......@@ -38,79 +38,6 @@ protected function isHtmlRequestEvent(GetResponseEvent $event) {
return in_array('text/html', $acceptable_content_types) || in_array('*/*', $acceptable_content_types);
}
/**
* Processes an AccessDenied exception into an HTTP 403 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onAccessDeniedException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof AccessDeniedHttpException) {
$event->setResponse(new Response('Access Denied', 403));
}
}
/**
* Processes a NotFound exception into an HTTP 404 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onNotFoundHttpException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof NotFoundHttpException) {
watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
// Check for and return a fast 404 page if configured.
// @todo Inline this rather than using a function.
drupal_fast_404();
$system_path = $event->getRequest()->attributes->get('system_path');
// Keep old path for reference, and to allow forms to redirect to it.
if (!isset($_GET['destination'])) {
$_GET['destination'] = $system_path;
}
$path = drupal_get_normal_path(variable_get('site_404', ''));
if ($path && $path != $system_path) {
// @TODO: Um, how do I specify an override URL again? Totally not clear.
// Do that and sub-call the kernel rather than using meah().
$request = Request::create($path);
$kernel = new DrupalKernel();
$response = $kernel->handle($request, DrupalKernel::SUB_REQUEST);
$response->setStatusCode(404, 'Not Found');
}
else {
$response = new Response('Not Found', 404);
// @todo Replace this block with something cleaner.
$return = t('The requested page "@path" could not be found.', array('@path' => $event->getRequest()->getPathInfo()));
drupal_set_title(t('Page not found'));
drupal_set_page_content($return);
$page = element_info('page');
$content = drupal_render_page($page);
$response->setContent($content);
}
$event->setResponse($response);
}
}
/**
* Processes a MethodNotAllowed exception into an HTTP 405 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onMethodAllowedException(GetResponseEvent $event) {
if ($this->isHtmlRequestEvent($event) && $event->getException() instanceof MethodNotAllowedException) {
$event->setResponse(new Response('Method Not Allowed', 405));
}
}
/**
* Processes a successful controller into an HTTP 200 response.
*
......@@ -142,9 +69,6 @@ public function onView(GetResponseEvent $event) {
static function getSubscribedEvents() {
// Since we want HTML to be our default, catch-all response type, give its
// listeners a very low priority so that they always check last.
$events[KernelEvents::EXCEPTION][] = array('onNotFoundHttpException', -5);
$events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException', -5);
$events[KernelEvents::EXCEPTION][] = array('onMethodAllowedException', -5);
$events[KernelEvents::VIEW][] = array('onView', -5);
......
......@@ -34,54 +34,6 @@ protected function isJsonRequestEvent(GetResponseEvent $event) {
return in_array('application/json', $event->getRequest()->getAcceptableContentTypes());
}
protected function createJsonResponse() {
$response = new Response();
$response->headers->set('Content-Type', 'application/json; charset=utf-8');
return $response;
}
/**
* Processes an AccessDenied exception into an HTTP 403 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onAccessDeniedException(GetResponseEvent $event) {
if ($this->isJsonRequestEvent($event) && $event->getException() instanceof AccessDeniedHttpException) {
$response = $this->createJsonResponse();
$response->setStatusCode(403, 'Access Denied');
$event->setResponse($response);
}
}
/**
* Processes a NotFound exception into an HTTP 404 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onNotFoundHttpException(GetResponseEvent $event) {
if ($this->isJsonRequestEvent($event) && $event->getException() instanceof NotFoundHttpException) {
$response = $this->createJsonResponse();
$response->setStatusCode(404, 'Not Found');
$event->setResponse($response);
}
}
/**
* Processes a MethodNotAllowed exception into an HTTP 405 response.
*
* @param GetResponseEvent $event
* The Event to process.
*/
public function onMethodAllowedException(GetResponseEvent $event) {
if ($this->isJsonRequestEvent($event) && $event->getException() instanceof MethodNotAllowedException) {
$response = $this->createJsonResponse();
$response->setStatusCode(405, 'Method Not Allowed');
$event->setResponse($response);
}
}
/**
* Processes a successful controller into an HTTP 200 response.
......@@ -113,9 +65,9 @@ public function onView(GetResponseEvent $event) {
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = array('onNotFoundHttpException');
$events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException');
$events[KernelEvents::EXCEPTION][] = array('onMethodAllowedException');
//$events[KernelEvents::EXCEPTION][] = array('onNotFoundHttpException');
//$events[KernelEvents::EXCEPTION][] = array('onAccessDeniedException');
//$events[KernelEvents::EXCEPTION][] = array('onMethodAllowedException');
$events[KernelEvents::VIEW][] = array('onView');
......
......@@ -60,6 +60,7 @@ public function match($pathinfo) {
require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
}
return $ret;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment