hiwye's avatar
Level 4

Exception handler fired multiple times

Is it possible to prevent the exception handling function from running more than once, even if there are multiple exceptions?

I am currently catching API errors, but if there are multiple exceptions thrown I get a messed up JSON response:

{"error":{"message":"Internal Server Error","status_code":500,"details":[]}}{"error":{"message":"Internal Server Error","status_code":500,"details":[]}}

As you can see, the result are two concatenated JSON strings, which (together) aren't valid.

Any help would be greatly appreciated!

Cheers, Seb

0 likes
7 replies
Braunson's avatar

@hiwye Can you post your code that's being run? You have exceptions that are returning a response, you need to return one response and exit there.

ejdelmonico's avatar

It almost looks like the framework is reporting the error along with your error code. In other words, maybe the api render is the son inside of the parent render. Just a guess.

ejdelmonico's avatar

Try commenting

if(strpos($request->url(), 'api') !== false){
            return $this->renderAPI($exception);
        }

and see if you get one error or none.

hiwye's avatar
Level 4

Now I get the normal HTML (not JSON) version of the exception.

The handler method is getting called twice, per call one JSON response gets generated and then somehow concatenated.

Please also note that in the HTML version there are two exceptions being displayed. Why is that? Should only the first one be handled and then die.

ejdelmonico's avatar

I am really not sure. I have never seen a duplicate error. Maybe try to produce two distinct errors...another with the 500 and see what is returned. Maybe that experiment will give some more clues as to what is happening.

Please or to participate in this conversation.