MediaWiki REL1_39
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}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:56
getModulePath()
Get the path to this module.
Definition ApiBase.php:573
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.
isOK()
Returns whether the operation completed.
Interface for MediaWiki-localized exceptions.