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 * This exception will be thrown when dieUsage is called to stop module execution.
25 * @deprecated since 1.29, use ApiUsageException instead
27 class UsageException
extends MWException
{
37 * @param string $message
38 * @param string $codestr
40 * @param array|null $extradata
42 public function __construct( $message, $codestr, $code = 0, $extradata = null ) {
43 parent
::__construct( $message, $code );
44 $this->mCodestr
= $codestr;
45 $this->mExtraData
= $extradata;
47 if ( !$this instanceof ApiUsageException
) {
48 wfDeprecated( __METHOD__
, '1.29' );
51 // This should never happen, so throw an exception about it that will
52 // hopefully get logged with a backtrace (T138585)
53 if ( !is_string( $codestr ) ||
$codestr === '' ) {
54 throw new InvalidArgumentException( 'Invalid $codestr, was ' .
55 ( $codestr === '' ?
'empty string' : gettype( $codestr ) )
63 public function getCodeString() {
64 wfDeprecated( __METHOD__
, '1.29' );
65 return $this->mCodestr
;
71 public function getMessageArray() {
72 wfDeprecated( __METHOD__
, '1.29' );
74 'code' => $this->mCodestr
,
75 'info' => $this->getMessage()
77 if ( is_array( $this->mExtraData
) ) {
78 $result = array_merge( $result, $this->mExtraData
);
87 public function __toString() {
88 return "{$this->getCodeString()}: {$this->getMessage()}";
93 * Exception used to abort API execution with an error
95 * If possible, use ApiBase::dieWithError() instead of throwing this directly.
98 * @note This currently extends UsageException for backwards compatibility, so
99 * all the existing code that catches UsageException won't break when stuff
100 * starts throwing ApiUsageException. Eventually UsageException will go away
101 * and this will (probably) extend MWException directly.
103 class ApiUsageException
extends UsageException
implements ILocalizedException
{
105 protected $modulePath;
109 * @param ApiBase|null $module API module responsible for the error, if known
110 * @param StatusValue $status Status holding errors
111 * @param int $httpCode HTTP error code to use
113 public function __construct(
114 ApiBase
$module = null, StatusValue
$status, $httpCode = 0
116 if ( $status->isOK() ) {
117 throw new InvalidArgumentException( __METHOD__
. ' requires a fatal Status' );
120 $this->modulePath
= $module ?
$module->getModulePath() : null;
121 $this->status
= $status;
123 // Bug T46111: Messages in the log files should be in English and not
124 // customized by the local wiki.
125 $enMsg = clone $this->getApiMessage();
126 $enMsg->inLanguage( 'en' )->useDatabase( false );
128 ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
129 $enMsg->getApiCode(),
136 * @param ApiBase|null $module API module responsible for the error, if known
137 * @param string|array|Message $msg See ApiMessage::create()
138 * @param string|null $code See ApiMessage::create()
139 * @param array|null $data See ApiMessage::create()
140 * @param int $httpCode HTTP error code to use
143 public static function newWithMessage(
144 ApiBase
$module = null, $msg, $code = null, $data = null, $httpCode = 0
148 StatusValue
::newFatal( ApiMessage
::create( $msg, $code, $data ) ),
156 private function getApiMessage() {
157 $errors = $this->status
->getErrorsByType( 'error' );
159 $errors = $this->status
->getErrors();
162 $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
164 $msg = ApiMessage
::create( $errors[0] );
170 * Fetch the responsible module name
171 * @return string|null
173 public function getModulePath() {
174 return $this->modulePath
;
178 * Fetch the error status
179 * @return StatusValue
181 public function getStatusValue() {
182 return $this->status
;
186 * @deprecated Do not use. This only exists here because UsageException is in
187 * the inheritance chain for backwards compatibility.
190 public function getCodeString() {
191 wfDeprecated( __METHOD__
, '1.29' );
192 return $this->getApiMessage()->getApiCode();
196 * @deprecated Do not use. This only exists here because UsageException is in
197 * the inheritance chain for backwards compatibility.
200 public function getMessageArray() {
201 wfDeprecated( __METHOD__
, '1.29' );
202 $enMsg = clone $this->getApiMessage();
203 $enMsg->inLanguage( 'en' )->useDatabase( false );
206 'code' => $enMsg->getApiCode(),
207 'info' => ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
208 ] +
$enMsg->getApiData();
214 public function getMessageObject() {
215 return Status
::wrap( $this->status
)->getMessage();
221 public function __toString() {
222 $enMsg = clone $this->getApiMessage();
223 $enMsg->inLanguage( 'en' )->useDatabase( false );
224 $text = ApiErrorFormatter
::stripMarkup( $enMsg->text() );
226 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
227 . "in {$this->getFile()}:{$this->getLine()}\n"
228 . "Stack trace:\n{$this->getTraceAsString()}";