Giter VIP home page Giter VIP logo

Comments (3)

sobhanatar avatar sobhanatar commented on June 13, 2024

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?

from sneaker.

akaamitgupta avatar akaamitgupta commented on June 13, 2024

@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.

from sneaker.

sobhanatar avatar sobhanatar commented on June 13, 2024

@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 :-)

from sneaker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.