MediaWiki master
ApiUsageException.php
Go to the documentation of this file.
1<?php
22
31class ApiUsageException extends MWException implements Stringable, ILocalizedException {
32
33 protected $modulePath;
34 protected $status;
35
44 public function __construct(
45 ?ApiBase $module, StatusValue $status, $httpCode = 0, Throwable $previous = null
46 ) {
47 if ( $status->isOK() ) {
48 throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
49 }
50
51 $this->modulePath = $module ? $module->getModulePath() : null;
52 $this->status = $status;
53
54 // Bug T46111: Messages in the log files should be in English and not
55 // customized by the local wiki.
56 $enMsg = clone $this->getApiMessage();
57 $enMsg->inLanguage( 'en' )->useDatabase( false );
58 parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous );
59 }
60
70 public static function newWithMessage(
71 ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, Throwable $previous = null
72 ) {
73 return new static(
74 $module,
75 StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
76 $httpCode,
77 $previous
78 );
79 }
80
84 private function getApiMessage() {
85 // Return the first error message, if any; or the first warning message, if any; or a generic message
86 foreach ( $this->status->getMessages( 'error' ) as $msg ) {
87 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
88 return ApiMessage::create( $msg );
89 }
90 foreach ( $this->status->getMessages( 'warning' ) as $msg ) {
91 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
92 return ApiMessage::create( $msg );
93 }
94 return new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
95 }
96
101 public function getModulePath() {
102 return $this->modulePath;
103 }
104
109 public function getStatusValue() {
110 return $this->status;
111 }
112
116 public function getMessageObject() {
117 return Status::wrap( $this->status )->getMessage();
118 }
119
123 public function __toString() {
124 $enMsg = clone $this->getApiMessage();
125 $enMsg->inLanguage( 'en' )->useDatabase( false );
126 $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
127
128 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
129 . "in {$this->getFile()}:{$this->getLine()}\n"
130 . "Stack trace:\n{$this->getTraceAsString()}"
131 . ( $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "" );
132 }
133
134}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:65
getModulePath()
Get the path to this module.
Definition ApiBase.php:621
static stripMarkup( $text)
Turn wikitext into something resembling plaintext.
Extension of Message implementing IApiMessage.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Exception used to abort API execution with an error.
getModulePath()
Fetch the responsible module name.
getStatusValue()
Fetch the error status.
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
getMessageObject()
Return a Message object for this exception.Message
__construct(?ApiBase $module, StatusValue $status, $httpCode=0, Throwable $previous=null)
MediaWiki exception.
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.