Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Created June 28, 2011 11:21
Show Gist options
  • Save jhartikainen/1050932 to your computer and use it in GitHub Desktop.
Save jhartikainen/1050932 to your computer and use it in GitHub Desktop.
PHP error to exception converter with special invalid argument handling
<?php
//This converts errors into exceptions, but if the error is caused
//by an argument being invalid (for example failing a typehint), it gets converted into an InvalidArgumentException
function handle($code, $message, $file, $line) {
//This test might be naive but it worked in my very very simple test code :)
if(strpos($message, 'Argument ') === 0) {
throw new InvalidArgumentException($message);
}
else {
throw new ErrorException($message, 0, $code, $file, $line);
}
}
set_error_handler('handle');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment