Commit 4855eaae authored by damjan's avatar damjan
Browse files

[BUGFIX] Right way to pass exception message by FOSRest bundle

parent c28489db
......@@ -95,8 +95,10 @@ fos_rest:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
'SGalinski\TypoScriptBackendBundle\Exception\BadRequestDataException': HTTP_BAD_REQUEST
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
'SGalinski\TypoScriptBackendBundle\Exception\BadRequestDataException': true
allowed_methods_listener: true
access_denied_listener:
json: true
......
......@@ -49,7 +49,7 @@ abstract class AbstractPropertyController extends ExceptionFOSRestController {
return $properties;
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -72,7 +72,7 @@ abstract class AbstractPropertyController extends ExceptionFOSRestController {
return $properties;
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -106,7 +106,7 @@ abstract class AbstractPropertyController extends ExceptionFOSRestController {
return $invalidFormException->getForm();
}
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -153,7 +153,7 @@ abstract class AbstractPropertyController extends ExceptionFOSRestController {
return $exception->getForm();
}
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -181,7 +181,7 @@ abstract class AbstractPropertyController extends ExceptionFOSRestController {
try {
$propertyRepository->deleteProperty($property);
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
} else {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
......@@ -222,7 +222,7 @@ abstract class AbstractPropertyController extends ExceptionFOSRestController {
return $exception->getForm();
}
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......
......@@ -43,7 +43,7 @@ abstract class AbstractTsrefController extends ExceptionFOSRestController {
$response->setContent($content);
return $response;
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
}
......@@ -66,7 +66,7 @@ abstract class AbstractTypeController extends ExceptionFOSRestController {
return $types;
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -101,7 +101,7 @@ abstract class AbstractTypeController extends ExceptionFOSRestController {
return $exception->getForm();
}
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -149,7 +149,7 @@ abstract class AbstractTypeController extends ExceptionFOSRestController {
return $exception->getForm();
}
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......@@ -179,7 +179,7 @@ abstract class AbstractTypeController extends ExceptionFOSRestController {
try {
$typeRepository->deleteType($type);
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
} else {
throw new NotFoundHttpException(sprintf('The resource \'%s\' was not found.', $id));
......@@ -221,7 +221,7 @@ abstract class AbstractTypeController extends ExceptionFOSRestController {
return $exception->getForm();
}
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
......
......@@ -33,7 +33,7 @@ class CategoryController extends ExceptionFOSRestController {
$categories = $categoryRepository->findAll();
return $categories;
} catch (\Exception $exception) {
return $this->makeExceptionResponse($exception);
$this->throwFosrestSupportedException($exception);
}
}
}
\ No newline at end of file
......@@ -9,22 +9,21 @@
namespace SGalinski\TypoScriptBackendBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Response;
use SGalinski\TypoScriptBackendBundle\Exception\BadRequestDataException;
/**
* Class ExceptionFOSRestController passes the error messages to FOSRest-registered exception.
*
* @package SGalinski\TypoScriptBackendBundle\Controller
*/
abstract class ExceptionFOSRestController extends FOSRestController {
/**
* Makes response from given exception.
*
* @param \Exception $exception
* @return Response
* @throws BadRequestDataException
*/
protected function makeExceptionResponse(\Exception $exception) {
return new Response(
json_encode(
['code' => Response::HTTP_BAD_REQUEST, 'message' => $exception->getMessage()]
),
Response::HTTP_BAD_REQUEST,
['content-type' => 'application/json']
);
protected function throwFosrestSupportedException(\Exception $exception) {
throw new BadRequestDataException($exception->getMessage());
}
}
\ No newline at end of file
<?php
namespace SGalinski\TypoScriptBackendBundle\Exception;
/**
* Class BadRequestDataException has purpose to pass the error message through FOSRest bundle to the client.
* It should be used for error caused by client's bad input.
*
* @package SGalinski\TypoScriptBackendBundle\Exception
*/
class BadRequestDataException extends \Exception {
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment