3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * Exception used to abort API execution with an error
24 * If possible, use ApiBase::dieWithError() instead of throwing this directly.
28 class ApiUsageException
extends MWException
implements ILocalizedException
{
30 protected $modulePath;
34 * @param ApiBase|null $module API module responsible for the error, if known
35 * @param StatusValue $status Status holding errors
36 * @param int $httpCode HTTP error code to use
38 public function __construct(
39 ApiBase
$module = null, StatusValue
$status, $httpCode = 0
41 if ( $status->isOK() ) {
42 throw new InvalidArgumentException( __METHOD__
. ' requires a fatal Status' );
45 $this->modulePath
= $module ?
$module->getModulePath() : null;
46 $this->status
= $status;
48 // Bug T46111: Messages in the log files should be in English and not
49 // customized by the local wiki.
50 $enMsg = clone $this->getApiMessage();
51 $enMsg->inLanguage( 'en' )->useDatabase( false );
52 parent
::__construct( ApiErrorFormatter
::stripMarkup( $enMsg->text() ), $httpCode );
56 * @param ApiBase|null $module API module responsible for the error, if known
57 * @param string|array|Message $msg See ApiMessage::create()
58 * @param string|null $code See ApiMessage::create()
59 * @param array|null $data See ApiMessage::create()
60 * @param int $httpCode HTTP error code to use
63 public static function newWithMessage(
64 ApiBase
$module = null, $msg, $code = null, $data = null, $httpCode = 0
68 StatusValue
::newFatal( ApiMessage
::create( $msg, $code, $data ) ),
76 private function getApiMessage() {
77 $errors = $this->status
->getErrorsByType( 'error' );
79 $errors = $this->status
->getErrors();
82 $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
84 $msg = ApiMessage
::create( $errors[0] );
90 * Fetch the responsible module name
93 public function getModulePath() {
94 return $this->modulePath
;
98 * Fetch the error status
101 public function getStatusValue() {
102 return $this->status
;
108 public function getMessageObject() {
109 return Status
::wrap( $this->status
)->getMessage();
115 public function __toString() {
116 $enMsg = clone $this->getApiMessage();
117 $enMsg->inLanguage( 'en' )->useDatabase( false );
118 $text = ApiErrorFormatter
::stripMarkup( $enMsg->text() );
120 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
121 . "in {$this->getFile()}:{$this->getLine()}\n"
122 . "Stack trace:\n{$this->getTraceAsString()}";