I'm learning Symfony5 and I was wondering whether it is possible to catch exceptions after route's execution.
The main idea is to get rid of some recurrent try-catch blocks of code in my routes:
public function getStudentMean(int $studentId): Response
{
try {
$mean = $this->gradedStudentService->getMean($studentId);
return new Response($mean, Response::HTTP_OK);
} catch (AbstractApiException $e) {
return $this->returnBadRequestResponse($e->getMessage());
}
}
public function deleteStudent(int $id): Response
{
try {
$this->studentService->deleteStudent($id);
return new Response('', Response::HTTP_NO_CONTENT);
} catch (AbstractApiException $e) {
return $this->returnBadRequestResponse($e->getMessage());
}
}
Do I have to edit my public\index.php file to catch the exception here? Is there another cleaner method to do so?
Thanks!