ResponseFactory::createFromException already had support for arbitrary exceptions, but Router was so far only using it for HttpExceptions, leaving other kinds of exceptions uncaught. In addition to catching all exceptions and generating an appropriate JSON response for them, this patch introduces the ErrorReporter interface, with an MWErrorReporter implementation which calls MWExceptionHandler::rollbackMasterChangesAndLog(). This is how uncaught errors are handled for requests coming in via api.php, so it seems appropriate to use the same approach for requests coming in via rest.php. Bug: T285984 Change-Id: I0605a7693821ef58fac80ab67f51a742556a37fd
35 lines
766 B
PHP
35 lines
766 B
PHP
<?php
|
|
|
|
namespace MediaWiki\Rest\Reporter;
|
|
|
|
use MediaWiki\Rest\Handler;
|
|
use MediaWiki\Rest\RequestInterface;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Error reporter based on php's native trigger_error() method.
|
|
* @since 1.38
|
|
*/
|
|
class PHPErrorReporter implements ErrorReporter {
|
|
|
|
/** @var int */
|
|
private $level;
|
|
|
|
/**
|
|
* @param int $level The error level to pass to trigger_error
|
|
*/
|
|
public function __construct( $level = E_USER_WARNING ) {
|
|
$this->level = $level;
|
|
}
|
|
|
|
/**
|
|
* @param Throwable $error
|
|
* @param Handler $handler
|
|
* @param RequestInterface $request
|
|
*/
|
|
public function reportError( Throwable $error, Handler $handler, RequestInterface $request ) {
|
|
$firstLine = preg_split( '#$#m', (string)$error, 0 )[0];
|
|
trigger_error( $firstLine, $this->level );
|
|
}
|
|
|
|
}
|