MediaWiki master
ApiUsageException.php
Go to the documentation of this file.
1<?php
22
32
33 protected $modulePath;
34 protected $status;
35
44 public function __construct(
45 ?ApiBase $module, StatusValue $status, $httpCode = 0, Throwable $previous = null
46 ) {
47 if ( $status->isOK() ) {
48 throw new InvalidArgumentException( __METHOD__ . ' requires a fatal Status' );
49 }
50
51 $this->modulePath = $module ? $module->getModulePath() : null;
52 $this->status = $status;
53
54 // Bug T46111: Messages in the log files should be in English and not
55 // customized by the local wiki.
56 $enMsg = clone $this->getApiMessage();
57 $enMsg->inLanguage( 'en' )->useDatabase( false );
58 parent::__construct( ApiErrorFormatter::stripMarkup( $enMsg->text() ), $httpCode, $previous );
59 }
60
70 public static function newWithMessage(
71 ?ApiBase $module, $msg, $code = null, $data = null, $httpCode = 0, Throwable $previous = null
72 ) {
73 return new static(
74 $module,
75 StatusValue::newFatal( ApiMessage::create( $msg, $code, $data ) ),
76 $httpCode,
77 $previous
78 );
79 }
80
84 private function getApiMessage() {
85 $errors = $this->status->getErrorsByType( 'error' );
86 if ( !$errors ) {
87 $errors = $this->status->getErrors();
88 }
89 if ( !$errors ) {
90 $msg = new ApiMessage( 'apierror-unknownerror-nocode', 'unknownerror' );
91 } else {
92 $msg = ApiMessage::create( $errors[0] );
93 }
94 return $msg;
95 }
96
101 public function getModulePath() {
102 return $this->modulePath;
103 }
104
109 public function getStatusValue() {
110 return $this->status;
111 }
112
116 public function getMessageObject() {
117 return Status::wrap( $this->status )->getMessage();
118 }
119
123 public function __toString() {
124 $enMsg = clone $this->getApiMessage();
125 $enMsg->inLanguage( 'en' )->useDatabase( false );
126 $text = ApiErrorFormatter::stripMarkup( $enMsg->text() );
127
128 return get_class( $this ) . ": {$enMsg->getApiCode()}: {$text} "
129 . "in {$this->getFile()}:{$this->getLine()}\n"
130 . "Stack trace:\n{$this->getTraceAsString()}"
131 . ( $this->getPrevious() ? "\n\nNext {$this->getPrevious()}" : "" );
132 }
133
134}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:64
getModulePath()
Get the path to this module.
Definition ApiBase.php:620
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.
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.