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
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' );
59 }
60
61 $this->modulePath = $module ? $module->getModulePath() : null;
62 $this->status = $status;
63
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 );
69 }
70
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
88 );
89 }
90
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 );
99 }
100 foreach ( $this->status->getMessages( 'warning' ) as $msg ) {
101 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
102 return ApiMessage::create( $msg );
103 }
104 return new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
105 }
106
111 public function getModulePath() {
112 return $this->modulePath;
113 }
114
119 public function getStatusValue() {
120 return $this->status;
121 }
122
126 public function getMessageObject() {
127 return Status::wrap( $this->status )->getMessage();
128 }
129
133 public function __toString() {
134 $enMsg = clone $this->getApiMessage();
135 $enMsg->inLanguage( 'en' )->useDatabase( false );
136 $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
137
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()}" : "" );
142 }
143
144}
145
147class_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.
getModulePath()
Fetch the responsible module name.
getMessageObject()
Return a Message object for this exception.Message
__construct(?ApiBase $module, StatusValue $status, $httpCode=0, ?Throwable $previous=null)
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, ?Throwable $previous=null)
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.