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

Commitff2f7d2

Browse files
committed
[Debug] Mimic __toString php behavior in FlattenException
1 parent5a3e894 commitff2f7d2

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

‎src/Symfony/Component/Debug/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CHANGELOG
55
-----
66

77
* made the`ErrorHandler` and`ExceptionHandler` classes final
8+
* added`Exception\FlattenException::getAsString` and
9+
`Exception\FlattenException::getTraceAsString` to increase compatibility to php
10+
exception objects
811

912
4.0.0
1013
-----

‎src/Symfony/Component/Debug/Exception/FlattenException.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class FlattenException
2727
private$code;
2828
private$previous;
2929
private$trace;
30+
private$traceAsString;
3031
private$class;
3132
private$statusCode;
3233
private$headers;
@@ -239,6 +240,8 @@ public function setTraceFromException(\Exception $exception)
239240

240241
publicfunctionsetTraceFromThrowable(\Throwable$throwable)
241242
{
243+
$this->traceAsString =$throwable->getTraceAsString();
244+
242245
return$this->setTrace($throwable->getTrace(),$throwable->getFile(),$throwable->getLine());
243246
}
244247

@@ -324,4 +327,33 @@ private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
324327

325328
return$array['__PHP_Incomplete_Class_Name'];
326329
}
330+
331+
publicfunctiongetTraceAsString()
332+
{
333+
return$this->traceAsString;
334+
}
335+
336+
publicfunctiongetAsString()
337+
{
338+
$message ='';
339+
$next =false;
340+
341+
foreach (array_reverse(array_merge(array($this),$this->getAllPrevious()))as$exception) {
342+
if ($next) {
343+
$message .='Next';
344+
}else {
345+
$next =true;
346+
}
347+
$message .=$exception->getClass();
348+
349+
if ('' !=$exception->getMessage()) {
350+
$message .=':'.$exception->getMessage();
351+
}
352+
353+
$message .=' in'.$exception->getFile().':'.$exception->getLine().
354+
"\nStack trace:\n".$exception->getTraceAsString()."\n\n";
355+
}
356+
357+
returnrtrim($message);
358+
}
327359
}

‎src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,41 @@ public function testAnonymousClass()
346346
$this->assertSame('Class "RuntimeException@anonymous" blah.',$flattened->getMessage());
347347
}
348348

349+
publicfunctiontestToStringEmptyMessage()
350+
{
351+
$exception =new \RuntimeException();
352+
353+
$flattened = FlattenException::create($exception);
354+
355+
$this->assertSame($exception->getTraceAsString(),$flattened->getTraceAsString());
356+
$this->assertSame($exception->__toString(),$flattened->getAsString());
357+
}
358+
359+
publicfunctiontestToString()
360+
{
361+
$test =function ($a,$b,$c,$d) {
362+
returnnew \RuntimeException('This is a test message');
363+
};
364+
365+
$exception =$test('foo123',1,null,1.5);
366+
367+
$flattened = FlattenException::create($exception);
368+
369+
$this->assertSame($exception->getTraceAsString(),$flattened->getTraceAsString());
370+
$this->assertSame($exception->__toString(),$flattened->getAsString());
371+
}
372+
373+
publicfunctiontestToStringParent()
374+
{
375+
$exception =new \LogicException('This is message 1');
376+
$exception =new \RuntimeException('This is messsage 2',500,$exception);
377+
378+
$flattened = FlattenException::create($exception);
379+
380+
$this->assertSame($exception->getTraceAsString(),$flattened->getTraceAsString());
381+
$this->assertSame($exception->__toString(),$flattened->getAsString());
382+
}
383+
349384
privatefunctioncreateException($foo)
350385
{
351386
returnnew \Exception();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp