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
23 * This exception will be thrown when dieUsage is called to stop module execution.
26 * @deprecated since 1.29, use ApiUsageException instead
28 class UsageException
extends MWException
{
38 * @param string $message
39 * @param string $codestr
41 * @param array|null $extradata
43 public function __construct( $message, $codestr, $code = 0, $extradata = null ) {
44 parent
::__construct( $message, $code );
45 $this->mCodestr
= $codestr;
46 $this->mExtraData
= $extradata;
48 // This should never happen, so throw an exception about it that will
49 // hopefully get logged with a backtrace (T138585)
50 if ( !is_string( $codestr ) ||
$codestr === '' ) {
51 throw new InvalidArgumentException( 'Invalid $codestr, was ' .
52 ( $codestr === '' ?
'empty string' : gettype( $codestr ) )
60 public function getCodeString() {
61 return $this->mCodestr
;
67 public function getMessageArray() {
69 'code' => $this->mCodestr
,
70 'info' => $this->getMessage()
72 if ( is_array( $this->mExtraData
) ) {
73 $result = array_merge( $result, $this->mExtraData
);
82 public function __toString() {
83 return "{$this->getCodeString()}: {$this->getMessage()}";
88 * Exception used to abort API execution with an error
90 * If possible, use ApiBase::dieWithError() instead of throwing this directly.
93 * @note This currently extends UsageException for backwards compatibility, so
94 * all the existing code that catches UsageException won't break when stuff
95 * starts throwing ApiUsageException. Eventually UsageException will go away
96 * and this will (probably) extend MWException directly.
98 class ApiUsageException
extends UsageException
implements ILocalizedException
{
100 protected $modulePath;
104 * @param ApiBase|null $module API module responsible for the error, if known
105 * @param StatusValue $status Status holding errors
106 * @param int $httpCode HTTP error code to use
108 public function __construct(
109 ApiBase
$module = null, StatusValue
$status, $httpCode = 0
111 if ( $status->isOK() ) {
112 throw new InvalidArgumentException( __METHOD__
. ' requires a fatal Status' );
115 $this->modulePath
= $module ?
$module->getModulePath() : null;
116 $this->status
= $status;
118 // Bug T46111: Messages in the log files should be in English and not
119 // customized by the local wiki.
120 $enMsg = clone $this->getApiMessage();
121 $enMsg->inLanguage( 'en' )->useDatabase( false );
123 ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
124 $enMsg->getApiCode(),
131 * @param ApiBase|null $module API module responsible for the error, if known
132 * @param string|array|Message $msg See ApiMessage::create()
133 * @param string|null $code See ApiMessage::create()
134 * @param array|null $data See ApiMessage::create()
135 * @param int $httpCode HTTP error code to use
138 public static function newWithMessage(
139 ApiBase
$module = null, $msg, $code = null, $data = null, $httpCode = 0
143 StatusValue
::newFatal( ApiMessage
::create( $msg, $code, $data ) ),
149 * @returns ApiMessage
151 private function getApiMessage() {
152 $errors = $this->status
->getErrorsByType( 'error' );
154 $errors = $this->status
->getErrors();
157 $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
159 $msg = ApiMessage
::create( $errors[0] );
165 * Fetch the responsible module name
166 * @return string|null
168 public function getModulePath() {
169 return $this->modulePath
;
173 * Fetch the error status
174 * @return StatusValue
176 public function getStatusValue() {
177 return $this->status
;
181 * @deprecated Do not use. This only exists here because UsageException is in
182 * the inheritance chain for backwards compatibility.
185 public function getCodeString() {
186 return $this->getApiMessage()->getApiCode();
190 * @deprecated Do not use. This only exists here because UsageException is in
191 * the inheritance chain for backwards compatibility.
194 public function getMessageArray() {
195 $enMsg = clone $this->getApiMessage();
196 $enMsg->inLanguage( 'en' )->useDatabase( false );
199 'code' => $enMsg->getApiCode(),
200 'info' => ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
201 ] +
$enMsg->getApiData();
207 public function getMessageObject() {
208 return $this->status
->getMessage();
214 public function __toString() {
215 $enMsg = clone $this->getApiMessage();
216 $enMsg->inLanguage( 'en' )->useDatabase( false );
217 $text = ApiErrorFormatter
::stripMarkup( $enMsg->text() );
219 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
220 . "in {$this->getFile()}:{$this->getLine()}\n"
221 . "Stack trace:\n{$this->getTraceAsString()}";