Merge "mediawiki.api: Remove console warning for legacy token type"
[mediawiki.git] / includes / api / ApiUsageException.php
blob4354fcd30544d16c2d938f0b91050a6e42e3130f
1 <?php
2 /**
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
18 * @file
21 namespace MediaWiki\Api;
23 use ILocalizedException;
24 use InvalidArgumentException;
25 use MediaWiki\Status\Status;
26 use MWException;
27 use StatusValue;
28 use Stringable;
29 use Throwable;
30 use Wikimedia\Message\MessageSpecifier;
32 /**
33 * Exception used to abort API execution with an error
35 * If possible, use ApiBase::dieWithError() instead of throwing this directly.
37 * @newable
38 * @ingroup API
40 class ApiUsageException extends MWException implements Stringable, ILocalizedException {
42 /** @var string|null */
43 protected $modulePath;
44 /** @var StatusValue */
45 protected $status;
47 /**
48 * @stable to call
49 * @param ApiBase|null $module API module responsible for the error, if known
50 * @param StatusValue $status Status holding errors
51 * @param int $httpCode HTTP error code to use
52 * @param Throwable|null $previous Previous exception
54 public function __construct(
55 ?ApiBase $module, StatusValue $status, $httpCode = 0, ?Throwable $previous = null
56 ) {
57 if ( $status->isOK() ) {
58 throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
61 $this->modulePath = $module ? $module->getModulePath() : null;
62 $this->status = $status;
64 // Bug T46111: Messages in the log files should be in English and not
65 // customized by the local wiki.
66 $enMsg = clone $this->getApiMessage();
67 $enMsg->inLanguage( 'en' )->useDatabase( false );
68 parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous );
71 /**
72 * @param ApiBase|null $module API module responsible for the error, if known
73 * @param string|array|MessageSpecifier $msg See ApiMessage::create()
74 * @param string|null $code See ApiMessage::create()
75 * @param array|null $data See ApiMessage::create()
76 * @param int $httpCode HTTP error code to use
77 * @param Throwable|null $previous Previous exception
78 * @return static
80 public static function newWithMessage(
81 ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, ?Throwable $previous = null
82 ) {
83 return new static(
84 $module,
85 StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
86 $httpCode,
87 $previous
91 /**
92 * @return ApiMessage
94 private function getApiMessage() {
95 // Return the first error message, if any; or the first warning message, if any; or a generic message
96 foreach ( $this->status->getMessages( 'error' ) as $msg ) {
97 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
98 return ApiMessage::create( $msg );
100 foreach ( $this->status->getMessages( 'warning' ) as $msg ) {
101 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
102 return ApiMessage::create( $msg );
104 return new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
108 * Fetch the responsible module name
109 * @return string|null
111 public function getModulePath() {
112 return $this->modulePath;
116 * Fetch the error status
117 * @return StatusValue
119 public function getStatusValue() {
120 return $this->status;
124 * @inheritDoc
126 public function getMessageObject() {
127 return Status::wrap( $this->status )->getMessage();
131 * @return string
133 public function __toString() {
134 $enMsg = clone $this->getApiMessage();
135 $enMsg->inLanguage( 'en' )->useDatabase( false );
136 $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
138 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
139 . "in {$this->getFile()}:{$this->getLine()}\n"
140 . "Stack trace:\n{$this->getTraceAsString()}"
141 . ( $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "" );
146 /** @deprecated class alias since 1.43 */
147 class_alias( ApiUsageException::class, 'ApiUsageException' );