Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
48.57% |
17 / 35 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
HttpError | |
50.00% |
17 / 34 |
|
66.67% |
4 / 6 |
26.12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
isLoggable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
report | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
doLog | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getHTML | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 |
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 | namespace MediaWiki\Exception; |
22 | |
23 | use MediaWiki\Logger\LoggerFactory; |
24 | use MediaWiki\Message\Message; |
25 | use Wikimedia\Http\HttpStatus; |
26 | |
27 | /** |
28 | * Show an error that looks like an HTTP server error. |
29 | * Replacement for wfHttpError(). |
30 | * |
31 | * @newable |
32 | * @stable to extend |
33 | * @since 1.19 |
34 | * @ingroup Exception |
35 | */ |
36 | class HttpError extends MWException { |
37 | /** @var int */ |
38 | private $httpCode; |
39 | /** @var string|Message|null */ |
40 | private $header; |
41 | /** @var string|Message */ |
42 | private $content; |
43 | |
44 | /** |
45 | * @stable to call |
46 | * @param int $httpCode HTTP status code to send to the client |
47 | * @param string|Message $content Content of the message |
48 | * @param string|Message|null $header Content of the header (\<title\> and \<h1\>) |
49 | * @param-taint $content tainted |
50 | * @param-taint $header tainted |
51 | */ |
52 | public function __construct( $httpCode, $content, $header = null ) { |
53 | parent::__construct( (string)$content ); |
54 | $this->httpCode = (int)$httpCode; |
55 | $this->header = $header; |
56 | $this->content = $content; |
57 | } |
58 | |
59 | /** |
60 | * We don't want the default exception logging as we got our own logging set |
61 | * up in self::report. |
62 | * |
63 | * @see MWException::isLoggable |
64 | * |
65 | * @since 1.24 |
66 | * @return bool |
67 | */ |
68 | public function isLoggable() { |
69 | return false; |
70 | } |
71 | |
72 | /** |
73 | * Returns the HTTP status code supplied to the constructor. |
74 | * |
75 | * @return int |
76 | */ |
77 | public function getStatusCode() { |
78 | return $this->httpCode; |
79 | } |
80 | |
81 | /** |
82 | * Report and log the HTTP error. |
83 | * Sends the appropriate HTTP status code and outputs an |
84 | * HTML page with an error message. |
85 | */ |
86 | public function report() { |
87 | $this->doLog(); |
88 | |
89 | HttpStatus::header( $this->httpCode ); |
90 | header( 'Content-type: text/html; charset=utf-8' ); |
91 | |
92 | print $this->getHTML(); |
93 | } |
94 | |
95 | private function doLog() { |
96 | $logger = LoggerFactory::getInstance( 'HttpError' ); |
97 | $content = $this->content; |
98 | |
99 | if ( $content instanceof Message ) { |
100 | $content = $content->text(); |
101 | } |
102 | |
103 | $context = [ |
104 | 'file' => $this->getFile(), |
105 | 'line' => $this->getLine(), |
106 | 'http_code' => $this->httpCode, |
107 | ]; |
108 | |
109 | $logMsg = "$content ({http_code}) from {file}:{line}"; |
110 | |
111 | if ( $this->getStatusCode() < 500 ) { |
112 | $logger->info( $logMsg, $context ); |
113 | } else { |
114 | $logger->error( $logMsg, $context ); |
115 | } |
116 | } |
117 | |
118 | /** |
119 | * Returns HTML for reporting the HTTP error. |
120 | * This will be a minimal but complete HTML document. |
121 | * |
122 | * @return string HTML |
123 | */ |
124 | public function getHTML() { |
125 | if ( $this->header === null ) { |
126 | $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) ); |
127 | } elseif ( $this->header instanceof Message ) { |
128 | $titleHtml = $this->header->escaped(); |
129 | } else { |
130 | $titleHtml = htmlspecialchars( $this->header ); |
131 | } |
132 | |
133 | if ( $this->content instanceof Message ) { |
134 | $contentHtml = $this->content->escaped(); |
135 | } else { |
136 | $contentHtml = nl2br( htmlspecialchars( $this->content ) ); |
137 | } |
138 | |
139 | return "<!DOCTYPE html>\n" . |
140 | "<html><head><title>$titleHtml</title><meta name=\"color-scheme\" content=\"light dark\" /></head>\n" . |
141 | "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n"; |
142 | } |
143 | } |
144 | |
145 | /** @deprecated class alias since 1.44 */ |
146 | class_alias( HttpError::class, 'HttpError' ); |