diff options
author | Thomas Steur <thomas.steur@googlemail.com> | 2014-11-14 02:14:37 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@googlemail.com> | 2014-11-14 02:35:29 +0300 |
commit | 25aace1fb7bf949e2ac18a3ab0e84e84e1d91596 (patch) | |
tree | cb9c73461a263baa81317f2cb5a6abdeecad3cc6 /core/Tracker.php | |
parent | 74436365533366123a78bf646b245116b8c0c565 (diff) |
refs #6661 initial work on triggering a 400 bad request instead of 500 in case Idsite is wrong
Diffstat (limited to 'core/Tracker.php')
-rw-r--r-- | core/Tracker.php | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/core/Tracker.php b/core/Tracker.php index 38467fc65d..e0b7e78e3d 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -9,6 +9,7 @@ namespace Piwik; use Exception; +use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; use Piwik\Plugins\SitesManager\SiteUrls; use Piwik\Tracker\Cache; @@ -412,15 +413,16 @@ class Tracker * * @param Exception $e * @param bool $authenticated + * @param int $statusCode eg 500 */ - protected function exitWithException($e, $authenticated = false) + protected function exitWithException($e, $authenticated = false, $statusCode = 500) { if ($this->hasRedirectUrl()) { $this->performRedirectToUrlIfSet(); exit; } - Common::sendHeader('HTTP/1.1 500 Internal Server Error'); + Common::sendResponseCode($statusCode); error_log(sprintf("Error in Piwik (tracker): %s", str_replace("\n", " ", $this->getMessageFromException($e)))); if ($this->usingBulkTracking) { @@ -456,6 +458,7 @@ class Tracker } else { $this->sendResponse(); } + die(1); exit; } @@ -841,6 +844,9 @@ class Tracker } else { Common::printDebug("The request is invalid: empty request, or maybe tracking is disabled in the config.ini.php via record_statistics=0"); } + } catch (UnexpectedWebsiteFoundException $e) { + Common::printDebug("Exception: " . $e->getMessage()); + $this->exitWithException($e, $isAuthenticated, 400); } catch (DbException $e) { Common::printDebug("Exception: " . $e->getMessage()); $this->exitWithException($e, $isAuthenticated); @@ -937,4 +943,44 @@ class Tracker return array_unique($siteIds); } + /** + * @param $e + * @param $authenticated + */ + private function outputException($e, $authenticated) + { + if ($this->usingBulkTracking) { + // when doing bulk tracking we return JSON so the caller will know how many succeeded + $result = array( + 'status' => 'error', + 'tracked' => $this->countOfLoggedRequests + ); + // send error when in debug mode or when authenticated (which happens when doing log importing, + if ((isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) + || $authenticated + ) { + $result['message'] = $this->getMessageFromException($e); + } + Common::sendHeader('Content-Type: application/json'); + echo Common::json_encode($result); + return; + } + + if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) { + Common::sendHeader('Content-Type: text/html; charset=utf-8'); + $trailer = '<span style="color: #888888">Backtrace:<br /><pre>' . $e->getTraceAsString() . '</pre></span>'; + $headerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/simpleLayoutHeader.tpl'); + $footerPage = file_get_contents(PIWIK_INCLUDE_PATH . '/plugins/Morpheus/templates/simpleLayoutFooter.tpl'); + $headerPage = str_replace('{$HTML_TITLE}', 'Piwik › Error', $headerPage); + + echo $headerPage . '<p>' . $this->getMessageFromException($e) . '</p>' . $trailer . $footerPage; + } // If not debug, but running authenticated (eg. during log import) then we display raw errors + elseif ($authenticated) { + Common::sendHeader('Content-Type: text/html; charset=utf-8'); + echo $this->getMessageFromException($e); + } else { + $this->outputTransparentGif(); + } + } + } |