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.
27 * @note This currently extends UsageException for backwards compatibility, so
28 * all the existing code that catches UsageException won't break when stuff
29 * starts throwing ApiUsageException. Eventually UsageException will go away
30 * and this will (probably) extend MWException directly.
32 class ApiUsageException
extends UsageException
implements ILocalizedException
{
34 protected $modulePath;
38 * @param ApiBase|null $module API module responsible for the error, if known
39 * @param StatusValue $status Status holding errors
40 * @param int $httpCode HTTP error code to use
42 public function __construct(
43 ApiBase
$module = null, StatusValue
$status, $httpCode = 0
45 if ( $status->isOK() ) {
46 throw new InvalidArgumentException( __METHOD__
. ' requires a fatal Status' );
49 $this->modulePath
= $module ?
$module->getModulePath() : null;
50 $this->status
= $status;
52 // Bug T46111: Messages in the log files should be in English and not
53 // customized by the local wiki.
54 $enMsg = clone $this->getApiMessage();
55 $enMsg->inLanguage( 'en' )->useDatabase( false );
57 ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
65 * @param ApiBase|null $module API module responsible for the error, if known
66 * @param string|array|Message $msg See ApiMessage::create()
67 * @param string|null $code See ApiMessage::create()
68 * @param array|null $data See ApiMessage::create()
69 * @param int $httpCode HTTP error code to use
72 public static function newWithMessage(
73 ApiBase
$module = null, $msg, $code = null, $data = null, $httpCode = 0
77 StatusValue
::newFatal( ApiMessage
::create( $msg, $code, $data ) ),
85 private function getApiMessage() {
86 $errors = $this->status
->getErrorsByType( 'error' );
88 $errors = $this->status
->getErrors();
91 $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
93 $msg = ApiMessage
::create( $errors[0] );
99 * Fetch the responsible module name
100 * @return string|null
102 public function getModulePath() {
103 return $this->modulePath
;
107 * Fetch the error status
108 * @return StatusValue
110 public function getStatusValue() {
111 return $this->status
;
115 * @deprecated Do not use. This only exists here because UsageException is in
116 * the inheritance chain for backwards compatibility.
119 public function getCodeString() {
120 wfDeprecated( __METHOD__
, '1.29' );
121 return $this->getApiMessage()->getApiCode();
125 * @deprecated Do not use. This only exists here because UsageException is in
126 * the inheritance chain for backwards compatibility.
129 public function getMessageArray() {
130 wfDeprecated( __METHOD__
, '1.29' );
131 $enMsg = clone $this->getApiMessage();
132 $enMsg->inLanguage( 'en' )->useDatabase( false );
135 'code' => $enMsg->getApiCode(),
136 'info' => ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
137 ] +
$enMsg->getApiData();
143 public function getMessageObject() {
144 return Status
::wrap( $this->status
)->getMessage();
150 public function __toString() {
151 $enMsg = clone $this->getApiMessage();
152 $enMsg->inLanguage( 'en' )->useDatabase( false );
153 $text = ApiErrorFormatter
::stripMarkup( $enMsg->text() );
155 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
156 . "in {$this->getFile()}:{$this->getLine()}\n"
157 . "Stack trace:\n{$this->getTraceAsString()}";