How to serialize an Exception with closure

Asked

Viewed 108 times

5

I am trying to serialize the error generated exceptions to record a log with all the information, but the problem is that some exceptions may come with a closure within the trace and fire the message below:

Uncaught Exception 'Exception' with message 'Serialization of 'Closure' is not allowed'

If I write a file using the object $exception it records using the magic method __toString. I wanted to record the Exception therefore the use of the serialize / unserialize.

From what I saw, an object closure cannot be serialized, so how can I remove from the trace of Exception and serialize it? My last wish is to use OB* for that reason.

1 answer

7

If this is not a problem, you can include a package in your project to enable the serialization of Clousures.

Remember that Laravel 4 uses this same package for the same problem:

https://github.com/jeremeamia/super_closure

Taken from the Super Closure:

Have you Ever Seen this?

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

It’s true! If you Try to serialize a Closure, PHP will throw an Exception and Tell you that it is not allowed. But Even though it is not "allowed" by PHP, the Super Closure library (jeremeamia/superclosure on Packagist) makes it possible.

Free translation:

You’ve actually seen this?

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

Yes, it’s true! If you try to serialize a Closure, PHP will fling an exception stating that it is not possible. But even if this is not "allowed" by PHP, the Super Closure library (jeremeamia/superclosure in Packagist) makes this possible.

  • The biggest problem is this __Construct of that pack that gets a type Closure, but I will not always have it within the trace of Exception. Closures are part of the composition of the view, they are not important as log.

  • @Papacharlie then you actually want to ignore the Closures when it comes to serializing?

  • At the moment I loop us Traces to remove the index args. The closures are irrelevant in my case, so I found it simpler to discard them from Exception.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.