Extract error handling to it's own method

The around serialize/deserialize blocks contain a catch block, which
calls $self->log in case of errors. But because the logger may not be
set at all, it fails with the error message 'cannot call log on $self'
instead of the actual important serialization/deserialization error
message. This PR changes error handling to call $self->log, if the
logger is present or print to STDERR otherwise.