MediaWiki  1.34.0
ApiUsageException.php
Go to the documentation of this file.
1 <?php
29 
30  protected $modulePath;
31  protected $status;
32 
38  public function __construct(
39  ApiBase $module = null, StatusValue $status, $httpCode = 0
40  ) {
41  if ( $status->isOK() ) {
42  throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
43  }
44 
45  $this->modulePath = $module ? $module->getModulePath() : null;
46  $this->status = $status;
47 
48  // Bug T46111: Messages in the log files should be in English and not
49  // customized by the local wiki.
50  $enMsg = clone $this->getApiMessage();
51  $enMsg->inLanguage( 'en' )->useDatabase( false );
52  parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode );
53  }
54 
63  public static function newWithMessage(
64  ApiBase $module = null, $msg, $code = null, $data = null, $httpCode = 0
65  ) {
66  return new static(
67  $module,
68  StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
69  $httpCode
70  );
71  }
72 
76  private function getApiMessage() {
77  $errors = $this->status->getErrorsByType( 'error' );
78  if ( !$errors ) {
79  $errors = $this->status->getErrors();
80  }
81  if ( !$errors ) {
82  $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
83  } else {
84  $msg = ApiMessage::create( $errors[0] );
85  }
86  return $msg;
87  }
88 
93  public function getModulePath() {
94  return $this->modulePath;
95  }
96 
101  public function getStatusValue() {
102  return $this->status;
103  }
104 
108  public function getMessageObject() {
109  return Status::wrap( $this->status )->getMessage();
110  }
111 
115  public function __toString() {
116  $enMsg = clone $this->getApiMessage();
117  $enMsg->inLanguage( 'en' )->useDatabase( false );
118  $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
119 
120  return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
121  . "in {$this->getFile()}:{$this->getLine()}\n"
122  . "Stack trace:\n{$this->getTraceAsString()}";
123  }
124 
125 }
ApiUsageException\getStatusValue
getStatusValue()
Fetch the error status.
Definition: ApiUsageException.php:101
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
ApiUsageException\__construct
__construct(ApiBase $module=null, StatusValue $status, $httpCode=0)
Definition: ApiUsageException.php:38
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
ApiUsageException\getModulePath
getModulePath()
Fetch the responsible module name.
Definition: ApiUsageException.php:93
ApiUsageException\newWithMessage
static newWithMessage(ApiBase $module=null, $msg, $code=null, $data=null, $httpCode=0)
Definition: ApiUsageException.php:63
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
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:55
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:108
ApiUsageException\__toString
__toString()
Definition: ApiUsageException.php:115
ApiUsageException\getApiMessage
getApiMessage()
Definition: ApiUsageException.php:76
ApiUsageException\$status
$status
Definition: ApiUsageException.php:31