Skip to content

Commit

Permalink
Merge pull request paysera#11 from Laurgrin/error-handling
Browse files Browse the repository at this point in the history
Update exception handling
  • Loading branch information
mSprunskas authored Apr 14, 2023
2 parents ca8ac20 + 6c95796 commit 10ed478
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.5.1]
### Changed
- Changed error handling signatures to work with new versions.

## [1.5.0]
### Changed
- `psr/log` bumped to `^2.0`
Expand Down
9 changes: 7 additions & 2 deletions src/Listener/RestExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Log\LoggerInterface;
use Exception;
use Paysera\Bundle\ApiBundle\Service\ErrorBuilderInterface;
use Throwable;

/**
* @internal
Expand Down Expand Up @@ -55,7 +56,11 @@ public function onKernelException($event)
return;
}

$exception = $event->getException();
if (method_exists($event, 'getException')) {
$exception = $event->getException();
} else {
$exception = $event->getThrowable();
}
$error = $this->errorBuilder->createErrorFromException($exception);
$normalizedError = $this->coreNormalizer->normalize($error);

Expand All @@ -69,7 +74,7 @@ public function onKernelException($event)
$event->setResponse($response);
}

private function logException(Response $response, Exception $exception)
private function logException(Response $response, Throwable $exception)
{
if ($response->getStatusCode() >= 500) {
$level = LogLevel::ERROR;
Expand Down
6 changes: 3 additions & 3 deletions src/Service/ErrorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Paysera\Bundle\ApiBundle\Exception\ApiException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Exception;
use Throwable;

class ErrorBuilder implements ErrorBuilderInterface
{
Expand All @@ -37,12 +37,12 @@ public function configureError(string $errorCode, int $statusCode, string $messa
];
}

public function createErrorFromException(Exception $exception): Error
public function createErrorFromException(Throwable $exception): Error
{
return $this->fillErrorFields($this->buildErrorFromException($exception));
}

private function buildErrorFromException(Exception $exception): Error
private function buildErrorFromException(Throwable $exception): Error
{
if ($exception instanceof ApiException) {
return (new Error())
Expand Down
4 changes: 2 additions & 2 deletions src/Service/ErrorBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace Paysera\Bundle\ApiBundle\Service;

use Exception;
use Paysera\Bundle\ApiBundle\Entity\Error;
use Throwable;

interface ErrorBuilderInterface
{
public function createErrorFromException(Exception $exception): Error;
public function createErrorFromException(Throwable $exception): Error;
}

0 comments on commit 10ed478

Please sign in to comment.