-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #456 from patchlevel/tranform-exception-in-array
transfrom projector throwable into an array
- Loading branch information
Showing
11 changed files
with
140 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Projection\Projection\Store; | ||
|
||
use Throwable; | ||
|
||
/** @psalm-type Context = array{message: string, code: int|string, file: string, line: int, trace: string} */ | ||
final class ErrorContext | ||
{ | ||
/** @return list<Context> */ | ||
public static function fromThrowable(Throwable $error): array | ||
{ | ||
$errors = []; | ||
|
||
do { | ||
$errors[] = self::transform($error); | ||
$error = $error->getPrevious(); | ||
} while ($error); | ||
|
||
return $errors; | ||
} | ||
|
||
/** @return Context */ | ||
private static function transform(Throwable $error): array | ||
{ | ||
return [ | ||
'message' => $error->getMessage(), | ||
'code' => $error->getCode(), | ||
'file' => $error->getFile(), | ||
'line' => $error->getLine(), | ||
'trace' => $error->getTraceAsString(), | ||
]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
tests/Unit/Projection/Projection/Store/ErrorContextTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Projection\Projection\Store; | ||
|
||
use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
/** @covers \Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext */ | ||
final class ErrorContextTest extends TestCase | ||
{ | ||
public function testWithoutPrevious(): void | ||
{ | ||
$error = new RuntimeException('foo'); | ||
|
||
$result = ErrorContext::fromThrowable($error); | ||
|
||
self::assertCount(1, $result); | ||
self::assertSame('foo', $result[0]['message']); | ||
self::assertSame(0, $result[0]['code']); | ||
self::assertSame(__FILE__, $result[0]['file']); | ||
} | ||
|
||
public function testWithPrevious(): void | ||
{ | ||
$error = new RuntimeException('foo', 0, new RuntimeException('bar')); | ||
|
||
$result = ErrorContext::fromThrowable($error); | ||
|
||
self::assertCount(2, $result); | ||
self::assertSame('foo', $result[0]['message']); | ||
self::assertSame(0, $result[0]['code']); | ||
self::assertSame(__FILE__, $result[0]['file']); | ||
|
||
self::assertSame('bar', $result[1]['message']); | ||
self::assertSame(0, $result[1]['code']); | ||
self::assertSame(__FILE__, $result[1]['file']); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
tests/Unit/Projection/Projection/Store/ErrorSerializerTest.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.