I have a callback that is running for a specific post request. I am returning a certain JSON response at the end of it.
What if I want to catch errors inside one of the functions that are ran in that callback ? How do I get the Response object, or is there another way of sending a response immediately in case errors are catched ?
private function someCalculation()
{
try {
} catch ($e) {
//how to send response in case of error inside this function
return new JsonResponse([
'data' => null,
'error' => $error
]);
}
}
public function getTemplate(Request $request)
{
$this->someCalculation();
return new JsonResponse([
'data' => $result,
]);
}
How do I send a JSON from inside the someCalculation function in case some errors are found ?