PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.6.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.6.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / ExceptionHandler.php
matomo / app / core Last commit date
API 4 years ago Access 5 years ago Application 4 years ago Archive 4 years ago ArchiveProcessor 4 years ago Archiver 5 years ago AssetManager 4 years ago Auth 5 years ago Category 5 years ago CliMulti 4 years ago Columns 5 years ago Concurrency 4 years ago Config 5 years ago Container 4 years ago CronArchive 4 years ago DataAccess 4 years ago DataFiles 5 years ago DataTable 4 years ago Db 4 years ago DeviceDetector 4 years ago Email 5 years ago Exception 4 years ago Http 5 years ago Intl 4 years ago Mail 5 years ago Measurable 5 years ago Menu 5 years ago Metrics 4 years ago Notification 4 years ago Period 4 years ago Plugin 4 years ago ProfessionalServices 5 years ago Report 5 years ago ReportRenderer 5 years ago Scheduler 4 years ago Segment 5 years ago Session 4 years ago Settings 5 years ago Tracker 4 years ago Translation 4 years ago UpdateCheck 5 years ago Updater 4 years ago Updates 4 years ago Validators 4 years ago View 4 years ago ViewDataTable 5 years ago Visualization 5 years ago Widget 5 years ago .htaccess 6 years ago Access.php 4 years ago Archive.php 4 years ago ArchiveProcessor.php 4 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 4 years ago Common.php 4 years ago Config.php 4 years ago Console.php 5 years ago Context.php 5 years ago Cookie.php 4 years ago CronArchive.php 4 years ago DataArray.php 4 years ago DataTable.php 4 years ago Date.php 4 years ago Db.php 4 years ago DbHelper.php 4 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 4 years ago ExceptionHandler.php 4 years ago FileIntegrity.php 5 years ago Filechecks.php 5 years ago Filesystem.php 4 years ago FrontController.php 4 years ago Http.php 4 years ago IP.php 4 years ago Log.php 4 years ago LogDeleter.php 4 years ago Mail.php 4 years ago Metrics.php 5 years ago NoAccessException.php 5 years ago Nonce.php 4 years ago Notification.php 5 years ago NumberFormatter.php 5 years ago Option.php 4 years ago Period.php 5 years ago Piwik.php 4 years ago Plugin.php 4 years ago Profiler.php 4 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 4 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 4 years ago SettingsPiwik.php 5 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 4 years ago SupportedBrowser.php 4 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 4 years ago Twig.php 4 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 4 years ago UrlHelper.php 4 years ago Version.php 4 years ago View.php 4 years ago bootstrap.php 5 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 5 years ago
ExceptionHandler.php
193 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\Container\StaticContainer;
17 use Piwik\Plugins\CoreAdminHome\CustomLogo;
18 use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor;
19 use Psr\Log\LoggerInterface;
20
21 /**
22 * Contains Piwik's uncaught exception handler.
23 */
24 class ExceptionHandler
25 {
26 public static function setUp()
27 {
28 set_exception_handler(array('Piwik\ExceptionHandler', 'handleException'));
29 }
30
31 /**
32 * @param Exception|\Throwable $exception
33 */
34 public static function handleException($exception)
35 {
36 if (Common::isPhpCliMode()) {
37 self::dieWithCliError($exception);
38 }
39
40 self::dieWithHtmlErrorPage($exception);
41 }
42
43 /**
44 * @param Exception|\Throwable $exception
45 */
46 public static function dieWithCliError($exception)
47 {
48 self::logException($exception);
49
50 $message = $exception->getMessage();
51
52 if (!method_exists($exception, 'isHtmlMessage') || !$exception->isHtmlMessage()) {
53 $message = strip_tags(str_replace('<br />', PHP_EOL, $message));
54 }
55
56 $message = sprintf(
57 "Uncaught exception in %s line %d:\n%s\n",
58 $exception->getFile(),
59 $exception->getLine(),
60 ExceptionToTextProcessor::getMessageAndWholeBacktrace($exception)
61 );
62
63 echo $message;
64
65 exit(1);
66 }
67
68 /**
69 * @param Exception|\Throwable $exception
70 */
71 public static function dieWithHtmlErrorPage($exception)
72 {
73 // Set an appropriate HTTP response code.
74 switch (true) {
75 case ( ($exception instanceof \Piwik\Http\HttpCodeException || $exception instanceof \Piwik\Exception\NotSupportedBrowserException) && $exception->getCode() > 0):
76 // For these exception types, use the exception-provided error code.
77 http_response_code($exception->getCode());
78 break;
79 case ($exception instanceof \Piwik\Exception\NotYetInstalledException):
80 http_response_code(404);
81 break;
82 default:
83 http_response_code(500);
84 }
85
86 // Log the error with an appropriate loglevel.
87 switch (true) {
88 case ($exception instanceof \Piwik\Exception\NotSupportedBrowserException):
89 // These unsupported browsers are really a client-side problem, so log only at DEBUG level.
90 self::logException($exception, Log::DEBUG);
91 break;
92 default:
93 self::logException($exception);
94 }
95
96 Common::sendHeader('Content-Type: text/html; charset=utf-8');
97
98 try {
99 echo self::getErrorResponse($exception);
100 } catch(Exception $e) {
101 // When there are failures while generating the HTML error response itself,
102 // we simply print out the error message instead.
103 echo $exception->getMessage();
104 }
105
106 exit(1);
107 }
108
109 /**
110 * @param Exception|\Throwable $ex
111 */
112 private static function getErrorResponse($ex)
113 {
114 $debugTrace = $ex->getTraceAsString();
115
116 $message = $ex->getMessage();
117
118 $isHtmlMessage = method_exists($ex, 'isHtmlMessage') && $ex->isHtmlMessage();
119
120 if (!$isHtmlMessage && Request::isApiRequest($_GET)) {
121
122 $outputFormat = strtolower(Common::getRequestVar('format', 'xml', 'string', $_GET + $_POST));
123 $response = new ResponseBuilder($outputFormat);
124 return $response->getResponseException($ex);
125
126 } elseif (!$isHtmlMessage) {
127 $message = Common::sanitizeInputValue($message);
128 }
129
130
131 $logoHeaderUrl = 'plugins/Morpheus/images/logo.svg';
132 $logoFaviconUrl = 'plugins/CoreHome/images/favicon.png';
133 try {
134 $logo = new CustomLogo();
135 if ($logo->hasSVGLogo()) {
136 $logoHeaderUrl = $logo->getSVGLogoUrl();
137 } else {
138 $logoHeaderUrl = $logo->getHeaderLogoUrl();
139 }
140 $logoFaviconUrl = $logo->getPathUserFavicon();
141 } catch (Exception $ex) {
142 try {
143 Log::debug($ex);
144 } catch (\Exception $otherEx) {
145 // DI container may not be setup at this point
146 }
147 }
148
149 $result = Piwik_GetErrorMessagePage($message, $debugTrace, true, true, $logoHeaderUrl, $logoFaviconUrl);
150
151 try {
152 /**
153 * Triggered before a Piwik error page is displayed to the user.
154 *
155 * This event can be used to modify the content of the error page that is displayed when
156 * an exception is caught.
157 *
158 * @param string &$result The HTML of the error page.
159 * @param Exception $ex The Exception displayed in the error page.
160 */
161 Piwik::postEvent('FrontController.modifyErrorPage', array(&$result, $ex));
162 } catch (ContainerDoesNotExistException $ex) {
163 // this can happen when an error occurs before the Piwik environment is created
164 }
165
166 return $result;
167 }
168
169 private static function logException($exception, $loglevel=Log::ERROR)
170 {
171 try {
172 switch ($loglevel) {
173 case(Log::DEBUG):
174 StaticContainer::get(LoggerInterface::class)->debug('Uncaught exception: {exception}', [
175 'exception' => $exception,
176 'ignoreInScreenWriter' => true,
177 ]);
178 break;
179 case(Log::ERROR):
180 default:
181 StaticContainer::get(LoggerInterface::class)->error('Uncaught exception: {exception}', [
182 'exception' => $exception,
183 'ignoreInScreenWriter' => true,
184 ]);
185 }
186 } catch (DependencyException $ex) {
187 // ignore (occurs if exception is thrown when resolving DI entries)
188 } catch (ContainerDoesNotExistException $ex) {
189 // ignore
190 }
191 }
192 }
193