Basic Controller with 3 methods. And each method has TryCatch block. Would it be possible not to repeat TryCatch block in each method but keep the functionality the same?
(for example, if FormController extends Controler than move TryCatch over there and now every methods that extends Controller is wrapped inside this specific TryCatch ... something like that)
class FormController
{
public function createForm()
{
try {
//code
} catch (Throwable $th) {
return response()->json(['status' => 'error', 'message' => 'There was an error.'], 500);
}
}
public function deleteForm()
{
try {
//code
} catch (Throwable $th) {
return response()->json(['status' => 'error', 'message' => 'There was an error.'], 500);
}
}
public function modifyForm()
{
try {
//code
} catch (Throwable $th) {
return response()->json(['status' => 'error', 'message' => 'There was an error.'], 500);
}
}
}
Becomes this:
class FormController
{
public function createForm()
{
// code
}
public function deleteForm()
{
// code
}
public function modifyForm()
{
// code
}
}
But if // code throws exception it still executes response()->json(['status' => 'error', 'message' => 'There was an error.'], 500)