Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiUsageException.php
blob8393445b375d83e430bc1cb91a510c799fae413f
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 /**
49 * @stable to call
50 * @param ApiBase|null $module API module responsible for the error, if known
51 * @param StatusValue $status Status holding errors
52 * @param int $httpCode HTTP error code to use
53 * @param Throwable|null $previous Previous exception
55 public function __construct(
56 ?ApiBase $module, StatusValue $status, $httpCode = 0, ?Throwable $previous = null
57 ) {
58 if ( $status->isOK() ) {
59 throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
62 $this->modulePath = $module ? $module->getModulePath() : null;
63 $this->status = $status;
65 // Bug T46111: Messages in the log files should be in English and not
66 // customized by the local wiki.
67 $enMsg = clone $this->getApiMessage();
68 $enMsg->inLanguage( 'en' )->useDatabase( false );
69 parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous );
72 /**
73 * @param ApiBase|null $module API module responsible for the error, if known
74 * @param string|array|MessageSpecifier $msg See ApiMessage::create()
75 * @param string|null $code See ApiMessage::create()
76 * @param array|null $data See ApiMessage::create()
77 * @param int $httpCode HTTP error code to use
78 * @param Throwable|null $previous Previous exception
79 * @return static
81 public static function newWithMessage(
82 ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, ?Throwable $previous = null
83 ) {
84 return new static(
85 $module,
86 StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
87 $httpCode,
88 $previous
92 /**
93 * @return ApiMessage
95 private function getApiMessage() {
96 // Return the first error message, if any; or the first warning message, if any; or a generic message
97 foreach ( $this->status->getMessages( 'error' ) as $msg ) {
98 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
99 return ApiMessage::create( $msg );
101 foreach ( $this->status->getMessages( 'warning' ) as $msg ) {
102 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
103 return ApiMessage::create( $msg );
105 return new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
109 * Fetch the responsible module name
110 * @return string|null
112 public function getModulePath() {
113 return $this->modulePath;
117 * Fetch the error status
118 * @return StatusValue
120 public function getStatusValue() {
121 return $this->status;
125 * @inheritDoc
127 public function getMessageObject() {
128 return Status::wrap( $this->status )->getMessage();
132 * @return string
134 public function __toString() {
135 $enMsg = clone $this->getApiMessage();
136 $enMsg->inLanguage( 'en' )->useDatabase( false );
137 $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
139 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
140 . "in {$this->getFile()}:{$this->getLine()}\n"
141 . "Stack trace:\n{$this->getTraceAsString()}"
142 . ( $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "" );
147 /** @deprecated class alias since 1.43 */
148 class_alias( ApiUsageException::class, 'ApiUsageException' );