API
5 years ago
Access
5 years ago
Application
5 years ago
Archive
5 years ago
ArchiveProcessor
5 years ago
Archiver
5 years ago
AssetManager
5 years ago
Auth
5 years ago
Category
5 years ago
CliMulti
5 years ago
Columns
5 years ago
Composer
5 years ago
Concurrency
5 years ago
Config
5 years ago
Container
5 years ago
CronArchive
5 years ago
DataAccess
5 years ago
DataFiles
5 years ago
DataTable
5 years ago
Db
5 years ago
DeviceDetector
5 years ago
Email
5 years ago
Exception
5 years ago
Http
5 years ago
Intl
5 years ago
Mail
5 years ago
Measurable
5 years ago
Menu
5 years ago
Metrics
5 years ago
Notification
5 years ago
Period
5 years ago
Plugin
5 years ago
ProfessionalServices
5 years ago
Report
5 years ago
ReportRenderer
5 years ago
Scheduler
5 years ago
Segment
5 years ago
Session
5 years ago
Settings
5 years ago
Tracker
5 years ago
Translation
5 years ago
UpdateCheck
5 years ago
Updater
5 years ago
Updates
5 years ago
Validators
5 years ago
View
5 years ago
ViewDataTable
5 years ago
Visualization
5 years ago
Widget
5 years ago
.htaccess
6 years ago
Access.php
5 years ago
Archive.php
5 years ago
ArchiveProcessor.php
5 years ago
AssetManager.php
5 years ago
Auth.php
5 years ago
AuthResult.php
5 years ago
BaseFactory.php
5 years ago
Cache.php
5 years ago
CacheId.php
5 years ago
CliMulti.php
5 years ago
Common.php
5 years ago
Config.php
5 years ago
Console.php
5 years ago
Context.php
5 years ago
Cookie.php
5 years ago
CronArchive.php
5 years ago
DataArray.php
5 years ago
DataTable.php
5 years ago
Date.php
5 years ago
Db.php
5 years ago
DbHelper.php
5 years ago
Development.php
5 years ago
ErrorHandler.php
5 years ago
EventDispatcher.php
5 years ago
ExceptionHandler.php
5 years ago
FileIntegrity.php
5 years ago
Filechecks.php
5 years ago
Filesystem.php
5 years ago
FrontController.php
5 years ago
Http.php
5 years ago
IP.php
5 years ago
Log.php
5 years ago
LogDeleter.php
5 years ago
Mail.php
5 years ago
Metrics.php
5 years ago
NoAccessException.php
5 years ago
Nonce.php
5 years ago
Notification.php
5 years ago
NumberFormatter.php
5 years ago
Option.php
5 years ago
Period.php
5 years ago
Piwik.php
5 years ago
Plugin.php
5 years ago
Profiler.php
5 years ago
ProxyHeaders.php
5 years ago
ProxyHttp.php
5 years ago
QuickForm2.php
5 years ago
RankingQuery.php
5 years ago
ReportRenderer.php
5 years ago
Segment.php
5 years ago
Sequence.php
5 years ago
Session.php
5 years ago
SettingsPiwik.php
5 years ago
SettingsServer.php
5 years ago
Singleton.php
5 years ago
Site.php
5 years ago
SupportedBrowser.php
5 years ago
TCPDF.php
5 years ago
Theme.php
5 years ago
Timer.php
5 years ago
Tracker.php
5 years ago
Twig.php
5 years ago
Unzip.php
5 years ago
UpdateCheck.php
5 years ago
Updater.php
5 years ago
UpdaterErrorException.php
5 years ago
Updates.php
5 years ago
Url.php
5 years ago
UrlHelper.php
5 years ago
Version.php
5 years ago
View.php
5 years ago
bootstrap.php
5 years ago
dispatch.php
5 years ago
testMinimumPhpVersion.php
5 years ago
ExceptionHandler.php
174 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use DI\DependencyException; |
| 12 | use Exception; |
| 13 | use Piwik\API\Request; |
| 14 | use Piwik\API\ResponseBuilder; |
| 15 | use Piwik\Container\ContainerDoesNotExistException; |
| 16 | use Piwik\Exception\NotYetInstalledException; |
| 17 | use Piwik\Http\HttpCodeException; |
| 18 | use Piwik\Container\StaticContainer; |
| 19 | use Piwik\Plugins\CoreAdminHome\CustomLogo; |
| 20 | use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor; |
| 21 | use Psr\Log\LoggerInterface; |
| 22 | |
| 23 | /** |
| 24 | * Contains Piwik's uncaught exception handler. |
| 25 | */ |
| 26 | class ExceptionHandler |
| 27 | { |
| 28 | public static function setUp() |
| 29 | { |
| 30 | set_exception_handler(array('Piwik\ExceptionHandler', 'handleException')); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param Exception|\Throwable $exception |
| 35 | */ |
| 36 | public static function handleException($exception) |
| 37 | { |
| 38 | if (Common::isPhpCliMode()) { |
| 39 | self::dieWithCliError($exception); |
| 40 | } |
| 41 | |
| 42 | self::dieWithHtmlErrorPage($exception); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param Exception|\Throwable $exception |
| 47 | */ |
| 48 | public static function dieWithCliError($exception) |
| 49 | { |
| 50 | self::logException($exception); |
| 51 | |
| 52 | $message = $exception->getMessage(); |
| 53 | |
| 54 | if (!method_exists($exception, 'isHtmlMessage') || !$exception->isHtmlMessage()) { |
| 55 | $message = strip_tags(str_replace('<br />', PHP_EOL, $message)); |
| 56 | } |
| 57 | |
| 58 | $message = sprintf( |
| 59 | "Uncaught exception in %s line %d:\n%s\n", |
| 60 | $exception->getFile(), |
| 61 | $exception->getLine(), |
| 62 | ExceptionToTextProcessor::getMessageAndWholeBacktrace($exception) |
| 63 | ); |
| 64 | |
| 65 | echo $message; |
| 66 | |
| 67 | exit(1); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param Exception|\Throwable $exception |
| 72 | */ |
| 73 | public static function dieWithHtmlErrorPage($exception) |
| 74 | { |
| 75 | if ($exception instanceof HttpCodeException |
| 76 | && $exception->getCode() > 0 |
| 77 | ) { |
| 78 | http_response_code($exception->getCode()); |
| 79 | } elseif ($exception instanceof NotYetInstalledException) { |
| 80 | http_response_code(404); |
| 81 | } else { |
| 82 | http_response_code(500); |
| 83 | } |
| 84 | |
| 85 | self::logException($exception); |
| 86 | |
| 87 | Common::sendHeader('Content-Type: text/html; charset=utf-8'); |
| 88 | |
| 89 | try { |
| 90 | echo self::getErrorResponse($exception); |
| 91 | } catch(Exception $e) { |
| 92 | // When there are failures while generating the HTML error response itself, |
| 93 | // we simply print out the error message instead. |
| 94 | echo $exception->getMessage(); |
| 95 | } |
| 96 | |
| 97 | exit(1); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param Exception|\Throwable $ex |
| 102 | */ |
| 103 | private static function getErrorResponse($ex) |
| 104 | { |
| 105 | $debugTrace = $ex->getTraceAsString(); |
| 106 | |
| 107 | $message = $ex->getMessage(); |
| 108 | |
| 109 | $isHtmlMessage = method_exists($ex, 'isHtmlMessage') && $ex->isHtmlMessage(); |
| 110 | |
| 111 | if (!$isHtmlMessage && Request::isApiRequest($_GET)) { |
| 112 | |
| 113 | $outputFormat = strtolower(Common::getRequestVar('format', 'xml', 'string', $_GET + $_POST)); |
| 114 | $response = new ResponseBuilder($outputFormat); |
| 115 | return $response->getResponseException($ex); |
| 116 | |
| 117 | } elseif (!$isHtmlMessage) { |
| 118 | $message = Common::sanitizeInputValue($message); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | $logoHeaderUrl = 'plugins/Morpheus/images/logo.svg'; |
| 123 | $logoFaviconUrl = 'plugins/CoreHome/images/favicon.png'; |
| 124 | try { |
| 125 | $logo = new CustomLogo(); |
| 126 | if ($logo->hasSVGLogo()) { |
| 127 | $logoHeaderUrl = $logo->getSVGLogoUrl(); |
| 128 | } else { |
| 129 | $logoHeaderUrl = $logo->getHeaderLogoUrl(); |
| 130 | } |
| 131 | $logoFaviconUrl = $logo->getPathUserFavicon(); |
| 132 | } catch (Exception $ex) { |
| 133 | try { |
| 134 | Log::debug($ex); |
| 135 | } catch (\Exception $otherEx) { |
| 136 | // DI container may not be setup at this point |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logoHeaderUrl, $logoFaviconUrl); |
| 141 | |
| 142 | try { |
| 143 | /** |
| 144 | * Triggered before a Piwik error page is displayed to the user. |
| 145 | * |
| 146 | * This event can be used to modify the content of the error page that is displayed when |
| 147 | * an exception is caught. |
| 148 | * |
| 149 | * @param string &$result The HTML of the error page. |
| 150 | * @param Exception $ex The Exception displayed in the error page. |
| 151 | */ |
| 152 | Piwik::postEvent('FrontController.modifyErrorPage', array(&$result, $ex)); |
| 153 | } catch (ContainerDoesNotExistException $ex) { |
| 154 | // this can happen when an error occurs before the Piwik environment is created |
| 155 | } |
| 156 | |
| 157 | return $result; |
| 158 | } |
| 159 | |
| 160 | private static function logException($exception) |
| 161 | { |
| 162 | try { |
| 163 | StaticContainer::get(LoggerInterface::class)->error('Uncaught exception: {exception}', [ |
| 164 | 'exception' => $exception, |
| 165 | 'ignoreInScreenWriter' => true, |
| 166 | ]); |
| 167 | } catch (DependencyException $ex) { |
| 168 | // ignore (occurs if exception is thrown when resolving DI entries) |
| 169 | } catch (ContainerDoesNotExistException $ex) { |
| 170 | // ignore |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 |