Created
June 28, 2011 11:21
-
-
Save jhartikainen/1050932 to your computer and use it in GitHub Desktop.
PHP error to exception converter with special invalid argument handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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