Skip to content

Commit

Permalink
Adding OptimisticLockException to KernelExceptionListener
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsorst committed Mar 27, 2017
1 parent 80cb77b commit df0c5bd
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions Listener/KernelExceptionListener.php
Expand Up @@ -2,6 +2,7 @@

namespace Dontdrinkandroot\RestBundle\Listener;

use Doctrine\ORM\OptimisticLockException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -47,15 +48,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
];
}

$statusCode = Response::HTTP_INTERNAL_SERVER_ERROR;

if ($exception instanceof InsufficientAuthenticationException) {
$statusCode = Response::HTTP_UNAUTHORIZED;
}

if ($exception instanceof BadCredentialsException) {
$statusCode = Response::HTTP_UNAUTHORIZED;
}
$statusCode = $this->resolveStatusCode($exception);

$response = new JsonResponse($data);

Expand Down Expand Up @@ -100,4 +93,26 @@ public function setDebug(bool $debug)
{
$this->debug = $debug;
}

/**
* @param $exception
*
* @return int
*/
protected function resolveStatusCode($exception): int
{
if ($exception instanceof InsufficientAuthenticationException) {
return Response::HTTP_UNAUTHORIZED;
}

if ($exception instanceof BadCredentialsException) {
return Response::HTTP_UNAUTHORIZED;
}

if ($exception instanceof OptimisticLockException) {
return Response::HTTP_PRECONDITION_FAILED;
}

return Response::HTTP_INTERNAL_SERVER_ERROR;
}
}

0 comments on commit df0c5bd

Please sign in to comment.