Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d66ef3

Browse files
committed
Get exception content according to request format
1 parent 4ad54da commit 3d66ef3

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

src/Symfony/Component/Debug/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ CHANGELOG
88
* added `Exception\FlattenException::getAsString` and
99
`Exception\FlattenException::getTraceAsString` to increase compatibility to php
1010
exception objects
11+
* added `ExceptionHandler::getFormattedContent()` to get the exception content
12+
according to given format (html, json, xml, txt)
1113

1214
4.0.0
1315
-----

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,117 @@ public function sendPhpResponse($exception)
189189
echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
190190
}
191191

192+
/**
193+
* Gets the content associated with the given exception.
194+
*
195+
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
196+
* @param string $format The request format (html, json, xml, txt)
197+
*
198+
* @return string The formatted content as a string
199+
*/
200+
public function getFormattedContent($exception, string $format): string
201+
{
202+
switch ($format) {
203+
case 'json':
204+
return $this->getJson($exception);
205+
case 'xml':
206+
return $this->getXml($exception);
207+
case 'txt':
208+
return $this->getTxt($exception);
209+
default:
210+
return $this->getHtml($exception);
211+
}
212+
}
213+
214+
/**
215+
* Gets the JSON content associated with the given exception.
216+
*
217+
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
218+
*
219+
* @return string The JSON content as a string
220+
*/
221+
public function getJson($exception): string
222+
{
223+
if (!$exception instanceof FlattenException) {
224+
$exception = FlattenException::create($exception);
225+
}
226+
227+
return (string) json_encode([
228+
'error' => [
229+
'code' => $exception->getStatusCode(),
230+
'exception' => $exception->toArray(),
231+
],
232+
]);
233+
}
234+
235+
/**
236+
* Gets the XML content associated with the given exception.
237+
*
238+
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
239+
*
240+
* @return string The XML content as a string
241+
*/
242+
public function getXml($exception): string
243+
{
244+
if (!$exception instanceof FlattenException) {
245+
$exception = FlattenException::create($exception);
246+
}
247+
248+
$content = '';
249+
foreach ($exception->toArray() as $e) {
250+
$content .= sprintf('<exception class="%s" message="%s"><traces>', $e['class'], $e['message']);
251+
foreach ($e['trace'] as $trace) {
252+
$content .= '<trace>';
253+
if ($trace['function']) {
254+
$content .= sprintf('at %s%s%s(%s) ', $trace['class'], $trace['type'], $trace['function'], strip_tags($this->formatArgs($trace['args'])));
255+
}
256+
if (isset($trace['file'], $trace['line'])) {
257+
$content .= strip_tags($this->formatPath($trace['file'], $trace['line']));
258+
}
259+
$content .= '</trace>';
260+
}
261+
$content .= '</traces></exception>';
262+
}
263+
264+
return <<<EOF
265+
<?xml version="1.0" encoding="{$this->charset}" ?>
266+
<error code="{$exception->getStatusCode()}">
267+
{$content}
268+
</error>
269+
EOF;
270+
}
271+
272+
/**
273+
* Gets the TXT content associated with the given exception.
274+
*
275+
* @param \Exception|FlattenException $exception An \Exception or FlattenException instance
276+
*
277+
* @return string The TXT content as a string
278+
*/
279+
public function getTxt($exception): string
280+
{
281+
if (!$exception instanceof FlattenException) {
282+
$exception = FlattenException::create($exception);
283+
}
284+
285+
$content = sprintf("[exception] %s | %s\n", $exception->getStatusCode(), $exception->getClass());
286+
foreach ($exception->toArray() as $i => $e) {
287+
$content .= sprintf("[%d] %s: %s\n", $i + 1, $e['class'], $e['message']);
288+
289+
foreach ($e['trace'] as $trace) {
290+
if ($trace['function']) {
291+
$content .= sprintf('at %s%s%s(%s) ', $trace['class'], $trace['type'], $trace['function'], strip_tags($this->formatArgs($trace['args'])));
292+
}
293+
if (isset($trace['file'], $trace['line'])) {
294+
$content .= strip_tags($this->formatPath($trace['file'], $trace['line']));
295+
}
296+
$content .= "\n";
297+
}
298+
}
299+
300+
return $content;
301+
}
302+
192303
/**
193304
* Gets the full HTML content associated with the given exception.
194305
*

src/Symfony/Component/Debug/Tests/ExceptionHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,28 @@ public function testHandleOutOfMemoryException()
139139

140140
$handler->handle($exception);
141141
}
142+
143+
public function testJsonExceptionContent()
144+
{
145+
$handler = new ExceptionHandler(true);
146+
$content = $handler->getJson(new \RuntimeException('Foo'));
147+
148+
$this->assertStringMatchesFormat('{"error":{"code":500,"exception":[{"message":"Foo","class":"RuntimeException"%S}]}}', $content);
149+
}
150+
151+
public function testXmlExceptionContent()
152+
{
153+
$handler = new ExceptionHandler(true);
154+
$content = $handler->getXml(new \RuntimeException('Foo'));
155+
156+
$this->assertStringMatchesFormat('<?xml version="1.0" encoding="UTF-8" ?>%A<error code="500">%A<exception class="RuntimeException" message="Foo">%A', $content);
157+
}
158+
159+
public function testTxtExceptionContent()
160+
{
161+
$handler = new ExceptionHandler(true);
162+
$content = $handler->getTxt(new \RuntimeException('Foo'));
163+
164+
$this->assertStringMatchesFormat("[exception] 500 | RuntimeException\n[1] RuntimeException: Foo\nin ExceptionHandlerTest.php line %A", $content);
165+
}
142166
}

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ protected function duplicateRequest(\Exception $exception, Request $request)
151151
{
152152
$attributes = [
153153
'exception' => $exception = FlattenException::create($exception),
154-
'_controller' => $this->controller ?: function () use ($exception) {
154+
'_controller' => $this->controller ?: function () use ($exception, $request) {
155155
$handler = new ExceptionHandler($this->debug, $this->charset, $this->fileLinkFormat);
156156

157-
return new Response($handler->getHtml($exception), $exception->getStatusCode(), $exception->getHeaders());
157+
return new Response($handler->getFormattedContent($exception, $request->getRequestFormat()), $exception->getStatusCode(), $exception->getHeaders());
158158
},
159159
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
160160
];

0 commit comments

Comments
 (0)