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 if ( !$this instanceof ApiUsageException
) {
49 wfDeprecated( __METHOD__
, '1.29' );
52 // This should never happen, so throw an exception about it that will
53 // hopefully get logged with a backtrace (T138585)
54 if ( !is_string( $codestr ) ||
$codestr === '' ) {
55 throw new InvalidArgumentException( 'Invalid $codestr, was ' .
56 ( $codestr === '' ?
'empty string' : gettype( $codestr ) )
64 public function getCodeString() {
65 wfDeprecated( __METHOD__
, '1.29' );
66 return $this->mCodestr
;
72 public function getMessageArray() {
73 wfDeprecated( __METHOD__
, '1.29' );
75 'code' => $this->mCodestr
,
76 'info' => $this->getMessage()
78 if ( is_array( $this->mExtraData
) ) {
79 $result = array_merge( $result, $this->mExtraData
);
88 public function __toString() {
89 return "{$this->getCodeString()}: {$this->getMessage()}";
94 * Exception used to abort API execution with an error
96 * If possible, use ApiBase::dieWithError() instead of throwing this directly.
99 * @note This currently extends UsageException for backwards compatibility, so
100 * all the existing code that catches UsageException won't break when stuff
101 * starts throwing ApiUsageException. Eventually UsageException will go away
102 * and this will (probably) extend MWException directly.
104 class ApiUsageException
extends UsageException
implements ILocalizedException
{
106 protected $modulePath;
110 * @param ApiBase|null $module API module responsible for the error, if known
111 * @param StatusValue $status Status holding errors
112 * @param int $httpCode HTTP error code to use
114 public function __construct(
115 ApiBase
$module = null, StatusValue
$status, $httpCode = 0
117 if ( $status->isOK() ) {
118 throw new InvalidArgumentException( __METHOD__
. ' requires a fatal Status' );
121 $this->modulePath
= $module ?
$module->getModulePath() : null;
122 $this->status
= $status;
124 // Bug T46111: Messages in the log files should be in English and not
125 // customized by the local wiki.
126 $enMsg = clone $this->getApiMessage();
127 $enMsg->inLanguage( 'en' )->useDatabase( false );
129 ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
130 $enMsg->getApiCode(),
137 * @param ApiBase|null $module API module responsible for the error, if known
138 * @param string|array|Message $msg See ApiMessage::create()
139 * @param string|null $code See ApiMessage::create()
140 * @param array|null $data See ApiMessage::create()
141 * @param int $httpCode HTTP error code to use
144 public static function newWithMessage(
145 ApiBase
$module = null, $msg, $code = null, $data = null, $httpCode = 0
149 StatusValue
::newFatal( ApiMessage
::create( $msg, $code, $data ) ),
155 * @returns ApiMessage
157 private function getApiMessage() {
158 $errors = $this->status
->getErrorsByType( 'error' );
160 $errors = $this->status
->getErrors();
163 $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
165 $msg = ApiMessage
::create( $errors[0] );
171 * Fetch the responsible module name
172 * @return string|null
174 public function getModulePath() {
175 return $this->modulePath
;
179 * Fetch the error status
180 * @return StatusValue
182 public function getStatusValue() {
183 return $this->status
;
187 * @deprecated Do not use. This only exists here because UsageException is in
188 * the inheritance chain for backwards compatibility.
191 public function getCodeString() {
192 wfDeprecated( __METHOD__
, '1.29' );
193 return $this->getApiMessage()->getApiCode();
197 * @deprecated Do not use. This only exists here because UsageException is in
198 * the inheritance chain for backwards compatibility.
201 public function getMessageArray() {
202 wfDeprecated( __METHOD__
, '1.29' );
203 $enMsg = clone $this->getApiMessage();
204 $enMsg->inLanguage( 'en' )->useDatabase( false );
207 'code' => $enMsg->getApiCode(),
208 'info' => ApiErrorFormatter
::stripMarkup( $enMsg->text() ),
209 ] +
$enMsg->getApiData();
215 public function getMessageObject() {
216 return $this->status
->getMessage();
222 public function __toString() {
223 $enMsg = clone $this->getApiMessage();
224 $enMsg->inLanguage( 'en' )->useDatabase( false );
225 $text = ApiErrorFormatter
::stripMarkup( $enMsg->text() );
227 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
228 . "in {$this->getFile()}:{$this->getLine()}\n"
229 . "Stack trace:\n{$this->getTraceAsString()}";