39 public function __construct( $httpCode, $content, $header =
null ) {
40 parent::__construct( (
string)$content );
41 $this->httpCode = (int)$httpCode;
42 $this->header = $header;
43 $this->content = $content;
65 return $this->httpCode;
76 HttpStatus::header( $this->httpCode );
77 header(
'Content-type: text/html; charset=utf-8' );
78 ContentSecurityPolicy::sendRestrictiveHeader();
83 private function doLog() {
84 $logger = LoggerFactory::getInstance(
'HttpError' );
85 $content = $this->content;
87 if ( $content instanceof
Message ) {
88 $content = $content->
text();
92 'file' => $this->getFile(),
93 'line' => $this->getLine(),
94 'http_code' => $this->httpCode,
97 $logMsg =
"$content ({http_code}) from {file}:{line}";
100 $logger->info( $logMsg, $context );
102 $logger->error( $logMsg, $context );
113 if ( $this->header ===
null ) {
114 $titleHtml = htmlspecialchars( HttpStatus::getMessage( $this->httpCode ) );
115 } elseif ( $this->header instanceof
Message ) {
116 $titleHtml = $this->header->escaped();
118 $titleHtml = htmlspecialchars( $this->header );
121 if ( $this->content instanceof
Message ) {
122 $contentHtml = $this->content->escaped();
124 $contentHtml = nl2br( htmlspecialchars( $this->content ) );
127 return "<!DOCTYPE html>\n" .
128 "<html><head><title>$titleHtml</title><meta name=\"color-scheme\" content=\"light dark\" /></head>\n" .
129 "<body><h1>$titleHtml</h1><p>$contentHtml</p></body></html>\n";
134class_alias( HttpError::class,
'HttpError' );
Handle sending Content-Security-Policy headers.