Closed sobhanatar closed 6 years ago
Hi
I dig dipper and I understood that by returning json response
no exception throws like the function abort()
do. So I changed my code to something like this and add finally to email the exception.
public function index(): JsonResponse
{
try {
//make some error
return response()->json(['data' => $data]);
} catch (Exception $e) {
app('sneaker')->captureException($e);
return response()->json(['message' => trans('code_execution_fail', ['e' => $e->getCode()])], 422);
}
}
But this way I have to add app('sneaker')->captureException($e);
to all my catch blocks. Is there any better way for that?
@sobhanattar Ideally your App/Exceptions/Handler@report
should be called for each exception but here you are catching each exception by try {} catch {}
so this is the only solution I can see.
@akaamitgupta I found a new solution for this problem, although the change in catch is still a necessity but I'm using a helper method report($e)
which is short and this way If I want to do some change in the behavior of types of emails and such a things I shouldn't go back and put all that logic in every catch
blocks which is a + for me.
Now my code is like this:
public function index(): JsonResponse
{
try {
//make some error
return response()->json(['data' => $data]);
} catch (Exception $e) {
report($e);
return response()->json(['message' => trans('code_execution_fail', ['e' => $e->getCode()])], 422);
}
}
I think it's not bad to change the Doc to mention this :-)
Hi I have this code:
I set
capture
to'*'
so it get all exceptions. But it doesn't callreport
function in this context although when I use a function likeabort(404, 'message')
it executessneaker
inreport
function and send email to my inbox.What I'm missing?