MediaWiki master
ApiUsageException.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Api;
22
24use InvalidArgumentException;
26use MWException;
27use StatusValue;
28use Stringable;
29use Throwable;
31
40class ApiUsageException extends MWException implements Stringable, ILocalizedException {
41
43 protected $modulePath;
45 protected $status;
46
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' );
60 }
61
62 $this->modulePath = $module ? $module->getModulePath() : null;
63 $this->status = $status;
64
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 );
70 }
71
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
89 );
90 }
91
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 );
100 }
101 foreach ( $this->status->getMessages( 'warning' ) as $msg ) {
102 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
103 return ApiMessage::create( $msg );
104 }
105 return new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
106 }
107
112 public function getModulePath() {
113 return $this->modulePath;
114 }
115
120 public function getStatusValue() {
121 return $this->status;
122 }
123
127 public function getMessageObject() {
128 return Status::wrap( $this->status )->getMessage();
129 }
130
134 public function __toString() {
135 $enMsg = clone $this->getApiMessage();
136 $enMsg->inLanguage( 'en' )->useDatabase( false );
137 $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
138
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()}" : "" );
143 }
144
145}
146
148class_alias( ApiUsageException::class, 'ApiUsageException' );
MediaWiki exception.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
getModulePath()
Get the path to this module.
Definition ApiBase.php:650
static stripMarkup( $text)
Turn wikitext into something resembling plaintext.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Exception used to abort API execution with an error.
__construct(?ApiBase $module, StatusValue $status, $httpCode=0, Throwable $previous=null)
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
getModulePath()
Fetch the responsible module name.
getMessageObject()
Return a Message object for this exception.Message
getStatusValue()
Fetch the error status.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Generic operation result class Has warning/error list, boolean status and arbitrary value.
isOK()
Returns whether the operation completed.
Interface for MediaWiki-localized exceptions.