ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PhpError.php
Go to the documentation of this file.
1 <?php
9 namespace Slim\Handlers;
10 
13 use Slim\Http\Body;
15 
22 class PhpError extends AbstractError
23 {
35  {
36  $contentType = $this->determineContentType($request);
37  switch ($contentType) {
38  case 'application/json':
39  $output = $this->renderJsonErrorMessage($error);
40  break;
41 
42  case 'text/xml':
43  case 'application/xml':
44  $output = $this->renderXmlErrorMessage($error);
45  break;
46 
47  case 'text/html':
48  $output = $this->renderHtmlErrorMessage($error);
49  break;
50  default:
51  throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType);
52  }
53 
54  $this->writeToErrorLog($error);
55 
56  $body = new Body(fopen('php://temp', 'r+'));
57  $body->write($output);
58 
59  return $response
60  ->withStatus(500)
61  ->withHeader('Content-type', $contentType)
62  ->withBody($body);
63  }
64 
72  protected function renderHtmlErrorMessage(\Throwable $error)
73  {
74  $title = 'Slim Application Error';
75 
76  if ($this->displayErrorDetails) {
77  $html = '<p>The application could not run because of the following error:</p>';
78  $html .= '<h2>Details</h2>';
79  $html .= $this->renderHtmlError($error);
80 
81  while ($error = $error->getPrevious()) {
82  $html .= '<h2>Previous error</h2>';
83  $html .= $this->renderHtmlError($error);
84  }
85  } else {
86  $html = '<p>A website error has occurred. Sorry for the temporary inconvenience.</p>';
87  }
88 
89  $output = sprintf(
90  "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
91  "<title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana," .
92  "sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{" .
93  "display:inline-block;width:65px;}</style></head><body><h1>%s</h1>%s</body></html>",
94  $title,
95  $title,
96  $html
97  );
98 
99  return $output;
100  }
101 
109  protected function renderHtmlError(\Throwable $error)
110  {
111  $html = sprintf('<div><strong>Type:</strong> %s</div>', get_class($error));
112 
113  if (($code = $error->getCode())) {
114  $html .= sprintf('<div><strong>Code:</strong> %s</div>', $code);
115  }
116 
117  if (($message = $error->getMessage())) {
118  $html .= sprintf('<div><strong>Message:</strong> %s</div>', htmlentities($message));
119  }
120 
121  if (($file = $error->getFile())) {
122  $html .= sprintf('<div><strong>File:</strong> %s</div>', $file);
123  }
124 
125  if (($line = $error->getLine())) {
126  $html .= sprintf('<div><strong>Line:</strong> %s</div>', $line);
127  }
128 
129  if (($trace = $error->getTraceAsString())) {
130  $html .= '<h2>Trace</h2>';
131  $html .= sprintf('<pre>%s</pre>', htmlentities($trace));
132  }
133 
134  return $html;
135  }
136 
144  protected function renderJsonErrorMessage(\Throwable $error)
145  {
146  $json = [
147  'message' => 'Slim Application Error',
148  ];
149 
150  if ($this->displayErrorDetails) {
151  $json['error'] = [];
152 
153  do {
154  $json['error'][] = [
155  'type' => get_class($error),
156  'code' => $error->getCode(),
157  'message' => $error->getMessage(),
158  'file' => $error->getFile(),
159  'line' => $error->getLine(),
160  'trace' => explode("\n", $error->getTraceAsString()),
161  ];
162  } while ($error = $error->getPrevious());
163  }
164 
165  return json_encode($json, JSON_PRETTY_PRINT);
166  }
167 
175  protected function renderXmlErrorMessage(\Throwable $error)
176  {
177  $xml = "<error>\n <message>Slim Application Error</message>\n";
178  if ($this->displayErrorDetails) {
179  do {
180  $xml .= " <error>\n";
181  $xml .= " <type>" . get_class($error) . "</type>\n";
182  $xml .= " <code>" . $error->getCode() . "</code>\n";
183  $xml .= " <message>" . $this->createCdataSection($error->getMessage()) . "</message>\n";
184  $xml .= " <file>" . $error->getFile() . "</file>\n";
185  $xml .= " <line>" . $error->getLine() . "</line>\n";
186  $xml .= " <trace>" . $this->createCdataSection($error->getTraceAsString()) . "</trace>\n";
187  $xml .= " </error>\n";
188  } while ($error = $error->getPrevious());
189  }
190  $xml .= "</error>";
191 
192  return $xml;
193  }
194 
201  private function createCdataSection($content)
202  {
203  return sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $content));
204  }
205 }
Representation of an incoming, server-side HTTP request.
foreach($paths as $path) $request
Definition: asyncclient.php:32
$code
Definition: example_050.php:99
withStatus($code, $reasonPhrase='')
Return an instance with the specified status code and, optionally, reason phrase. ...
renderXmlErrorMessage(\Throwable $error)
Render XML error.
Definition: PhpError.php:175
determineContentType(ServerRequestInterface $request)
Determine which content type we know about is wanted using Accept header.
writeToErrorLog($throwable)
Write to the error log if displayErrorDetails is false.
catch(Exception $e) $message
renderHtmlErrorMessage(\Throwable $error)
Render HTML error page.
Definition: PhpError.php:72
createCdataSection($content)
Returns a CDATA section with the given content.
Definition: PhpError.php:201
Representation of an outgoing, server-side response.
Abstract Slim application error handler.
renderHtmlError(\Throwable $error)
Render error as HTML.
Definition: PhpError.php:109
renderJsonErrorMessage(\Throwable $error)
Render JSON error.
Definition: PhpError.php:144
Body.
Definition: Body.php:19
if($path[strlen($path) - 1]==='/') if(is_dir($path)) if(!file_exists($path)) if(preg_match('#\.php$#D', mb_strtolower($path, 'UTF-8'))) $contentType
Definition: module.php:144
Slim Framework (https://slimframework.com)
Default Slim application error handler for PHP 7+ Throwables.
Definition: PhpError.php:22
__invoke(ServerRequestInterface $request, ResponseInterface $response, \Throwable $error)
Invoke error handler.
Definition: PhpError.php:34
$response
$html
Definition: example_001.php:87