MediaWiki  master
ApiUsageException.php
Go to the documentation of this file.
1 <?php
30 
31  protected $modulePath;
32  protected $status;
33 
42  public function __construct(
43  ?ApiBase $module, StatusValue $status, $httpCode = 0, Throwable $previous = null
44  ) {
45  if ( $status->isOK() ) {
46  throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
47  }
48 
49  $this->modulePath = $module ? $module->getModulePath() : null;
50  $this->status = $status;
51 
52  // Bug T46111: Messages in the log files should be in English and not
53  // customized by the local wiki.
54  $enMsg = clone $this->getApiMessage();
55  $enMsg->inLanguage( 'en' )->useDatabase( false );
56  parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous );
57  }
58 
68  public static function newWithMessage(
69  ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, Throwable $previous = null
70  ) {
71  return new static(
72  $module,
73  StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
74  $httpCode,
75  $previous
76  );
77  }
78 
82  private function getApiMessage() {
83  $errors = $this->status->getErrorsByType( 'error' );
84  if ( !$errors ) {
85  $errors = $this->status->getErrors();
86  }
87  if ( !$errors ) {
88  $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
89  } else {
90  $msg = ApiMessage::create( $errors[0] );
91  }
92  return $msg;
93  }
94 
99  public function getModulePath() {
100  return $this->modulePath;
101  }
102 
107  public function getStatusValue() {
108  return $this->status;
109  }
110 
114  public function getMessageObject() {
115  return Status::wrap( $this->status )->getMessage();
116  }
117 
121  public function __toString() {
122  $enMsg = clone $this->getApiMessage();
123  $enMsg->inLanguage( 'en' )->useDatabase( false );
124  $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
125 
126  return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
127  . "in {$this->getFile()}:{$this->getLine()}\n"
128  . "Stack trace:\n{$this->getTraceAsString()}"
129  . $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "";
130  }
131 
132 }
ApiUsageException\getStatusValue
getStatusValue()
Fetch the error status.
Definition: ApiUsageException.php:107
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:29
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:43
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
ApiUsageException\newWithMessage
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
Definition: ApiUsageException.php:68
ApiUsageException\getModulePath
getModulePath()
Fetch the responsible module name.
Definition: ApiUsageException.php:99
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiMessage
Extension of Message implementing IApiMessage @newable.
Definition: ApiMessage.php:27
ApiUsageException\$modulePath
$modulePath
Definition: ApiUsageException.php:31
MWException
MediaWiki exception.
Definition: MWException.php:29
ApiErrorFormatter\stripMarkup
static stripMarkup( $text)
Turn wikitext into something resembling plaintext.
Definition: ApiErrorFormatter.php:291
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:62
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:509
ApiUsageException\__construct
__construct(?ApiBase $module, StatusValue $status, $httpCode=0, Throwable $previous=null)
Stable to call.
Definition: ApiUsageException.php:42
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:42
ILocalizedException
Interface for MediaWiki-localized exceptions.
Definition: ILocalizedException.php:29
ApiUsageException\getMessageObject
getMessageObject()
Return a Message object for this exception.Message
Definition: ApiUsageException.php:114
ApiUsageException\__toString
__toString()
Definition: ApiUsageException.php:121
ApiUsageException\getApiMessage
getApiMessage()
Definition: ApiUsageException.php:82
ApiUsageException\$status
$status
Definition: ApiUsageException.php:32