Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
60.00% |
18 / 30 |
|
28.57% |
2 / 7 |
CRAP | |
0.00% |
0 / 1 |
ApiUsageException | |
60.00% |
18 / 30 |
|
28.57% |
2 / 7 |
21.22 | |
0.00% |
0 / 1 |
__construct | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
newWithMessage | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
getApiMessage | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
getModulePath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getStatusValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMessageObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\Status\Status; |
22 | |
23 | /** |
24 | * Exception used to abort API execution with an error |
25 | * |
26 | * If possible, use ApiBase::dieWithError() instead of throwing this directly. |
27 | * |
28 | * @newable |
29 | * @ingroup API |
30 | */ |
31 | class ApiUsageException extends MWException implements ILocalizedException { |
32 | |
33 | protected $modulePath; |
34 | protected $status; |
35 | |
36 | /** |
37 | * |
38 | * @stable to call |
39 | * @param ApiBase|null $module API module responsible for the error, if known |
40 | * @param StatusValue $status Status holding errors |
41 | * @param int $httpCode HTTP error code to use |
42 | * @param Throwable|null $previous Previous exception |
43 | */ |
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 | |
61 | /** |
62 | * @param ApiBase|null $module API module responsible for the error, if known |
63 | * @param string|array|Message $msg See ApiMessage::create() |
64 | * @param string|null $code See ApiMessage::create() |
65 | * @param array|null $data See ApiMessage::create() |
66 | * @param int $httpCode HTTP error code to use |
67 | * @param Throwable|null $previous Previous exception |
68 | * @return static |
69 | */ |
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 | |
81 | /** |
82 | * @return ApiMessage |
83 | */ |
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 | |
97 | /** |
98 | * Fetch the responsible module name |
99 | * @return string|null |
100 | */ |
101 | public function getModulePath() { |
102 | return $this->modulePath; |
103 | } |
104 | |
105 | /** |
106 | * Fetch the error status |
107 | * @return StatusValue |
108 | */ |
109 | public function getStatusValue() { |
110 | return $this->status; |
111 | } |
112 | |
113 | /** |
114 | * @inheritDoc |
115 | */ |
116 | public function getMessageObject() { |
117 | return Status::wrap( $this->status )->getMessage(); |
118 | } |
119 | |
120 | /** |
121 | * @return string |
122 | */ |
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 | } |