2

I have need to store a PHP Exception object in a mysql column. It's for an offline error logging system. Usually I would just serialize() the Exception object and be done with it, but half the time, when trying to do that, I get the following error:

Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed'

I am not sure how to get this to work consistently. I will greatly appreciate anyone who has an answer to this problem.

Thanks.

2 Answers 2

4

The exception object to be logged contains an instance of Closure class, PHP's implementation of anonymous functions and closure. Apparently anonymous functions cannot be serialized.

You need to investigate your exception classes and see if any of them is supposed to contain them. Normally, exception classes shouldn't have an anonymous function as property.

This reproduces the same error message as your case:

$exception = new Exception('BOO');
$anonymousFunction = function() { echo 'blah'; };
$exception->anonymousFunction = $anonymousFunction;
serialize($exception);

So dig in through your code, your framework's code, your library's code, and try to find out which exception class did have an anonymous function as class property, who assigned them, why - and then you should be able to create a special case for it.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

The closure may be stored in the exceptions backtrace, ie. when the exception was raised from within an anonymous function.
More specifically, one of the arguments provided to a function up the stack was a closure - calling a closure directly simply results in the string '{closure}' as the function name.
0

http://php.net/manual/en/function.set-error-handler.php

here's the global error handler definition function. you can define a global error handler and make it write the error description to the database.

And the structure of the exception class :

http://php.net/manual/en/class.exception.php

2 Comments

Thanks for the response. This is however not what I asked. I am well aware of how to construct an error handling framework. I'm asking specifically about converting an Exception and all it's data into a viable construct which can reside in the database.
did you try Exception e.toString(); look here : php.net/manual/en/class.exception.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.