Skip to content

Commit

Permalink
Merge pull request #472 from patchlevel/use-array-as-trace-in-project…
Browse files Browse the repository at this point in the history
…ion-error

use array as trace in projection error
  • Loading branch information
DavidBadura authored Feb 1, 2024
2 parents 70e2054 + 7b3f6fc commit b44bcd6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
22 changes: 21 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
parameters:
ignoreErrors:
-
message: "#^Method Patchlevel\\\\EventSourcing\\\\Console\\\\Command\\\\ProjectionStatusCommand\\:\\:displayError\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#"
count: 1
path: src/Console/Command/ProjectionStatusCommand.php

-
message: "#^Method Patchlevel\\\\EventSourcing\\\\EventBus\\\\Message\\:\\:headers\\(\\) should return array\\{aggregateClass\\?\\: class\\-string\\<Patchlevel\\\\EventSourcing\\\\Aggregate\\\\AggregateRoot\\>, aggregateId\\?\\: string, playhead\\?\\: int\\<1, max\\>, recordedOn\\?\\: DateTimeImmutable, newStreamStart\\?\\: bool, archived\\?\\: bool\\} but returns non\\-empty\\-array\\<string, mixed\\>\\.$#"
count: 1
path: src/EventBus/Message.php

-
message: "#^Parameter \\#2 \\$errorContext of class Patchlevel\\\\EventSourcing\\\\Projection\\\\Projection\\\\ProjectionError constructor expects array\\<int, array\\{message\\: string, code\\: int\\|string, file\\: string, line\\: int, trace\\: string\\}\\>\\|null, mixed given\\.$#"
message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projection\\\\ProjectionError\\:\\:__construct\\(\\) has parameter \\$errorContext with no value type specified in iterable type array\\.$#"
count: 1
path: src/Projection/Projection/ProjectionError.php

-
message: "#^Parameter \\#2 \\$errorContext of class Patchlevel\\\\EventSourcing\\\\Projection\\\\Projection\\\\ProjectionError constructor expects array\\<int, array\\{message\\: string, code\\: int\\|string, file\\: string, line\\: int, trace\\: array\\<int, array\\{file\\?\\: string, line\\?\\: int, function\\?\\: string, class\\?\\: string, type\\?\\: string, args\\?\\: array\\}\\>\\}\\>\\|null, mixed given\\.$#"
count: 1
path: src/Projection/Projection/Store/DoctrineStore.php

-
message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projection\\\\Store\\\\ErrorContext\\:\\:fromThrowable\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Projection/Projection/Store/ErrorContext.php

-
message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projection\\\\Store\\\\ErrorContext\\:\\:transformThrowable\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Projection/Projection/Store/ErrorContext.php

-
message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projector\\\\InMemoryProjectorRepository\\:\\:projectors\\(\\) should return array\\<int, object\\> but returns array\\<int\\|string, object\\>\\.$#"
count: 1
Expand Down
6 changes: 5 additions & 1 deletion src/Console/Command/ProjectionStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use function array_map;
use function is_array;
use function sprintf;

/** @psalm-import-type Context from ErrorContext */
#[AsCommand(
Expand Down Expand Up @@ -106,6 +107,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function displayError(OutputStyle $io, array $context): void
{
$io->error($context['message']);
$io->block($context['trace']);

foreach ($context['trace'] as $trace) {
$io->writeln(sprintf('%s: %s', $trace['file'] ?? '#unknown', $trace['line'] ?? '#unknown'));
}
}
}
11 changes: 7 additions & 4 deletions src/Projection/Projection/Store/ErrorContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Throwable;

/** @psalm-type Context = array{message: string, code: int|string, file: string, line: int, trace: string} */
/**
* @psalm-type Trace = array{file?: string, line?: int, function?: string, class?: string, type?: string, args?: array}
* @psalm-type Context = array{message: string, code: int|string, file: string, line: int, trace: list<Trace>}
*/
final class ErrorContext
{
/** @return list<Context> */
Expand All @@ -15,22 +18,22 @@ public static function fromThrowable(Throwable $error): array
$errors = [];

do {
$errors[] = self::transform($error);
$errors[] = self::transformThrowable($error);
$error = $error->getPrevious();
} while ($error);

return $errors;
}

/** @return Context */
private static function transform(Throwable $error): array
private static function transformThrowable(Throwable $error): array
{
return [
'message' => $error->getMessage(),
'code' => $error->getCode(),
'file' => $error->getFile(),
'line' => $error->getLine(),
'trace' => $error->getTraceAsString(),
'trace' => $error->getTrace(),
];
}
}

0 comments on commit b44bcd6

Please sign in to comment.