Clean up redundant Exception|Throwable union type
PHP 7.0 makes many error conditions throw instances of the new Error class which does not extend the known Exception. The Throwable interface provides a concise and type-safe way of handling either, e.g. for logging purposes, but HHVM did not support it, requiring tedious fallback checks. This commit replaces occurrences of Exception in code paths equally covered by Throwable, like Exception|Throwable parameter and return types (also nullable), instanceof guards, duplicated `catch` blocks, as well as related comments and documentation blocks, with the exception of $previous parameter descriptions consistent with the manual at https://www.php.net/manual/en/exception.construct.php Proper type declarations have been added or reinstated where possible. Change-Id: I5d3920d3cc66936a350314e2f19c4f6faeffd7c0
This commit is contained in:
parent
67a588d4ea
commit
1b3bc281ac
18 changed files with 134 additions and 137 deletions
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace MediaWiki\Rest;
|
||||
|
||||
use Exception;
|
||||
use HttpStatus;
|
||||
use InvalidArgumentException;
|
||||
use LanguageCode;
|
||||
|
|
@ -190,11 +189,11 @@ class ResponseFactory {
|
|||
}
|
||||
|
||||
/**
|
||||
* Turn an exception into a JSON error response.
|
||||
* @param Exception|Throwable $exception
|
||||
* Turn a throwable into a JSON error response.
|
||||
* @param Throwable $exception
|
||||
* @return Response
|
||||
*/
|
||||
public function createFromException( $exception ) {
|
||||
public function createFromException( Throwable $exception ) {
|
||||
if ( $exception instanceof LocalizedHttpException ) {
|
||||
$response = $this->createLocalizedHttpError( $exception->getCode(),
|
||||
$exception->getMessageValue() );
|
||||
|
|
|
|||
|
|
@ -1377,14 +1377,14 @@ abstract class ApiBase extends ContextSource {
|
|||
}
|
||||
|
||||
/**
|
||||
* Abort execution with an error derived from an exception
|
||||
* Abort execution with an error derived from a throwable
|
||||
*
|
||||
* @since 1.29
|
||||
* @param Exception|Throwable $exception See ApiErrorFormatter::getMessageFromException()
|
||||
* @param Throwable $exception See ApiErrorFormatter::getMessageFromException()
|
||||
* @param array $options See ApiErrorFormatter::getMessageFromException()
|
||||
* @throws ApiUsageException always
|
||||
*/
|
||||
public function dieWithException( $exception, array $options = [] ) {
|
||||
public function dieWithException( Throwable $exception, array $options = [] ) {
|
||||
$this->dieWithError(
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgument
|
||||
$this->getErrorFormatter()->getMessageFromException( $exception, $options )
|
||||
|
|
|
|||
|
|
@ -187,18 +187,18 @@ class ApiErrorFormatter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get an ApiMessage from an exception
|
||||
* Get an ApiMessage from a throwable
|
||||
* @since 1.29
|
||||
* @param Exception|Throwable $exception
|
||||
* @param Throwable $exception
|
||||
* @param array $options
|
||||
* - wrap: (string|array|MessageSpecifier) Used to wrap the exception's
|
||||
* message if it's not an ILocalizedException. The exception's message
|
||||
* - wrap: (string|array|MessageSpecifier) Used to wrap the throwable's
|
||||
* message if it's not an ILocalizedException. The throwable's message
|
||||
* will be added as the final parameter.
|
||||
* - code: (string) Default code
|
||||
* - data: (array) Default extra data
|
||||
* @return IApiMessage
|
||||
*/
|
||||
public function getMessageFromException( $exception, array $options = [] ) {
|
||||
public function getMessageFromException( Throwable $exception, array $options = [] ) {
|
||||
$options += [ 'code' => null, 'data' => [] ];
|
||||
|
||||
if ( $exception instanceof ILocalizedException ) {
|
||||
|
|
@ -228,14 +228,14 @@ class ApiErrorFormatter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Format an exception as an array
|
||||
* Format a throwable as an array
|
||||
* @since 1.29
|
||||
* @param Exception|Throwable $exception
|
||||
* @param Throwable $exception
|
||||
* @param array $options See self::getMessageFromException(), plus
|
||||
* - format: (string) Format override
|
||||
* @return array
|
||||
*/
|
||||
public function formatException( $exception, array $options = [] ) {
|
||||
public function formatException( Throwable $exception, array $options = [] ) {
|
||||
return $this->formatMessage(
|
||||
// @phan-suppress-next-line PhanTypeMismatchArgument
|
||||
$this->getMessageFromException( $exception, $options ),
|
||||
|
|
|
|||
|
|
@ -74,14 +74,14 @@ class ApiErrorFormatter_BackCompat extends ApiErrorFormatter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Format an exception as an array
|
||||
* Format a throwable as an array
|
||||
* @since 1.29
|
||||
* @param Exception|Throwable $exception
|
||||
* @param Throwable $exception
|
||||
* @param array $options See parent::formatException(), plus
|
||||
* - bc: (bool) Return only the string, not an array
|
||||
* @return array|string
|
||||
*/
|
||||
public function formatException( $exception, array $options = [] ) {
|
||||
public function formatException( Throwable $exception, array $options = [] ) {
|
||||
$ret = parent::formatException( $exception, $options );
|
||||
return empty( $options['bc'] ) ? $ret : $ret['info'];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -559,12 +559,12 @@ class ApiMain extends ApiBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Handle an exception as an API response
|
||||
* Handle a throwable as an API response
|
||||
*
|
||||
* @since 1.23
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
*/
|
||||
protected function handleException( $e ) {
|
||||
protected function handleException( Throwable $e ) {
|
||||
// T65145: Rollback any open database transactions
|
||||
if ( !$e instanceof ApiUsageException ) {
|
||||
// ApiUsageExceptions are intentional, so don't rollback if that's the case
|
||||
|
|
@ -626,16 +626,16 @@ class ApiMain extends ApiBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Handle an exception from the ApiBeforeMain hook.
|
||||
* Handle a throwable from the ApiBeforeMain hook.
|
||||
*
|
||||
* This tries to print the exception as an API response, to be more
|
||||
* friendly to clients. If it fails, it will rethrow the exception.
|
||||
* This tries to print the throwable as an API response, to be more
|
||||
* friendly to clients. If it fails, it will rethrow the throwable.
|
||||
*
|
||||
* @since 1.23
|
||||
* @param Exception|Throwable $e
|
||||
* @throws Exception|Throwable
|
||||
* @param Throwable $e
|
||||
* @throws Throwable
|
||||
*/
|
||||
public static function handleApiBeforeMainException( $e ) {
|
||||
public static function handleApiBeforeMainException( Throwable $e ) {
|
||||
ob_start();
|
||||
|
||||
try {
|
||||
|
|
@ -991,21 +991,21 @@ class ApiMain extends ApiBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create an error message for the given exception.
|
||||
* Create an error message for the given throwable.
|
||||
*
|
||||
* If an ApiUsageException, errors/warnings will be extracted from the
|
||||
* embedded StatusValue.
|
||||
*
|
||||
* Any other exception will be returned with a generic code and wrapper
|
||||
* text around the exception's (presumably English) message as a single
|
||||
* Any other throwable will be returned with a generic code and wrapper
|
||||
* text around the throwable's (presumably English) message as a single
|
||||
* error (no warnings).
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @param string $type 'error' or 'warning'
|
||||
* @return ApiMessage[]
|
||||
* @since 1.27
|
||||
*/
|
||||
protected function errorMessagesFromException( $e, $type = 'error' ) {
|
||||
protected function errorMessagesFromException( Throwable $e, $type = 'error' ) {
|
||||
$messages = [];
|
||||
if ( $e instanceof ApiUsageException ) {
|
||||
foreach ( $e->getStatusValue()->getErrorsByType( $type ) as $error ) {
|
||||
|
|
@ -1039,11 +1039,11 @@ class ApiMain extends ApiBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* Replace the result data with the information about an exception.
|
||||
* @param Exception|Throwable $e
|
||||
* Replace the result data with the information about a throwable.
|
||||
* @param Throwable $e
|
||||
* @return string[] Error codes
|
||||
*/
|
||||
protected function substituteResultWithError( $e ) {
|
||||
protected function substituteResultWithError( Throwable $e ) {
|
||||
$result = $this->getResult();
|
||||
$formatter = $this->getErrorFormatter();
|
||||
$config = $this->getConfig();
|
||||
|
|
@ -1616,9 +1616,9 @@ class ApiMain extends ApiBase {
|
|||
/**
|
||||
* Log the preceding request
|
||||
* @param float $time Time in seconds
|
||||
* @param Exception|Throwable|null $e Exception caught while processing the request
|
||||
* @param Throwable|null $e Throwable caught while processing the request
|
||||
*/
|
||||
protected function logRequest( $time, $e = null ) {
|
||||
protected function logRequest( $time, Throwable $e = null ) {
|
||||
$request = $this->getRequest();
|
||||
|
||||
$logCtx = [
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ namespace MediaWiki\Logger;
|
|||
|
||||
use DateTimeZone;
|
||||
use Error;
|
||||
use Exception;
|
||||
use MWDebug;
|
||||
use MWExceptionHandler;
|
||||
use Psr\Log\AbstractLogger;
|
||||
|
|
@ -313,16 +312,16 @@ class LegacyLogger extends AbstractLogger {
|
|||
$text = self::formatAsWfDebugLog( $channel, $message, $context );
|
||||
}
|
||||
|
||||
// Append stacktrace of exception if available
|
||||
// Append stacktrace of throwable if available
|
||||
if ( $wgLogExceptionBacktrace && isset( $context['exception'] ) ) {
|
||||
$e = $context['exception'];
|
||||
$backtrace = false;
|
||||
|
||||
if ( $e instanceof Throwable || $e instanceof Exception ) {
|
||||
if ( $e instanceof Throwable ) {
|
||||
$backtrace = MWExceptionHandler::getRedactedTrace( $e );
|
||||
|
||||
} elseif ( is_array( $e ) && isset( $e['trace'] ) ) {
|
||||
// Exception has already been unpacked as structured data
|
||||
// Throwable has already been unpacked as structured data
|
||||
$backtrace = $e['trace'];
|
||||
}
|
||||
|
||||
|
|
@ -454,7 +453,7 @@ class LegacyLogger extends AbstractLogger {
|
|||
return $item->format( 'c' );
|
||||
}
|
||||
|
||||
if ( $item instanceof Throwable || $item instanceof Exception ) {
|
||||
if ( $item instanceof Throwable ) {
|
||||
$which = $item instanceof Error ? 'Error' : 'Exception';
|
||||
return '[' . $which . ' ' . get_class( $item ) . '( ' .
|
||||
$item->getFile() . ':' . $item->getLine() . ') ' .
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@
|
|||
namespace MediaWiki\Logger\Monolog;
|
||||
|
||||
use Error;
|
||||
use Exception;
|
||||
use Monolog\Formatter\LineFormatter as MonologLineFormatter;
|
||||
use MWExceptionHandler;
|
||||
use Throwable;
|
||||
|
|
@ -68,7 +67,7 @@ class LineFormatter extends MonologLineFormatter {
|
|||
// Drop the 'private' flag from the context
|
||||
unset( $record['context']['private'] );
|
||||
|
||||
// Handle exceptions specially: pretty format and remove from context
|
||||
// Handle throwables specially: pretty format and remove from context
|
||||
// Will be output for a '%exception%' placeholder in format
|
||||
$prettyException = '';
|
||||
if ( isset( $record['context']['exception'] ) &&
|
||||
|
|
@ -77,7 +76,7 @@ class LineFormatter extends MonologLineFormatter {
|
|||
$e = $record['context']['exception'];
|
||||
unset( $record['context']['exception'] );
|
||||
|
||||
if ( $e instanceof Throwable || $e instanceof Exception ) {
|
||||
if ( $e instanceof Throwable ) {
|
||||
$prettyException = $this->normalizeException( $e );
|
||||
} elseif ( is_array( $e ) ) {
|
||||
$prettyException = $this->normalizeExceptionArray( $e );
|
||||
|
|
@ -97,20 +96,21 @@ class LineFormatter extends MonologLineFormatter {
|
|||
/**
|
||||
* Convert a Throwable to a string.
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
protected function normalizeException( $e ) {
|
||||
// Can't use typehint. Must match Monolog\Formatter\LineFormatter::normalizeException($e)
|
||||
return $this->normalizeExceptionArray( $this->exceptionAsArray( $e ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a throwable to an array of structured data.
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return array
|
||||
*/
|
||||
protected function exceptionAsArray( $e ) {
|
||||
protected function exceptionAsArray( Throwable $e ) {
|
||||
$out = [
|
||||
'class' => get_class( $e ),
|
||||
'message' => $e->getMessage(),
|
||||
|
|
|
|||
|
|
@ -84,12 +84,12 @@ class LogstashFormatter extends \Monolog\Formatter\LogstashFormatter {
|
|||
|
||||
/**
|
||||
* Use a more user-friendly trace format than NormalizerFormatter
|
||||
* @param \Exception|\Throwable $e
|
||||
* @param \Throwable $e
|
||||
* @return array
|
||||
*/
|
||||
protected function normalizeException( $e ) {
|
||||
if ( !$e instanceof \Exception && !$e instanceof \Throwable ) {
|
||||
throw new \InvalidArgumentException( 'Exception/Throwable expected, got '
|
||||
if ( !$e instanceof \Throwable ) {
|
||||
throw new \InvalidArgumentException( 'Throwable expected, got '
|
||||
. gettype( $e ) . ' / ' . get_class( $e ) );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ class DeferredUpdates {
|
|||
|
||||
/** @var ErrorPageError $guiEx */
|
||||
$guiEx = null;
|
||||
/** @var Exception|Throwable $exception */
|
||||
/** @var Throwable $exception */
|
||||
$exception = null;
|
||||
|
||||
/** @var DeferrableUpdate[] $updates Snapshot of queue */
|
||||
|
|
@ -271,14 +271,14 @@ class DeferredUpdates {
|
|||
}
|
||||
|
||||
/**
|
||||
* Run a task and catch/log any exceptions
|
||||
* Run a task and catch/log any throwables
|
||||
*
|
||||
* @param DeferrableUpdate $update
|
||||
* @param LBFactory $lbFactory
|
||||
* @param LoggerInterface $logger
|
||||
* @param StatsdDataFactoryInterface $stats
|
||||
* @param string $httpMethod
|
||||
* @return Exception|Throwable|null
|
||||
* @return Throwable|null
|
||||
*/
|
||||
private static function run(
|
||||
DeferrableUpdate $update,
|
||||
|
|
@ -286,7 +286,7 @@ class DeferredUpdates {
|
|||
LoggerInterface $logger,
|
||||
StatsdDataFactoryInterface $stats,
|
||||
$httpMethod
|
||||
) {
|
||||
) : ?Throwable {
|
||||
$suffix = ( $update instanceof DeferrableCallback ) ? "_{$update->getOrigin()}" : '';
|
||||
$type = get_class( $update ) . $suffix;
|
||||
$stats->increment( "deferred_updates.$httpMethod.$type" );
|
||||
|
|
@ -296,7 +296,6 @@ class DeferredUpdates {
|
|||
self::attemptUpdate( $update, $lbFactory );
|
||||
|
||||
return null;
|
||||
} catch ( Exception $e ) {
|
||||
} catch ( Throwable $e ) {
|
||||
}
|
||||
|
||||
|
|
@ -319,7 +318,6 @@ class DeferredUpdates {
|
|||
JobQueueGroup::singleton( $spec['domain'] )->push( $spec['job'] );
|
||||
|
||||
return $e;
|
||||
} catch ( Exception $jobEx ) {
|
||||
} catch ( Throwable $jobEx ) {
|
||||
}
|
||||
|
||||
|
|
@ -363,7 +361,6 @@ class DeferredUpdates {
|
|||
JobQueueGroup::singleton( $spec['domain'] )->push( $spec['job'] );
|
||||
|
||||
return;
|
||||
} catch ( Exception $e ) {
|
||||
} catch ( Throwable $e ) {
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,10 +32,10 @@ class LocalizedException extends Exception implements ILocalizedException {
|
|||
/**
|
||||
* @param string|array|MessageSpecifier $messageSpec See Message::newFromSpecifier
|
||||
* @param int $code
|
||||
* @param Exception|Throwable|null $previous The previous exception used for the exception
|
||||
* @param Throwable|null $previous The previous exception used for the exception
|
||||
* chaining.
|
||||
*/
|
||||
public function __construct( $messageSpec, $code = 0, $previous = null ) {
|
||||
public function __construct( $messageSpec, $code = 0, Throwable $previous = null ) {
|
||||
$this->messageSpec = $messageSpec;
|
||||
|
||||
// Exception->getMessage() should be in plain English, not localized.
|
||||
|
|
|
|||
|
|
@ -88,10 +88,10 @@ class MWExceptionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Report an exception to the user
|
||||
* @param Exception|Throwable $e
|
||||
* Report a throwable to the user
|
||||
* @param Throwable $e
|
||||
*/
|
||||
protected static function report( $e ) {
|
||||
protected static function report( Throwable $e ) {
|
||||
try {
|
||||
// Try and show the exception prettily, with the normal skin infrastructure
|
||||
if ( $e instanceof MWException ) {
|
||||
|
|
@ -111,14 +111,14 @@ class MWExceptionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Roll back any open database transactions and log the stack trace of the exception
|
||||
* Roll back any open database transactions and log the stack trace of the throwable
|
||||
*
|
||||
* This method is used to attempt to recover from exceptions
|
||||
*
|
||||
* @since 1.23
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public static function rollbackMasterChangesAndLog( $e ) {
|
||||
public static function rollbackMasterChangesAndLog( Throwable $e ) {
|
||||
$services = MediaWikiServices::getInstance();
|
||||
if ( !$services->isServiceDisabled( 'DBLoadBalancerFactory' ) ) {
|
||||
// Rollback DBs to avoid transaction notices. This might fail
|
||||
|
|
@ -143,9 +143,9 @@ class MWExceptionHandler {
|
|||
* Callback to use with PHP's set_exception_handler.
|
||||
*
|
||||
* @since 1.31
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public static function handleUncaughtException( $e ) {
|
||||
public static function handleUncaughtException( Throwable $e ) {
|
||||
self::handleException( $e );
|
||||
|
||||
// Make sure we don't claim success on exit for CLI scripts (T177414)
|
||||
|
|
@ -170,9 +170,9 @@ class MWExceptionHandler {
|
|||
* }
|
||||
*
|
||||
* @since 1.25
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
*/
|
||||
public static function handleException( $e ) {
|
||||
public static function handleException( Throwable $e ) {
|
||||
self::rollbackMasterChangesAndLog( $e );
|
||||
self::report( $e );
|
||||
}
|
||||
|
|
@ -330,16 +330,16 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Generate a string representation of an exception's stack trace
|
||||
* Generate a string representation of a throwable's stack trace
|
||||
*
|
||||
* Like Exception::getTraceAsString, but replaces argument values with
|
||||
* Like Throwable::getTraceAsString, but replaces argument values with
|
||||
* argument type or class name.
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
* @see prettyPrintTrace()
|
||||
*/
|
||||
public static function getRedactedTraceAsString( $e ) {
|
||||
public static function getRedactedTraceAsString( Throwable $e ) {
|
||||
return self::prettyPrintTrace( self::getRedactedTrace( $e ) );
|
||||
}
|
||||
|
||||
|
|
@ -359,9 +359,9 @@ TXT;
|
|||
if ( isset( $frame['file'] ) && isset( $frame['line'] ) ) {
|
||||
$text .= "{$pad}#{$level} {$frame['file']}({$frame['line']}): ";
|
||||
} else {
|
||||
// 'file' and 'line' are unset for calls via call_user_func
|
||||
// 'file' and 'line' are unset for calls from C code
|
||||
// (T57634) This matches behaviour of
|
||||
// Exception::getTraceAsString to instead display "[internal
|
||||
// Throwable::getTraceAsString to instead display "[internal
|
||||
// function]".
|
||||
$text .= "{$pad}#{$level} [internal function]: ";
|
||||
}
|
||||
|
|
@ -388,22 +388,22 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Return a copy of an exception's backtrace as an array.
|
||||
* Return a copy of a throwable's backtrace as an array.
|
||||
*
|
||||
* Like Exception::getTrace, but replaces each element in each frame's
|
||||
* Like Throwable::getTrace, but replaces each element in each frame's
|
||||
* argument array with the name of its class (if the element is an object)
|
||||
* or its type (if the element is a PHP primitive).
|
||||
*
|
||||
* @since 1.22
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return array
|
||||
*/
|
||||
public static function getRedactedTrace( $e ) {
|
||||
public static function getRedactedTrace( Throwable $e ) {
|
||||
return static::redactTrace( $e->getTrace() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Redact a stacktrace generated by Exception::getTrace(),
|
||||
* Redact a stacktrace generated by Throwable::getTrace(),
|
||||
* debug_backtrace() or similar means. Replaces each element in each
|
||||
* frame's argument array with the name of its class (if the element is an
|
||||
* object) or its type (if the element is a PHP primitive).
|
||||
|
|
@ -439,13 +439,13 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a message formatting the exception message and its origin.
|
||||
* Get a message formatting the throwable message and its origin.
|
||||
*
|
||||
* @since 1.22
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
public static function getLogMessage( $e ) {
|
||||
public static function getLogMessage( Throwable $e ) {
|
||||
$id = WebRequest::getRequestId();
|
||||
$type = get_class( $e );
|
||||
$file = $e->getFile();
|
||||
|
|
@ -462,10 +462,10 @@ TXT;
|
|||
* Must be used together with `getLogContext()` to be useful.
|
||||
*
|
||||
* @since 1.30
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
public static function getLogNormalMessage( $e ) {
|
||||
public static function getLogNormalMessage( Throwable $e ) {
|
||||
$type = get_class( $e );
|
||||
$file = $e->getFile();
|
||||
$line = $e->getLine();
|
||||
|
|
@ -475,10 +475,10 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
public static function getPublicLogMessage( $e ) {
|
||||
public static function getPublicLogMessage( Throwable $e ) {
|
||||
$reqId = WebRequest::getRequestId();
|
||||
$type = get_class( $e );
|
||||
return '[' . $reqId . '] '
|
||||
|
|
@ -487,17 +487,17 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a PSR-3 log event context from an Exception.
|
||||
* Get a PSR-3 log event context from a Throwable.
|
||||
*
|
||||
* Creates a structured array containing information about the provided
|
||||
* exception that can be used to augment a log message sent to a PSR-3
|
||||
* throwable that can be used to augment a log message sent to a PSR-3
|
||||
* logger.
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @param string $catcher CAUGHT_BY_* class constant indicating what caught the error
|
||||
* @return array
|
||||
*/
|
||||
public static function getLogContext( $e, $catcher = self::CAUGHT_BY_OTHER ) {
|
||||
public static function getLogContext( Throwable $e, $catcher = self::CAUGHT_BY_OTHER ) {
|
||||
return [
|
||||
'exception' => $e,
|
||||
'exception_id' => WebRequest::getRequestId(),
|
||||
|
|
@ -507,18 +507,21 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Get a structured representation of an Exception.
|
||||
* Get a structured representation of a Throwable.
|
||||
*
|
||||
* Returns an array of structured data (class, message, code, file,
|
||||
* backtrace) derived from the given exception. The backtrace information
|
||||
* backtrace) derived from the given throwable. The backtrace information
|
||||
* will be redacted as per getRedactedTraceAsArray().
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @param string $catcher CAUGHT_BY_* class constant indicating what caught the error
|
||||
* @return array
|
||||
* @since 1.26
|
||||
*/
|
||||
public static function getStructuredExceptionData( $e, $catcher = self::CAUGHT_BY_OTHER ) {
|
||||
public static function getStructuredExceptionData(
|
||||
Throwable $e,
|
||||
$catcher = self::CAUGHT_BY_OTHER
|
||||
) {
|
||||
global $wgLogExceptionBacktrace;
|
||||
|
||||
$data = [
|
||||
|
|
@ -552,16 +555,16 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Serialize an Exception object to JSON.
|
||||
* Serialize a Throwable object to JSON.
|
||||
*
|
||||
* The JSON object will have keys 'id', 'file', 'line', 'message', and
|
||||
* 'url'. These keys map to string values, with the exception of 'line',
|
||||
* which is a number, and 'url', which may be either a string URL or or
|
||||
* null if the exception did not occur in the context of serving a web
|
||||
* null if the throwable did not occur in the context of serving a web
|
||||
* request.
|
||||
*
|
||||
* If $wgLogExceptionBacktrace is true, it will also have a 'backtrace'
|
||||
* key, mapped to the array return value of Exception::getTrace, but with
|
||||
* key, mapped to the array return value of Throwable::getTrace, but with
|
||||
* each element in each frame's "args" array (if set) replaced with the
|
||||
* argument's class name (if the argument is an object) or type name (if
|
||||
* the argument is a PHP primitive).
|
||||
|
|
@ -599,14 +602,14 @@ TXT;
|
|||
* @endcode
|
||||
*
|
||||
* @since 1.23
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @param bool $pretty Add non-significant whitespace to improve readability (default: false).
|
||||
* @param int $escaping Bitfield consisting of FormatJson::.*_OK class constants.
|
||||
* @param string $catcher CAUGHT_BY_* class constant indicating what caught the error
|
||||
* @return string|false JSON string if successful; false upon failure
|
||||
*/
|
||||
public static function jsonSerializeException(
|
||||
$e, $pretty = false, $escaping = 0, $catcher = self::CAUGHT_BY_OTHER
|
||||
Throwable $e, $pretty = false, $escaping = 0, $catcher = self::CAUGHT_BY_OTHER
|
||||
) {
|
||||
return FormatJson::encode(
|
||||
self::getStructuredExceptionData( $e, $catcher ),
|
||||
|
|
@ -616,17 +619,21 @@ TXT;
|
|||
}
|
||||
|
||||
/**
|
||||
* Log an exception to the exception log (if enabled).
|
||||
* Log a throwable to the exception log (if enabled).
|
||||
*
|
||||
* This method must not assume the exception is an MWException,
|
||||
* This method must not assume the throwable is an MWException,
|
||||
* it is also used to handle PHP exceptions or exceptions from other libraries.
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @param string $catcher CAUGHT_BY_* class constant indicating what caught the error
|
||||
* @param array $extraData (since 1.34) Additional data to log
|
||||
* @since 1.22
|
||||
*/
|
||||
public static function logException( $e, $catcher = self::CAUGHT_BY_OTHER, $extraData = [] ) {
|
||||
public static function logException(
|
||||
Throwable $e,
|
||||
$catcher = self::CAUGHT_BY_OTHER,
|
||||
$extraData = []
|
||||
) {
|
||||
if ( !( $e instanceof MWException ) || $e->isLoggable() ) {
|
||||
$logger = LoggerFactory::getInstance( 'exception' );
|
||||
$context = self::getLogContext( $e, $catcher );
|
||||
|
|
|
|||
|
|
@ -32,11 +32,11 @@ class MWExceptionRenderer {
|
|||
const AS_PRETTY = 2; // show as HTML
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e Original exception
|
||||
* @param Throwable $e Original exception
|
||||
* @param int $mode MWExceptionExposer::AS_* constant
|
||||
* @param Exception|Throwable|null $eNew New exception from attempting to show the first
|
||||
* @param Throwable|null $eNew New throwable from attempting to show the first
|
||||
*/
|
||||
public static function output( $e, $mode, $eNew = null ) {
|
||||
public static function output( Throwable $e, $mode, Throwable $eNew = null ) {
|
||||
global $wgMimeType, $wgShowExceptionDetails;
|
||||
|
||||
if ( function_exists( 'apache_setenv' ) ) {
|
||||
|
|
@ -99,10 +99,10 @@ class MWExceptionRenderer {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e
|
||||
* @return bool Should the exception use $wgOut to output the error?
|
||||
* @param Throwable $e
|
||||
* @return bool Should the throwable use $wgOut to output the error?
|
||||
*/
|
||||
private static function useOutputPage( $e ) {
|
||||
private static function useOutputPage( Throwable $e ) {
|
||||
// Can the extension use the Message class/wfMessage to get i18n-ed messages?
|
||||
foreach ( $e->getTrace() as $frame ) {
|
||||
if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
|
||||
|
|
@ -123,11 +123,11 @@ class MWExceptionRenderer {
|
|||
}
|
||||
|
||||
/**
|
||||
* Output the exception report using HTML
|
||||
* Output the throwable report using HTML
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
*/
|
||||
private static function reportHTML( $e ) {
|
||||
private static function reportHTML( Throwable $e ) {
|
||||
global $wgOut, $wgSitename;
|
||||
|
||||
if ( self::useOutputPage( $e ) ) {
|
||||
|
|
@ -171,10 +171,10 @@ class MWExceptionRenderer {
|
|||
* backtrace to the error, otherwise show a message to ask to set it to true
|
||||
* to show that information.
|
||||
*
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string Html to output
|
||||
*/
|
||||
public static function getHTML( $e ) {
|
||||
public static function getHTML( Throwable $e ) {
|
||||
global $wgShowExceptionDetails;
|
||||
|
||||
if ( $wgShowExceptionDetails ) {
|
||||
|
|
@ -228,10 +228,10 @@ class MWExceptionRenderer {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
private static function getText( $e ) {
|
||||
private static function getText( Throwable $e ) {
|
||||
global $wgShowExceptionDetails;
|
||||
|
||||
if ( $wgShowExceptionDetails ) {
|
||||
|
|
@ -244,10 +244,10 @@ class MWExceptionRenderer {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
* @return string
|
||||
*/
|
||||
private static function getShowBacktraceError( $e ) {
|
||||
private static function getShowBacktraceError( Throwable $e ) {
|
||||
$var = '$wgShowExceptionDetails = true;';
|
||||
return "Set $var at the bottom of LocalSettings.php to show detailed debugging information.";
|
||||
}
|
||||
|
|
@ -296,9 +296,9 @@ class MWExceptionRenderer {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Exception|Throwable $e
|
||||
* @param Throwable $e
|
||||
*/
|
||||
private static function reportOutageHTML( $e ) {
|
||||
private static function reportOutageHTML( Throwable $e ) {
|
||||
global $wgShowExceptionDetails, $wgShowHostnames, $wgSitename;
|
||||
|
||||
$sorry = htmlspecialchars( self::msg(
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Wikimedia\ParamValidator\Util;
|
||||
|
||||
use Exception;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
|
@ -70,9 +69,6 @@ class UploadedFileStream implements StreamInterface {
|
|||
try {
|
||||
$this->seek( 0 );
|
||||
return $this->getContents();
|
||||
} catch ( Exception $ex ) {
|
||||
// Not allowed to throw
|
||||
return '';
|
||||
} catch ( Throwable $ex ) {
|
||||
// Not allowed to throw
|
||||
return '';
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Wikimedia\ParamValidator;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
use UnexpectedValueException;
|
||||
use Wikimedia\Message\DataMessageValue;
|
||||
|
|
@ -32,10 +31,10 @@ class ValidationException extends UnexpectedValueException {
|
|||
* @param string $name Parameter name being validated
|
||||
* @param mixed $value Value of the parameter
|
||||
* @param array $settings Settings array being used for validation
|
||||
* @param Throwable|Exception|null $previous Previous exception causing this failure
|
||||
* @param Throwable|null $previous Previous throwable causing this failure
|
||||
*/
|
||||
public function __construct(
|
||||
DataMessageValue $failureMessage, $name, $value, $settings, $previous = null
|
||||
DataMessageValue $failureMessage, $name, $value, $settings, Throwable $previous = null
|
||||
) {
|
||||
$this->failureMessage = $failureMessage;
|
||||
$this->paramName = $name;
|
||||
|
|
|
|||
|
|
@ -3747,7 +3747,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
|
|||
private function runOnAtomicSectionCancelCallbacks(
|
||||
$trigger, array $sectionIds = null
|
||||
) {
|
||||
/** @var Exception|Throwable $e */
|
||||
/** @var Throwable $e */
|
||||
$e = null; // first exception
|
||||
|
||||
$notCancelled = [];
|
||||
|
|
@ -3774,7 +3774,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
|
|||
$this->trxSectionCancelCallbacks = $notCancelled;
|
||||
|
||||
if ( $e !== null ) {
|
||||
throw $e; // re-throw any first Exception/Throwable
|
||||
throw $e; // re-throw any first Throwable
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@ class DBError extends RuntimeException {
|
|||
* Construct a database error
|
||||
* @param IDatabase|null $db Object which threw the error
|
||||
* @param string $error A simple error message to be used for debugging
|
||||
* @param \Exception|\Throwable|null $prev Previous exception
|
||||
* @param \Throwable|null $prev Previous throwable
|
||||
*/
|
||||
public function __construct( ?IDatabase $db, $error, $prev = null ) {
|
||||
public function __construct( ?IDatabase $db, $error, \Throwable $prev = null ) {
|
||||
parent::__construct( $error, 0, $prev );
|
||||
$this->db = $db;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,10 +37,10 @@ class DBExpectedError extends DBError implements MessageSpecifier {
|
|||
* @param IDatabase|null $db
|
||||
* @param string $error
|
||||
* @param array $params
|
||||
* @param \Exception|\Throwable|null $prev
|
||||
* @param \Throwable|null $prev
|
||||
*/
|
||||
public function __construct(
|
||||
?IDatabase $db, $error, array $params = [], $prev = null
|
||||
?IDatabase $db, $error, array $params = [], \Throwable $prev = null
|
||||
) {
|
||||
parent::__construct( $db, $error, $prev );
|
||||
$this->params = $params;
|
||||
|
|
|
|||
|
|
@ -30,10 +30,10 @@ class UploadStashException extends MWException implements ILocalizedException {
|
|||
/**
|
||||
* @param string|array|MessageSpecifier $messageSpec See Message::newFromSpecifier
|
||||
* @param int $code Exception code
|
||||
* @param Exception|Throwable|null $previous The previous exception used for the exception
|
||||
* @param Throwable|null $previous The previous exception used for the exception
|
||||
* chaining.
|
||||
*/
|
||||
public function __construct( $messageSpec, $code = 0, $previous = null ) {
|
||||
public function __construct( $messageSpec, $code = 0, Throwable $previous = null ) {
|
||||
$this->messageSpec = $messageSpec;
|
||||
|
||||
$msg = $this->getMessageObject()->text();
|
||||
|
|
|
|||
Loading…
Reference in a new issue