Sup guys. I’m using ajax to load in a bunch of data with php. Sometimes exceptions go uncaught. This generates some error messages which javascript cannot parse (because it’s expecting json). This crashes the page, that’s not cool. So I’m building a custom exception handler. Basically it should just json_encode the Exception object. But json only works on objects of type stdClass. So here’s the actual question:
How do you cast an exception object to stdClass, so that I can json_encode it? I’ve tried all the naive stuff I could think of, like
(stdClass) $ex and (Object) ((Array) $ex)
And yeah, I know that exceptions shouldn’t be uncaught to begin with, but it’s more like a development feature. It makes debugging less painful 😉
Thanks!
Using exceptions handler in this case is an horrible idea, especially when all you have to do is:
When error occurs you should setup response correctly. Set Response Code to 500 (Internal Server Error) so JavaScript could easly handle that.
Exceptionobject provides onlygetMessage()andgetCode()methods that could be usefull, so response body could be generated like so: