ExceptionReporter#
Meritum\StructuredLogging\ExceptionReporter
Translates a Throwable into a DomainException
and logs it using the structured data. Registered by StructuredLoggingModule as ExceptionReporter::class.
Methods#
public function report(Throwable $exception): DomainException;- Passes
$exceptionthroughExceptionTranslator::translate(). - Calls
$logger->log($severity, $message, $structuredData)on the translated exception. - Returns the
DomainExceptionso the caller can use it to construct a response.
Example#
use Meritum\StructuredLogging\Severity;
use Meritum\StructuredLogging\ExceptionReporter;
final class ExceptionHandler implements ExceptionHandlerInterface
{
public function __construct(private readonly ExceptionReporter $reporter) {}
public function handle(Throwable $e, ServerRequestInterface $request): ResponseInterface
{
$domain = $this->reporter->report($e);
$status = match ($domain->severity) {
Severity::Error, Severity::Critical => 500,
default => 400,
};
return new JsonResponse(['error' => $domain->getErrorCode()], $status);
}
}