ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Slim\Handlers\Error Class Reference

Default Slim application error handler. More...

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

Public Member Functions

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

Protected Member Functions

 renderHtmlErrorMessage (\Exception $exception)
 Render HTML error page. More...
 
 renderHtmlException (\Exception $exception)
 Render exception as HTML. More...
 
 renderHtmlExceptionOrError ($exception)
 Render exception or error as HTML. More...
 
 renderJsonErrorMessage (\Exception $exception)
 Render JSON error. More...
 
 renderXmlErrorMessage (\Exception $exception)
 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.

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 Error.php.

Member Function Documentation

◆ __invoke()

Slim\Handlers\Error::__invoke ( ServerRequestInterface  $request,
ResponseInterface  $response,
\Exception  $exception 
)

Invoke error handler.

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

Definition at line 34 of file Error.php.

35 {
36 $contentType = $this->determineContentType($request);
37 switch ($contentType) {
38 case 'application/json':
39 $output = $this->renderJsonErrorMessage($exception);
40 break;
41
42 case 'text/xml':
43 case 'application/xml':
44 $output = $this->renderXmlErrorMessage($exception);
45 break;
46
47 case 'text/html':
48 $output = $this->renderHtmlErrorMessage($exception);
49 break;
50
51 default:
52 throw new UnexpectedValueException('Cannot render unknown content type ' . $contentType);
53 }
54
55 $this->writeToErrorLog($exception);
56
57 $body = new Body(fopen('php://temp', 'r+'));
58 $body->write($output);
59
60 return $response
61 ->withStatus(500)
62 ->withHeader('Content-type', $contentType)
63 ->withBody($body);
64 }
writeToErrorLog($throwable)
Write to the error log if displayErrorDetails is false.
determineContentType(ServerRequestInterface $request)
Determine which content type we know about is wanted using Accept header.
renderJsonErrorMessage(\Exception $exception)
Render JSON error.
Definition: Error.php:163
renderHtmlErrorMessage(\Exception $exception)
Render HTML error page.
Definition: Error.php:73
renderXmlErrorMessage(\Exception $exception)
Render XML error.
Definition: Error.php:194
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
$response

References $contentType, Sabre\VObject\$output, $response, Slim\Handlers\AbstractHandler\determineContentType(), Slim\Handlers\Error\renderHtmlErrorMessage(), Slim\Handlers\Error\renderJsonErrorMessage(), Slim\Handlers\Error\renderXmlErrorMessage(), and Slim\Handlers\AbstractError\writeToErrorLog().

+ Here is the call graph for this function:

◆ createCdataSection()

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

Returns a CDATA section with the given content.

Parameters
string$content
Returns
string

Definition at line 220 of file Error.php.

221 {
222 return sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $content));
223 }

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

+ Here is the caller graph for this function:

◆ renderHtmlErrorMessage()

Slim\Handlers\Error::renderHtmlErrorMessage ( \Exception  $exception)
protected

Render HTML error page.

Parameters
\Exception$exception
Returns
string

Definition at line 73 of file Error.php.

74 {
75 $title = 'Slim Application Error';
76
77 if ($this->displayErrorDetails) {
78 $html = '<p>The application could not run because of the following error:</p>';
79 $html .= '<h2>Details</h2>';
80 $html .= $this->renderHtmlException($exception);
81
82 while ($exception = $exception->getPrevious()) {
83 $html .= '<h2>Previous exception</h2>';
84 $html .= $this->renderHtmlExceptionOrError($exception);
85 }
86 } else {
87 $html = '<p>A website error has occurred. Sorry for the temporary inconvenience.</p>';
88 }
89
90 $output = sprintf(
91 "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
92 "<title>%s</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana," .
93 "sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{" .
94 "display:inline-block;width:65px;}</style></head><body><h1>%s</h1>%s</body></html>",
95 $title,
96 $title,
97 $html
98 );
99
100 return $output;
101 }
renderHtmlException(\Exception $exception)
Render exception as HTML.
Definition: Error.php:112
renderHtmlExceptionOrError($exception)
Render exception or error as HTML.
Definition: Error.php:124
$html
Definition: example_001.php:87

References $html, Sabre\VObject\$output, $title, Slim\Handlers\Error\renderHtmlException(), and Slim\Handlers\Error\renderHtmlExceptionOrError().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ renderHtmlException()

Slim\Handlers\Error::renderHtmlException ( \Exception  $exception)
protected

Render exception as HTML.

Provided for backwards compatibility; use renderHtmlExceptionOrError().

Parameters
\Exception$exception
Returns
string

Definition at line 112 of file Error.php.

113 {
114 return $this->renderHtmlExceptionOrError($exception);
115 }

References Slim\Handlers\Error\renderHtmlExceptionOrError().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ renderHtmlExceptionOrError()

Slim\Handlers\Error::renderHtmlExceptionOrError (   $exception)
protected

Render exception or error as HTML.

Parameters
\Exception | \Error$exception
Returns
string

Definition at line 124 of file Error.php.

125 {
126 if (!$exception instanceof \Exception && !$exception instanceof \Error) {
127 throw new \RuntimeException("Unexpected type. Expected Exception or Error.");
128 }
129
130 $html = sprintf('<div><strong>Type:</strong> %s</div>', get_class($exception));
131
132 if (($code = $exception->getCode())) {
133 $html .= sprintf('<div><strong>Code:</strong> %s</div>', $code);
134 }
135
136 if (($message = $exception->getMessage())) {
137 $html .= sprintf('<div><strong>Message:</strong> %s</div>', htmlentities($message));
138 }
139
140 if (($file = $exception->getFile())) {
141 $html .= sprintf('<div><strong>File:</strong> %s</div>', $file);
142 }
143
144 if (($line = $exception->getLine())) {
145 $html .= sprintf('<div><strong>Line:</strong> %s</div>', $line);
146 }
147
148 if (($trace = $exception->getTraceAsString())) {
149 $html .= '<h2>Trace</h2>';
150 $html .= sprintf('<pre>%s</pre>', htmlentities($trace));
151 }
152
153 return $html;
154 }
$code
Definition: example_050.php:99
catch(Exception $e) $message

References $code, $html, and $message.

Referenced by Slim\Handlers\Error\renderHtmlErrorMessage(), and Slim\Handlers\Error\renderHtmlException().

+ Here is the caller graph for this function:

◆ renderJsonErrorMessage()

Slim\Handlers\Error::renderJsonErrorMessage ( \Exception  $exception)
protected

Render JSON error.

Parameters
\Exception$exception
Returns
string

Definition at line 163 of file Error.php.

164 {
165 $error = [
166 'message' => 'Slim Application Error',
167 ];
168
169 if ($this->displayErrorDetails) {
170 $error['exception'] = [];
171
172 do {
173 $error['exception'][] = [
174 'type' => get_class($exception),
175 'code' => $exception->getCode(),
176 'message' => $exception->getMessage(),
177 'file' => $exception->getFile(),
178 'line' => $exception->getLine(),
179 'trace' => explode("\n", $exception->getTraceAsString()),
180 ];
181 } while ($exception = $exception->getPrevious());
182 }
183
184 return json_encode($error, JSON_PRETTY_PRINT);
185 }

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

+ Here is the caller graph for this function:

◆ renderXmlErrorMessage()

Slim\Handlers\Error::renderXmlErrorMessage ( \Exception  $exception)
protected

Render XML error.

Parameters
\Exception$exception
Returns
string

Definition at line 194 of file Error.php.

195 {
196 $xml = "<error>\n <message>Slim Application Error</message>\n";
197 if ($this->displayErrorDetails) {
198 do {
199 $xml .= " <exception>\n";
200 $xml .= " <type>" . get_class($exception) . "</type>\n";
201 $xml .= " <code>" . $exception->getCode() . "</code>\n";
202 $xml .= " <message>" . $this->createCdataSection($exception->getMessage()) . "</message>\n";
203 $xml .= " <file>" . $exception->getFile() . "</file>\n";
204 $xml .= " <line>" . $exception->getLine() . "</line>\n";
205 $xml .= " <trace>" . $this->createCdataSection($exception->getTraceAsString()) . "</trace>\n";
206 $xml .= " </exception>\n";
207 } while ($exception = $exception->getPrevious());
208 }
209 $xml .= "</error>";
210
211 return $xml;
212 }
createCdataSection($content)
Returns a CDATA section with the given content.
Definition: Error.php:220

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

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

+ 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: