MediaWiki  master
ApiUsageException.php
Go to the documentation of this file.
1 <?php
29 
30  protected $modulePath;
31  protected $status;
32 
39  public function __construct(
40  ?ApiBase $module, StatusValue $status, $httpCode = 0, Throwable $previous = null
41  ) {
42  if ( $status->isOK() ) {
43  throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
44  }
45 
46  $this->modulePath = $module ? $module->getModulePath() : null;
47  $this->status = $status;
48 
49  // Bug T46111: Messages in the log files should be in English and not
50  // customized by the local wiki.
51  $enMsg = clone $this->getApiMessage();
52  $enMsg->inLanguage( 'en' )->useDatabase( false );
53  parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous );
54  }
55 
65  public static function newWithMessage(
66  ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, Throwable $previous = null
67  ) {
68  return new static(
69  $module,
70  StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
71  $httpCode,
72  $previous
73  );
74  }
75 
79  private function getApiMessage() {
80  $errors = $this->status->getErrorsByType( 'error' );
81  if ( !$errors ) {
82  $errors = $this->status->getErrors();
83  }
84  if ( !$errors ) {
85  $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
86  } else {
87  $msg = ApiMessage::create( $errors[0] );
88  }
89  return $msg;
90  }
91 
96  public function getModulePath() {
97  return $this->modulePath;
98  }
99 
104  public function getStatusValue() {
105  return $this->status;
106  }
107 
111  public function getMessageObject() {
112  return Status::wrap( $this->status )->getMessage();
113  }
114 
118  public function __toString() {
119  $enMsg = clone $this->getApiMessage();
120  $enMsg->inLanguage( 'en' )->useDatabase( false );
121  $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
122 
123  return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
124  . "in {$this->getFile()}:{$this->getLine()}\n"
125  . "Stack trace:\n{$this->getTraceAsString()}"
126  . $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "";
127  }
128 
129 }
ApiUsageException\getStatusValue
getStatusValue()
Fetch the error status.
Definition: ApiUsageException.php:104
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:28
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:42
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
ApiUsageException\newWithMessage
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
Definition: ApiUsageException.php:65
ApiUsageException\getModulePath
getModulePath()
Fetch the responsible module name.
Definition: ApiUsageException.php:96
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:50
ApiMessage
Extension of Message implementing IApiMessage.
Definition: ApiMessage.php:26
ApiUsageException\$modulePath
$modulePath
Definition: ApiUsageException.php:30
MWException
MediaWiki exception.
Definition: MWException.php:26
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:60
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:490
ApiUsageException\__construct
__construct(?ApiBase $module, StatusValue $status, $httpCode=0, Throwable $previous=null)
Definition: ApiUsageException.php:39
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40
ILocalizedException
Interface for MediaWiki-localized exceptions.
Definition: ILocalizedException.php:27
ApiUsageException\getMessageObject
getMessageObject()
Return a Message object for this exception.Message
Definition: ApiUsageException.php:111
ApiUsageException\__toString
__toString()
Definition: ApiUsageException.php:118
ApiUsageException\getApiMessage
getApiMessage()
Definition: ApiUsageException.php:79
ApiUsageException\$status
$status
Definition: ApiUsageException.php:31