Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa6bc3d

Browse files
committed
Get exception content according to request format
1 parent4ad54da commitaa6bc3d

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-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+
publicfunctiongetFormattedContent($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+
publicfunctiongetJson($exception):string
222+
{
223+
if (!$exceptioninstanceof 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+
publicfunctiongetXml($exception):string
243+
{
244+
if (!$exceptioninstanceof 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+
publicfunctiongetTxt($exception):string
280+
{
281+
if (!$exceptioninstanceof 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+
publicfunctiontestJsonExceptionContent()
144+
{
145+
$handler =newExceptionHandler(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+
publicfunctiontestXmlExceptionContent()
152+
{
153+
$handler =newExceptionHandler(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+
publicfunctiontestTxtExceptionContent()
160+
{
161+
$handler =newExceptionHandler(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 =newExceptionHandler($this->debug,$this->charset,$this->fileLinkFormat);
156156

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

‎src/Symfony/Component/HttpKernel/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"conflict": {
4949
"symfony/browser-kit":"<4.3",
5050
"symfony/config":"<3.4",
51+
"symfony/debug":"<4.3",
5152
"symfony/dependency-injection":"<4.2",
5253
"symfony/translation":"<4.2",
5354
"symfony/var-dumper":"<4.1.1",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp