Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Hotfix/console 404 reporting #2437

Merged
merged 3 commits into from

5 participants

@weierophinney

Adds error reporting capabilities to the Console RouteNotFoundStrategy.

If the view_manager "display_not_found_reason" flag is enabled, this will display the reason for the "404" condition, as well as any exceptions, if any, present in the event (looping through all previous exceptions).

@EvanDotPro EvanDotPro was assigned
library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php
((6 lines not shown))
$result = $banner ? rtrim($banner, "\r\n") : '';
$result .= $usage ? "\n\n" . trim($usage, "\r\n") : '';
$result .= "\n"; // to ensure we output a final newline
+
+ // Report 404 reason and/or exceptions
@Freeaqingme Collaborator

This method hereby becomes 100+ lines long. Can't we move this to its own method?

+1

The getConsoleUsage() method has 100+ lines too :pensive:

@weierophinney Owner

@Freeaqingme Done.

@b-durand I didn't add any code to getConsoleUsage(), which makes that out-of-scope for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
weierophinney added some commits
@weierophinney weierophinney Display reason for not-found conditions in Console
- If display_not_found_reason is enabled, use this value to display:
  - reason for "404"
  - any related exceptions
aae13cb
@weierophinney weierophinney Display previous exceptions as well
- so that we can determine full root cause from CLI
982e0c6
@weierophinney weierophinney Extract method
- extracted display_not_found_reason and exception reporting to
  reportNotFoundReason() method, per feedback on pull request.
52162ad
@weierophinney

I figured a force push would tell travis to re-run. Odd.

Rebased off of latest master, which corrects the CS issues reported in the former travis results. I've also extracted the logic for displaying the exception stack trace to a new method, addressing the concerns @Freeaqingme raised.

@ralphschindler ralphschindler merged commit 52162ad into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 4, 2012
  1. @weierophinney

    Display reason for not-found conditions in Console

    weierophinney authored
    - If display_not_found_reason is enabled, use this value to display:
      - reason for "404"
      - any related exceptions
  2. @weierophinney

    Display previous exceptions as well

    weierophinney authored
    - so that we can determine full root cause from CLI
  3. @weierophinney

    Extract method

    weierophinney authored
    - extracted display_not_found_reason and exception reporting to
      reportNotFoundReason() method, per feedback on pull request.
This page is out of date. Refresh to see the latest.
View
64 library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php
@@ -42,6 +42,13 @@ class RouteNotFoundStrategy implements ListenerAggregateInterface
protected $listeners = array();
/**
+ * Whether or not to display the reason for routing failure
+ *
+ * @var bool
+ */
+ protected $displayNotFoundReason = true;
+
+ /**
* The reason for a not-found condition
*
* @var boolean|string
@@ -75,6 +82,28 @@ public function detach(EventManagerInterface $events)
}
/**
+ * Set flag indicating whether or not to display the routing failure
+ *
+ * @param bool $displayNotFoundReason
+ * @return RouteNotFoundStrategy
+ */
+ public function setDisplayNotFoundReason($displayNotFoundReason)
+ {
+ $this->displayNotFoundReason = (bool) $displayNotFoundReason;
+ return $this;
+ }
+
+ /**
+ * Do we display the routing failure?
+ *
+ * @return bool
+ */
+ public function displayNotFoundReason()
+ {
+ return $this->displayNotFoundReason;
+ }
+
+ /**
* Detect if an error is a route not found condition
*
* If a "controller not found" or "invalid controller" error type is
@@ -162,10 +191,11 @@ public function handleRouteNotFoundError(MvcEvent $e)
// Get application usage information
$usage = $this->getConsoleUsage($console, $scriptName, $mm, $router);
- // Inject the text into view model
+ // Inject the text into view
$result = $banner ? rtrim($banner, "\r\n") : '';
$result .= $usage ? "\n\n" . trim($usage, "\r\n") : '';
$result .= "\n"; // to ensure we output a final newline
+ $result .= $this->reportNotFoundReason($e);
$model->setResult($result);
// Inject the result into MvcEvent
@@ -412,4 +442,36 @@ protected function renderTable($data, $cols, $consoleWidth)
return $table->render();
}
+
+ /**
+ * Report the 404 reason and/or exceptions
+ *
+ * @param \Zend\Event\EventInterface $e
+ * @return string
+ */
+ protected function reportNotFoundReason($e)
+ {
+ if (!$this->displayNotFoundReason()) {
+ return '';
+ }
+ $exception = $e->getParam('exception', false);
+ if (!$exception && !$this->reason) {
+ return '';
+ }
+
+ $reason = (isset($this->reason) && !empty($this->reason)) ? $this->reason : 'unknown';
+ $reasons = array(
+ Application::ERROR_CONTROLLER_NOT_FOUND => 'Could not match to a controller',
+ Application::ERROR_CONTROLLER_INVALID => 'Invalid controller specified',
+ Application::ERROR_ROUTER_NO_MATCH => 'Invalid arguments or no arguments provided',
+ 'unknown' => 'Unknown',
+ );
+ $report = sprintf("\nReason for failure: %s\n", $reasons[$reason]);
+
+ while ($exception instanceof \Exception) {
+ $report .= sprintf("Exception: %s\nTrace:\n%s\n", $exception->getMessage(), $exception->getTraceAsString());
+ $exception = $exception->getPrevious();
+ }
+ return $report;
+ }
}
View
7 library/Zend/Mvc/View/Console/ViewManager.php
@@ -146,6 +146,13 @@ public function getRouteNotFoundStrategy()
$this->routeNotFoundStrategy = new RouteNotFoundStrategy();
+ $displayNotFoundReason = true;
+
+ if (array_key_exists('display_not_found_reason', $this->config)) {
+ $displayNotFoundReason = $this->config['display_not_found_reason'];
+ }
+ $this->routeNotFoundStrategy->setDisplayNotFoundReason($displayNotFoundReason);
+
$this->services->setService('RouteNotFoundStrategy', $this->routeNotFoundStrategy);
$this->services->setAlias('Zend\Mvc\View\RouteNotFoundStrategy', 'RouteNotFoundStrategy');
$this->services->setAlias('Zend\Mvc\View\Console\RouteNotFoundStrategy', 'RouteNotFoundStrategy');
Something went wrong with that request. Please try again.