MediaWiki REL1_34
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}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
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=null, $msg, $code=null, $data=null, $httpCode=0)
__construct(ApiBase $module=null, StatusValue $status, $httpCode=0)
getMessageObject()
Return a Message object for this exception.Message
MediaWiki exception.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Interface for MediaWiki-localized exceptions.