Hi,
I am looking for maybe a better way of doing this or opinions whether this is normal way of handling exception in a project. Imagine that I have a service class which has this method in it which updates an eloquent model. If it fails i have custom exception which has logging to custom channel, slack notifcation and other stuff in the report method. Here is the method of the service:
public function updateTransformatorData(EsoDataLoader $esoDataLoader, array $transformatorData): bool
{
try {
$result = $esoDataLoader->update([
'transformator_data' => json_encode($transformatorData)
]);
} catch (\Throwable $th) {
throw new EsoDataLoaderException(['esoDataLoader' => json_encode($esoDataLoader), 'transformatorData' => json_encode($transformatorData)], 'Problem with saving trasnformator data', EsoDataLoaderException::CODE_LOADER_TRANSFORMATOR_DATA_UPDATE_PROBLEM, $th);
}
return $result;
}
And now here is it's usage in other class somewhere in the project:
try {
eso()->service()->updateTransformatorData($this->esoDataLoader, $this->transformatorClassesToBeRunCollection->pluck('title')->toArray());
} catch(EsoDataLoaderException $ex) {
report($ex);
} catch (\Throwable $th) {
throw $th;
}
What I find strange is that anywhere in the project where some service throws my custom exception, I have to catch it and specifically run the report($ex) method in order to handle the logging and slack notification. I find that pretty redundant but I did not find any other way to do it.
I mean yes I can use the report helper in the service itself but that would mean that the application would continue and not stop on the exception which is not correct either.
Is there a better approach for this problem please?