Tag Archives: exception

Do not be alarmed – Or do!

This morning I watched a video on Rusts Error-Handling that Larry Garfield posted on Mastodon.

The essence – at least to me – is that Exceptions throw one out of the normal flow of execution just like GOTO does. And we all know that GOTO is bad. SO how does Rust handle Errors, when Exceptions are bad and we always want to return a defined type?

Rust uses Result “enums” instead of a dedicated type that a function returns instead of throwing an exception or returning null. They contain either the expected return value or an Error.

I am not a mathematician. So I might be getting things wrong from a logical point of view. But as a developer I do have a slightly different view on the topic of Error and Exception handling.

Continue reading Do not be alarmed – Or do!

unserialize values

Last week I had the problem, that – due to a wrong database-entry – a serialized value got scrambled. And due to that (and some missconfiguration of the server) the application broke. Instead of a search result the user simply got an error message about not being able to unserialize the value due to an offset-error.

So how could I avoid the error (which actually is a notice) being thrown and besides that get to know whether unserializing worked or not?

The hack I finaly came up with is as follows:

  • suppress the notice in any way
  • check whether the returned value is a boolean false and the given string is ‘b:0;’ (which represents a boolean false in a serialized string).
  • If so, the unserialize() failed, so lets throw an InvalidArgumentException

And here it is:

public function my_unserialize ( $value )
{
    $unserialized = @unserialize ( $value );
    if ( false === $unserialized && 'b:0;' !== $value ) {
        $le = error_get_last ();
        throw new InvalidArgumentException ( $le['message'] );
    }
    return $unserialized;
}