ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Slim\Handlers\PhpError Class Reference

Default Slim application error handler for PHP 7+ Throwables. More...

+ Inheritance diagram for Slim\Handlers\PhpError:
+ Collaboration diagram for Slim\Handlers\PhpError:

Public Member Functions

 __invoke (ServerRequestInterface $request, ResponseInterface $response, \Throwable $error)
 Invoke error handler. More...
 
- Public Member Functions inherited from Slim\Handlers\AbstractError
 __construct ($displayErrorDetails=false)
 Constructor. More...
 

Protected Member Functions

 renderHtmlErrorMessage (\Throwable $error)
 Render HTML error page. More...
 
 renderHtmlError (\Throwable $error)
 Render error as HTML. More...
 
 renderJsonErrorMessage (\Throwable $error)
 Render JSON error. More...
 
 renderXmlErrorMessage (\Throwable $error)
 Render XML error. More...
 
- Protected Member Functions inherited from Slim\Handlers\AbstractError
 writeToErrorLog ($throwable)
 Write to the error log if displayErrorDetails is false. More...
 
 renderThrowableAsText ($throwable)
 Render error as Text. More...
 
 logError ($message)
 Wraps the error_log function so that this can be easily tested. More...
 
- Protected Member Functions inherited from Slim\Handlers\AbstractHandler
 determineContentType (ServerRequestInterface $request)
 Determine which content type we know about is wanted using Accept header. More...
 

Private Member Functions

 createCdataSection ($content)
 Returns a CDATA section with the given content. More...
 

Additional Inherited Members

- Protected Attributes inherited from Slim\Handlers\AbstractError
 $displayErrorDetails
 
- Protected Attributes inherited from Slim\Handlers\AbstractHandler
 $knownContentTypes
 

Detailed Description

Default Slim application error handler for PHP 7+ Throwables.

It outputs the error message and diagnostic information in either JSON, XML, or HTML based on the Accept header.

Definition at line 22 of file PhpError.php.

Member Function Documentation

◆ __invoke()

Slim\Handlers\PhpError::__invoke ( ServerRequestInterface  $request,
ResponseInterface  $response,
\Throwable  $error 
)

Invoke error handler.

Parameters
ServerRequestInterface$requestThe most recent Request object
ResponseInterface$responseThe most recent Response object
\Throwable$errorThe caught Throwable object
Returns
ResponseInterface
Exceptions
UnexpectedValueException

Definition at line 34 of file PhpError.php.

References $contentType, $output, Slim\Handlers\AbstractHandler\determineContentType(), Slim\Handlers\PhpError\renderHtmlErrorMessage(), Slim\Handlers\PhpError\renderJsonErrorMessage(), Slim\Handlers\PhpError\renderXmlErrorMessage(), Psr\Http\Message\ResponseInterface\withStatus(), and Slim\Handlers\AbstractError\writeToErrorLog().

35  {
36  $contentType = $this->determineContentType($request);
37  switch ($contentType) {
38  case 'application/json':
40  break;
41 
42  case 'text/xml':
43  case 'application/xml':
45  break;
46 
47  case 'text/html':
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  }
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.
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
$error
Definition: Error.php:17
writeToErrorLog($throwable)
Write to the error log if displayErrorDetails is false.
renderHtmlErrorMessage(\Throwable $error)
Render HTML error page.
Definition: PhpError.php:72
renderJsonErrorMessage(\Throwable $error)
Render JSON error.
Definition: PhpError.php:144
if($path[strlen($path) - 1]==='/') if(is_dir($path)) if(!file_exists($path)) if(preg_match('#\.php$#D', $path)) $contentType
Definition: module.php:142
$response
+ Here is the call graph for this function:

◆ createCdataSection()

Slim\Handlers\PhpError::createCdataSection (   $content)
private

Returns a CDATA section with the given content.

Parameters
string$content
Returns
string

Definition at line 201 of file PhpError.php.

Referenced by Slim\Handlers\PhpError\renderXmlErrorMessage().

202  {
203  return sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $content));
204  }
+ Here is the caller graph for this function:

◆ renderHtmlError()

Slim\Handlers\PhpError::renderHtmlError ( \Throwable  $error)
protected

Render error as HTML.

Parameters
\Throwable$error
Returns
string

Definition at line 109 of file PhpError.php.

References $code, $file, $html, and $message.

Referenced by Slim\Handlers\PhpError\renderHtmlErrorMessage().

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  }
$code
Definition: example_050.php:99
$error
Definition: Error.php:17
catch(Exception $e) $message
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
$html
Definition: example_001.php:87
+ Here is the caller graph for this function:

◆ renderHtmlErrorMessage()

Slim\Handlers\PhpError::renderHtmlErrorMessage ( \Throwable  $error)
protected

Render HTML error page.

Parameters
\Throwable$error
Returns
string

Definition at line 72 of file PhpError.php.

References $html, $output, $title, and Slim\Handlers\PhpError\renderHtmlError().

Referenced by Slim\Handlers\PhpError\__invoke().

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  }
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
$error
Definition: Error.php:17
renderHtmlError(\Throwable $error)
Render error as HTML.
Definition: PhpError.php:109
$html
Definition: example_001.php:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ renderJsonErrorMessage()

Slim\Handlers\PhpError::renderJsonErrorMessage ( \Throwable  $error)
protected

Render JSON error.

Parameters
\Throwable$error
Returns
string

Definition at line 144 of file PhpError.php.

Referenced by Slim\Handlers\PhpError\__invoke().

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  }
$error
Definition: Error.php:17
+ Here is the caller graph for this function:

◆ renderXmlErrorMessage()

Slim\Handlers\PhpError::renderXmlErrorMessage ( \Throwable  $error)
protected

Render XML error.

Parameters
\Throwable$error
Returns
string

Definition at line 175 of file PhpError.php.

References $xml, and Slim\Handlers\PhpError\createCdataSection().

Referenced by Slim\Handlers\PhpError\__invoke().

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  }
$xml
Definition: metadata.php:240
$error
Definition: Error.php:17
createCdataSection($content)
Returns a CDATA section with the given content.
Definition: PhpError.php:201
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: