Commit graph

131 commits

Author SHA1 Message Date
Reedy
fc1759bfe6 MWExceptionHandler: Add error suppression to constant( 'E_STRICT' )
Bug: T375707
Change-Id: I075edf064cefa012d3d7a2c734a2e9051a826074
Follows-Up: I5937cacdf5b01614042a06d4deb5112ffff51727
(cherry picked from commit 96a12a2dd6b98a25e4a9048a25b2b5010036e32c)
2025-01-21 22:31:11 +00:00
xtex
7fec3fdec6 exception: Convert E_STRICT errors to E_USER_NOTICE
E_STRICT is deprecated in PHP 8.4.0.

MediaWiki (and almost all extensions) no longer produces errors at this
level.
To not break the compatibility, let's converts all E_STRICT level errors
to E_USER_NOTICE, which is also mapped to warning severity.

Using '@' operator to avoid generating the deprecation warning of
E_STRICT.

This should addresses the deprecation warning of E_STRICT when another
warning or error is raised.

Bug: T375707
Change-Id: I5937cacdf5b01614042a06d4deb5112ffff51727
2025-01-03 16:31:41 +00:00
Tim Starling
218bb176cb exception: Suppress dependency loop exception
If an exception is thrown or an error is raised during HookContainer
construction, don't be surprised when you can't get a HookContainer.

When checking if we need to roll back transactions, use peekService()
instead of isDisabled(). According to the disableService()
documentation, peekService() will always return null for a disabled
service. If the service hasn't been created, there can be no
transactions open and so there is no point in rolling back.

It's not necessary to catch DBError exceptions from peekService(),
since it never calls the instantiator.

Fix phan error by broadening the onLogException() parameter type. Phan
is correct, the parameter is not guaranteed to be
Exception|ErrorException and so the previous documentation was wrong.

Bug: T379125
Change-Id: Ie5ec2bd1ecfb0114f3d00fc28b2c5e3db9bd87c0
(cherry picked from commit 5cf3cd03dfd3f7acca4d78bf12054f3c315fee28)
2024-11-08 16:16:00 +00:00
Umherirrender
31c1011105 exception: Use const for MWExceptionHandler::$fatalErrorTypes
Also remove @var from const, the type is given by the value of the const
and does not need documentation.

Change-Id: I65636f2b97b44387c4320ebfb4be6c81d6f771b9
2024-09-01 13:24:03 +02:00
Bartosz Dziewoński
df4cbf5ac6 Replace gettype() with get_debug_type() in debug/log/test output
get_debug_type() does the same thing but better (spelling type names
in the same way as in type declarations, and including names of
object classes and resource types). It was added in PHP 8, but the
symfony/polyfill-php80 package provides it while we still support 7.4.

Also remove uses of get_class() and get_resource_type() where the new
method already provides the same information.

For reference:
https://www.php.net/manual/en/function.get-debug-type.php
https://www.php.net/manual/en/function.gettype.php

In this commit I'm only changing code where it looks like the result
is used only for some king of debug, log, or test output. This
probably won't break anything important, but I'm not sure whether
anything might depend on the exact values.

Change-Id: I7c1f0a8f669228643e86f8e511c0e26a2edb2948
2024-07-31 19:33:57 +02:00
Ebrahim Byagowi
a717db8e60 Add namespace and deprecation alias to FormatJson
This patch introduces a namespace declaration for the
MediaWiki\Json to FormatJson and establishes a class
alias marked as deprecated since version 1.43.

Bug: T353458
Change-Id: I5e1311e4eb7a878a7db319b725ae262f40671c32
2024-05-16 16:28:01 +03:30
Timo Tijhof
ee292ca30d exception: Remove "error-json" debug log channel
This was introduced to allow maintainers during local development,
and e.g. when debugging in production, to capture silenced runtime
warnings, such as those silenced via AtEase or the `@` PHP operator.

These are not expected to be of interest to capture by default on
web requests, hence placed in a separate channel that e.g. one would
only enable during development, or when debugging.

When this was introduced, MediaWiki was only just starting to
experiment with PSR-3 structuerd logging, and thus it hardcoded JSON.

Now that we support structured logging natively, this has been
obsoleted by the "error" channel (for warnings in production),
and (since last December) the "silenced-error" channel (for debugging
and development) with commit f6cb39979b (I34b19837c3).

With the above two alternatives available, there is no longer a need
for the duplicate "error-json" channel.

Bug: T193472
Change-Id: I7a6e6fa52a47a29ec04411a6c8b05e005a0a4fc7
2024-05-14 17:42:08 +00:00
Amir Sarabadani
214674d6b1 namespace MWDebug
Bug: T353458
Change-Id: I99d728bd111ff882220cd175ff09d4da20b81eae
2024-05-03 22:59:47 +02:00
Bartosz Dziewoński
3bb9d855c1 Remove error printing in MWException
Was deprecated in I66d896f6f229b90e6ba9949311b56a6b6ab3da3d.

Bug: T353444
Change-Id: I004b1b87a7ef1a908123369842792d940b7d8079
2024-04-25 11:58:53 +00:00
jenkins-bot
3f814c0753 Merge "Deprecate error printing in MWException" 2024-01-22 02:16:11 +00:00
Bartosz Dziewoński
825bf602a5 Deprecate error printing in MWException
It's now deprecated to use the individual error printing functions in
MWException or to override them. Printing MWException now works the
same as any other exception, and happens in MWExceptionRenderer.

It's still allowed to override MWException::report() to replace the
entire error page, for example in ErrorPageError.

Bug: T353444
Change-Id: I66d896f6f229b90e6ba9949311b56a6b6ab3da3d
2024-01-10 16:38:43 +01:00
jenkins-bot
c8d061d177 Merge "exception: Send silenced errors to a new log channel with level=DEBUG" 2023-12-21 18:38:46 +00:00
Bartosz Dziewoński
5c8a688b4d exception: Replace $wgCommandLineMode checks with MW_ENTRY_POINT
Replace some other global variable checks with MW_ENTRY_POINT too.

This unfortunately makes it difficult to override the result of
isCommandLine() for testing. However, the existing tests weren't
really checking anything useful, but rather just duplicated the real
code, so I won't miss them.

Bug: T313841
Change-Id: I8bc200d67877ce04f80fd587481a76c7df629d51
2023-12-14 18:01:17 +00:00
Timo Tijhof
f6cb39979b exception: Send silenced errors to a new log channel with level=DEBUG
Follows-up 4f08bb3be9 (I240b75f5a4), which improved the "error" channel
to make use of the PSR-3 severity level (instead of defaulting to
ERROR).

This change exposes the suppressed errors to a new channel,
with a forced severity level DEBUG.

The formatted message is unchanged and reflects the original severity
through its "message" (i.e. "PHP Notice", "PHP Warning", etc.)

Bug: T193472
Change-Id: I34b19837c391ff4acdb55fc0c310c0f554f15da2
2023-12-10 04:17:22 +00:00
James D. Forrester
439a60eabb Drop MWExceptionHandler::rollbackMasterChangesAndLog(), deprecated in 1.37
Change-Id: I1fb68eca5159b56725f6a16822b3d3fcb503d4e4
2023-12-08 17:32:58 -05:00
Umherirrender
f4ef430d28 Use ::class for class name resolution
Change-Id: Ic74a3d317c9d36509a50c658e52fad65026e7ac7
2023-09-21 23:27:28 +02:00
Daimona Eaytoy
f78fd3710d Add $wgRequest to phan's globals_type_map
The base phan config uses a file_exists check to determine whether to
use the namespaced class name, but it doesn't work when running against
core because MW_INSTALL_PATH isn't set. So specify the type in the local
config, and remove @phan-var annotations added in I6bbdbbe6.

Also use `::class` instead of string literals for classes.

Change-Id: I994a0ed32ea948253ed07ee3cc8868a0eaa6d8b9
2023-09-11 23:22:12 +02:00
Amir Sarabadani
5bd33d46ef Reorg: Move WebRequest to includes\Request
This has been approved as part of RFC T166010

Bug: T321882
Change-Id: I6bbdbbe6ea48cc1f50bc568bb8780fc7c5361a6f
2023-09-11 21:44:34 +01:00
Umherirrender
e04d3a28f6 Replace internal Hooks::runner
The Hooks class contains deprecated functions and the whole class is
going to get removed, so remove the convenience function and inline the
code.

Bug: T335536
Change-Id: I8ef3468a64a0199996f26ef293543fcacdf2797f
2023-05-11 06:17:38 +00:00
Amir Sarabadani
bbe704b5c1 Reorg: Move some of request related classes to MediaWiki/Request
Redoing I5ea70120d74 but without moving WebRequest that caused issues
with phan-taint-plugin.

Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequestUpload

Bug: T321882
Change-Id: I832b133aaf61ee9f6190b0227d2f3de99bd1717b
2022-10-28 10:15:31 +00:00
Zabe
f6b9381d7f Revert "Reorg: Move some of request related classes to MediaWiki/Request"
This reverts commit 2bdc0b2b72.

Reason for revert: T166010#8349431

Bug: T166010
Change-Id: Idcd3025647aec99532f5d69b9c1718c531761283
2022-10-27 13:14:16 +00:00
Amir Sarabadani
2bdc0b2b72 Reorg: Move some of request related classes to MediaWiki/Request
Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequest
 - WebRequestUpload

Bug: T166010
Change-Id: I5ea70120d745f2876ae31d039f3f8a51e49e9ad8
2022-10-26 16:49:10 +02:00
daniel
085b9d18c6 exception: Tolerate no service container when trying DB rollback
If the exception handler gets triggered before a service container is
available, it should not attempt a DB rollback. Attempting to access
the DBLoadBalancerFactory service object would cause a secondary error,
obscuring the original issue.

This might happen when the exception handler is triggered during
bootstrapping or in the installer.

Change-Id: I644bd0953aa5e690fea16d9fc11ca3f24cb3f104
2022-09-30 18:42:10 +00:00
Thiemo Kreuz
e23b070b45 Make use of ?? and ?: operators where it makes sense
Change-Id: I1d9d62d80b17d1a05222c6e82f631cb801c311a8
2022-08-04 21:43:12 +02:00
Timo Tijhof
e506500bf0 language: Improve type hints in MessageCache.php
* Replace is_/throw with native where possible.
* Prefer strict comparisons where possible.
* Remove `@throws` for exceptions that are not meant to be checked
  or caught by callers.
* Make the separateCacheableRows() return hint more precise.

Change-Id: I1c14bb8faaf1b377b6d179d96e18331acff23c5b
2022-07-15 05:37:51 +00:00
Matěj Suchánek
1865180ae7 Do minor code cleanup
Remove dead code and fix typos. Should cause no change in behavior.

Change-Id: I5d293b842bc93a28b8bcd799a31b5e6e30fe692e
2022-06-24 13:52:42 +02:00
Timo Tijhof
0bb1538929 REST: Hide exception message when wgShowExceptionDetails=false
This reverts parts of I8520d8cb16 and Ib941c22d6b7e.

The documentation of ShowExceptionDetails, as well as all other
uses of it (e.g. MWExeceptionRenderer for index.php, API, and
ResourceLoader) take it to mean to hide both exception message and
details.

This is why MWExceptionHandler didn't have, and didn't need,
the added complexity of this as a parameter as this method
simply wouldn't be called at all in that case.

* Rename the method added in I8520d8cb16 to match the one
  in MWExceptionRenderer.

* Update REST handling to now print any exception details
  when it is true.

* Remove the now-unused code introduced in Ib941c22d6b7e.

Change-Id: I1a9920dea0bafe315a20489efbe46ea9b55b0f74
2022-05-26 14:45:50 +01:00
daniel
05b0937bdf Remove access to config globals from includes/exception
Allow callers of MWExceptionHandler::getStructuredExceptionData() and
jsonSerializeException() to explicitly control whether a backtrace is
included in the return value. This avoids the need to rely on the
LogExceptionBacktrace setting in static methods.

Bug: T294739
Change-Id: Ib941c22d6b7ec5f1b984bf5ded90652e42ad7b67
2022-05-12 20:33:15 +02:00
Aaron Schulz
db36853718 rdbms: make automatic connection recovery more robust
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Track session state errors caused by the loss of named locks or
temp tables (e.g. during connection loss). Such errors will prevent
further queries except for rollback() and flushSession(), which must
be issued to resolve the error.

Add flushPrimarySessions() methods to LBFactory/LoadBalancer
and use it in places where session state loss is meant to be
safely aknowledged.

Change-Id: I60532f86e629c83b357d4832d1963eca17752944
2022-04-14 11:09:31 +10:00
Alexander Vorwerk
60c3cae9b9 Update comment to use AtEase
Change-Id: I00bf95631509747330d1f58133046d489eed475e
2022-04-06 01:50:55 +02:00
Umherirrender
1f71eccf63 phan: Disable null_casts_as_any_type setting
Make phan stricter about null types by setting null_casts_as_any_type to
false (the default in mediawiki-phan-config)
Remaining false positive issues are suppressed.
The suppression and the setting change can only be done together

Bug: T242536
Bug: T301991
Change-Id: I0f295382b96fb3be8037a01c10487d9d591e7e01
2022-03-21 18:25:07 +00:00
Ladsgroup
31c1ca8658 Revert "rdbms: make automatic connection recovery apply to more cases"
This reverts commit 4cac31de4e.

Reason for revert: Blocking the train, reverting the chain.

Change-Id: I7f275b3a25379c6f3256e90947c8eed4b232c0f4
2022-03-17 20:11:10 +01:00
Aaron Schulz
4cac31de4e rdbms: make automatic connection recovery apply to more cases
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Add methods and class constants to track session state errors
caused by the loss of named locks or temp tables. Such errors can
be resolved by a "session flush" method.

Make assertQueryIsCurrentlyAllowed() better distinguish ROLLBACK
queries from ROLLBACK TO SAVEPOINT queries. For some scenarios,
only full tranasction ROLLBACK queries should be allowed.

Add flushSession() method to Database and flushPrimarySessions()
methods to LBFactory/LoadBalancer.

Also:
* Rename wasKnownStatementRollbackError() and make it take the
  error number as an argument, similar to wasConnectionError().
  Add mysql error codes for query timeouts since they only cause
  statement rollbacks.
* Rename wasConnectionError() and mark it as protected. This is an
  internal method with no outside callers.
* Rename wasQueryTimeout(), remove some HHVM-specific code, and
  simplify the arguments.
* Make executeQuery() use a for loop for the query retry logic
  to reduce code duplication.
* Move the error state setting logic in executeQueryAttempt() up
  in order to reduce code duplication.
* Move the beginIfImplied() call in executeQueryAttempt() up to the
  retry loop in executeQuery(). This narrows the executeQueryAttempt()
  concerns to sending a single query and updating tracking fields.
* Make closeConnection() and doHandleSessionLossPreconnect() in
  DatabaseSqlite more consistent with the base class by releasing named locks.
* Mark trxStatus() as @internal.

Bug: T281451
Bug: T293859
Change-Id: I200f90e413b8a725828745f81925b54985c72180
2022-03-09 15:49:38 +11:00
Umherirrender
b126dbe3f2 Fix various documentation related to null types
The functions returning null or the class property is set explict null

Found by phan strict checks

Change-Id: I4a271093fb6526564d8083a08249c64cb21f2453
2022-02-26 10:31:24 +01:00
Siddharth VP
b77dd0640c Fix typos in comments (M)
Change-Id: I5ab88a01ba3e5ea2aae853bb6f06492fbc84ceb5
2022-01-09 23:00:20 +05:30
Reedy
2a2bb1e9bd Remove or replace usages of "sane"
Bug: T254646
Change-Id: I096b2cf738a1395a14f1d47bcbed0c2c686c2581
2021-11-22 13:35:17 +00:00
jenkins-bot
7d9a98af09 Merge "MWExceptionHandler::rollbackMasterChangesAndLog: Hard-deprecate, unused anywhere" 2021-09-08 20:52:42 +00:00
jenkins-bot
2202cebdeb Merge "Using @return never documentation on always-throw-function" 2021-09-08 03:12:06 +00:00
James D. Forrester
7792a3bc74 MWExceptionHandler::rollbackMasterChangesAndLog: Hard-deprecate, unused anywhere
Change-Id: Ie96598f4e7230f8a9f56516676833b340e0c3fbb
2021-09-07 18:49:44 -07:00
Umherirrender
44fd53fee3 Using @return never documentation on always-throw-function
This helps phan to detect unreachable code and also impossible types
after the functions.
It helps phan to avoid false positives for array keys
when the keys are checked before

Bug: T240141
Change-Id: I895f70e82b3053a46cd44135b15437e6f82a07b2
2021-09-07 17:29:03 +02:00
James D. Forrester
3407458ea0 MWExceptionHandler: Rename rollbackMasterChangesAndLog to rollbackPrimaryChangesAndLog
Change-Id: I9a90b4f74eb65cd9e20ae9faa6d1949be96543c0
2021-09-03 17:36:34 -07:00
James D. Forrester
10324c232a ILoadBalancer/ILBFactory: Rename rollbackMasterChanges to rollbackPrimaryChanges
Bug: T282894
Change-Id: I31794e052d71160195dd3b6c29fea24bc98b356b
2021-09-02 12:50:52 -07:00
libraryupgrader
5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00
Gergő Tisza
3bf86e2413 Handle INormalizedException in MWExceptionHandler
Log INormalizedException messages in a structured way, allowing
the logging infrastructure to group them better.

Change-Id: I877909d1113ab93b4b8a115af5bb0fe039ea32d6
2021-07-07 22:01:27 +02:00
Andre Klapper
6077aa3ac4 Make "A database query error has occurred" message link to documentation
Bug: T272588
Change-Id: I87e7b9014ed4220383c3c83f265274e79643f59e
2021-06-23 17:44:30 +02:00
Timo Tijhof
9911b03707 exception: Restore "PHP Deprecated" prefix for E_USER_DEPRECATED
Follows-up f2543d442a, in which I accidentally removed the prefix.

Also, while at it, drop the "$class: " prefix that we normally
add to exceptions. For errors we internally construct ErrorException,
but this is something a clas name rarely seen by PHP developers and
has sometimes led to confusions about whether this is an exception
or not. We did still have "PHP Notice" and "PHP Warning" right after
it (except for deprecations) which should act as confidence indicator
that they are in fact not exceptions. We also have the channel
name ("error" instead of "exception"). But removing this will help
boost that confidence further, plus it had no added value anyhow, so
less is more?

Change-Id: Ib93f48c94d642f519558aab40d143b43e1d9ed16
2021-04-07 19:21:29 +01:00
Umherirrender
8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00
Timo Tijhof
3a044d0f02 exception: Remove "exception_id" key in favour of reqId
This key predates MediaWiki's standardised notion of request IDs,
and of PSR-3 and Monolog usage.

Bug: T199607
Change-Id: Ibdd5bf12591761ab45be12ba72943e9d94f678cb
2021-01-18 05:33:17 +00:00
Timo Tijhof
8c906e237f exception: Add the 'from' file/line to the logged exception trace
This should make error logs easier to work with through a couple
of ways:

* The stack trace is now complete, instead of missing the first
  crucial step, which is often the one used for filtering
  purposes and for identifying errors within a given deployed
  version of MediaWiki. (E.g. when filtering out an error that is
  expected to be fixed by the next release and/or when checking
  how prominent an error currently is).

* Logstash reports that report message + trace will not need to be
  edited by hand to include the file+line.

* The workflow for Logstash generally follows one of two patterns.

  The default is to filter by exception.file (including line number),
  which is very sure to catch all possible variants thrown from
  the same code, regardless of any variables in the message, but
  has the downside of not matching week-over-week consistency due to
  file paths (at least for WMF) containing the deployment version.

  The other option is to filter by message, which has the risk of
  possibly excluding too much if there are multiple unrelated ways
  to trigger the issue, but is a sensible second option. This is
  usually done by filtering on normalized_message for non-exception
  errors, but doesn't work well for exceptions because they contain
  the file paths and do so in-between the class and message words,
  and thus are not compatible with Logstash's default substring/term
  match. The alternative of exception.message is then considered but
  is lacking the class/type, which can be fragile.

  With this change applied, no editing is needed, and no multiple
  approaches need to be considered with the same option.
  Either filtering by exception.file as-is, or filtering by
  normalized_message as-is, regardless of whether it is an exception
  error or other message in another channel, will both work.

Bug: T271496
Change-Id: I5908ed53f9b97b3c9cde126aca89ab6fc197c845
2021-01-16 22:15:54 +00:00
Tim Starling
95aafed56f In MWExceptionHandler::report(), catch all throwables
A PHP fatal error from MWExceptionRenderer::output() causes the
exception handler to be re-entered, with the original exception thrown
away. So, catch all throwables. That way we can see both error messages,
neither is thrown away.

Bug: T263911
Change-Id: Ie98438cbdd328fe295c9b5202d79edb0c8fb41c5
2020-12-15 17:51:52 +11:00