Php mkdir( ) exception handling - php

mkdir() is working correctly this question is more about catching an error. Instead of printing this when the directory exists I would just like to have it write to a message to me in a custom log. How do I create this exception.
Warning: mkdir() [function.mkdir]: File exists

I would just like to have it write to a message to me in a custom log.
the solution is very easy. PHP already have everything for you:
ini_set('display_errors',0);
ini_set('log_errors',1);
ini_set('error_log','/path/to/custom.log');
or same settings in the php.ini or .htaccess
I think it would be better than write each possible error manually
If you don't want this error to be logged (as it may be not error but part of application logic), you can check folder existence first
if (!file_exists($folder)) mkdir($folder);
else {/*take some appropriate action*/}

You can stop the error message from displaying either by suppressing error messages globally (in config or runtime) with the display_errors setting, or case by case by prefixing the function call with an #-character. (E.g. #mkdir('...')).
You can then check with error_get_last when mkdir returns false.
For error logging global rules apply. You can log errors manually with error_log.
For further reading, see the manual section on Error handling.
Edit:
As suggested in the comments, a custom error handler is also a possible, arguably more robust (depending on your implementation) but certainly more elegant, solution.
function err_handler($errno, $errstr) {
// Ignore or log error here
}
set_error_handler('err_handler');
This way, the error message will not display, unless you explicitly echo it. Note, though, when using a custom error handler error_get_last will return NULL.

You can rewrite any system call function with a class like this:
file: system.php
namespace abc;
class System {
const CAN_NOT_MAKE_DIRECTORY = 1;
static public function makeDirectory($path) {
$cmd = "mkdir " . $path;
$output = \shell_exec($cmd . " 2>&1"); // system call
if ($output != "") {
throw new \Exception($output, System::CAN_NOT_MAKE_DIRECTORY);
}
return(\TRUE);
}
}
Then you can call the method and intercept the exception:
file: index.php
namespace abc;
require 'system.php';
try {
System::makeDirectory($directoryName);
} catch (\Exception $e) {
throw new \Exception($e->getMessage(), System::CAN_NOT_MAKE_DIRECTORY);
}
Now you can treat all the system errors with the try {...} catch(...) {...} finally {...} normally.

Related

How Laravel handles PHP warnings?

I'm trying to connect to a LDAP server using Laravel. It is important to say that I'm using the PHP functions ldap_connect and ldap_bind instead of using a package to handle it.
The point is that when I provide wrong user and password, the ldap_bind function gives to us a PHP warning. I'm OK with this warning and, as is in the documentation, the function returns false when the bind does not occur.
But, Laravel is throwing an exception when this warning is triggered. This is not an exception, Laravel should not throw an exception and I wouldn't like to handle this as an exception; I just have to build an if condition that will return a message to the user.
Does Laravel catch all warnings as an Exception?
This is the intended behavior for Laravel. Laravel will turn any error into an ErrorException instance. Here's the bootstrap() method inside the Illuminate/Foundation/Bootstrap/HandleExceptions.php class.
public function bootstrap(Application $app)
{
$this->app = $app;
error_reporting(-1);
set_error_handler([$this, 'handleError']);
set_exception_handler([$this, 'handleException']);
register_shutdown_function([$this, 'handleShutdown']);
if (! $app->environment('testing')) {
ini_set('display_errors', 'Off');
}
}
The error_reporting(-1); will set PHP to report all errors (read more here).
While this part of code:
set_error_handler([$this, 'handleError']);
Will set a custom error handler. If you check the handleError() method, it's pretty clear that Laravel will turn any error into an ErrorException instance.
public function handleError($level, $message, $file = '', $line = 0, $context = [])
{
if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line);
}
}
Read more about user-defined error handler here.
Hope this clear things up. :)
Not sure exactly the reason because I didn't write it but I assume having it work this way makes logging much easier.
Warnings get logged in php's error log but it's not possible to pass additional information along with it as context.
If that warning gets turned into an exception though, then you can pass the exception to the logger as well as other information such as the route which was hit, the request variables, etc... and log everything together. You also get to dictate the severity of the issue by designating it as log, debug, info, notice, warning, error, critical, alert, and emergency and through monolog, handle each of those as you see fit. This creates a log which is much easier to read and makes your code much easier to debug.
Additionally, as others have pointed out, it also allows you to do your work in try catch blocks which I believe also creates neater and cleaner code than trying to check if some variable === false

How to make a PHP autoloader throw an error when the autoloaded file has "compile-time" errors (never mind, it already does)

EDIT: It turned out that what I stated in this question was totally wrong. The code was actually turning off error reporting explicitly prior to autoloading, in places where I hadn't found it.
So this question is basically useless. The accepted answer is correct.
In my current configuration, whenever some PHP file has fatal errors such as syntax errors or calling a function that does not exist, I usually get an error message such like:
Parse error: syntax error, unexpected <whatever> in /path/to/file.php on line XXX
or
Fatal error: Call to undefined function whatever() in /path/to/file.php on line YYY
or the like in the very output.
However, I am using third-party libraries which use a third-party autoloader. Whenever there's a fatal error in any of the autoloaded classes (including parse errors or calling unexisting functions - actually not completely sure about the latter but definitely of the parse error case), I just get a blank page, and not only that: no error is even logged in Apache's error_log file, where usually PHP fatal errors would be logged. So debugging becomes impossible.
I can't stress this enough: this only happens when the fatal error is in some autoloaded file. In every other case (including of course errors in files included via require(), include() and the like), the same errors do show up in the output and in the error_log.
I didn't write the autoloader code, but it's basically like this:
// no idea why this line, but I don't think it's relevant:
ini_set('unserialize_callback_func', 'spl_autoload_call');
spl_autoload_register(array('My_Autoloader', 'autoload'), true);
class My_Autoloader {
static function autoload($classname) {
$filename = //.... computes $filename from $classname
require_once($filename);
}
}
There must be a way to have the autoloader throw errors the same way they would be thrown (and handled) if the errors were not in an autoloaded file, right?
How do I get that?
The only way that the code would behave as you suggest is if the third party code is overriding the error reporting. (EDIT by OP: Yep, it turns out it actually was.) That is usually considered good practice for production systems, but it should be logging the error.
That your third party code is causing such errors gives me pause to wonder about its quality, but we'll ignore that for now.
PHP's built in mechanisms will handle the reporting (to the browser) and the logging (to file). Non fatal errors can be managed by your own code after calling set_error_handler() however fatal errors are not handed off via this route. It is possible to trap and handle fatal errors in your own code using register_shutdown_function(). But start by checking your log files.
If, as you say, both error logging and error reporting are disabled, then stop using this third party code - it is toxic.
Syntax Error Check only on Command Line
With php -l somefile.php from PHP
shell_exec('php -l /fullpath/to/somefile.php')
but you have to analyse the respone string for errors.
Normal response No syntax errors detected in somefile.php
In PHP <= 5.0.4 there was php.net/manual/en/function.php-check-syntax.php
Here a fatal error catch that works:
register_shutdown_function(function(){
$err=error_get_last();
if($err['type']===1){
/*you got an fatal error do something, write it to an file*/
#file_put_contents(var_export($err,true),'myfatalerror.log');
}
});
Hope that helps:)
Use php exception so you can call your file into
function inverse($x) {
if (!$x) {
throw new Exception('Division par zéro.');
}
return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Exception reçue : ', $e->getMessage(), "\n";
}

file_exists() failing without safe_mode being on

spl_autoload_register(function ($className)
{
if (file_exists($className . '.php'))
{
require_once($className . '.php');
}
else
{
throw new Exception('Could not load class: ' . $className);
}
});
//Load models and save them in variable instances
try
{
$this->database = new Database (
$config['DB']['HOST_IP'],
$config['DB']['DATABASE_NAME'],
$config['DB']['USERNAME'],
$config['DB']['PASSWORD']
);
//Set the initial language for our template model.
Template::setLanguage();
}
catch (Exception $e)
{
echo $e->getMessage();
}
Script works if I erase file_exists, but file_exists returns false no matter what.
What may be causing this?
Also, I get an error message: Uncaught exception 'Exception' with message 'Could not load class: Template'. Is it because Template class is static?
There are 2 faults with your code.
Instead of meaningful and reasonable system error message you are ecoing just useless and generalized error message which cannot help you
You uare using try-catch just to echo a message. Which is deadly wrong.
So, just get rid of all try-catch blocks in your code and run it again.
And never use try..catch if you're not going to handle an error
instead of that set error reporting to be able to see the error messages
error_reporting(E_ALL);
ini_set('display_errors',1);
The problem was having two extensions on my PHP file. (e.g index.php.php) Sadly, the windows environment wasn't configured so I struggled finding the issue.

error handling in codeigniter

In my project to catch all the PHP errors I have set up my error handling mechanism as follows:
I have set error_reporting() in index.php file which overrides
anything in the php.ini file
An error handler is set in system/codeigniter/CodeIgniter.php using
set_error_handler - this error handler, _exception_handler, is
found in system/codeigniter/Common.php
The _exception_handler function ignores E_STRICT errors, calls the
show_php_error function From the Exceptions system library if the
severity is that specified by your error_reporting() function in
index.php and logs the error according to whatever you have set up in your config.php file
The handler returns FALSE so after this PHP goes on to handle the
error however it normally would according your error_reporting level
and display_errors setting.
The thing that is puzzling me is that E_ERROR errors i.e. fatal errors don’t seem to be being caught by _exception_handler at all. It’s not just that show_php_error isn’t being called, it looks like the function just isn’t being called for them. This is obviously a problem as it means that they aren’t get handled by show_php_error or logged. For example if I deliberately mistype $this->load->views('foo'); in a controller, the handler doesn’t get called.
Any suggestion about error handling would be much appreciated, thanks!
Now this is a rather big debate:
Whether you should catch the fatal errors or not.
Some say that they are FATAL so you dont know in which condition is the system but I will go with the "try to do the cleanup if the error occured".
In order to catch ALL fatal errors you will need to setup a pre_system hook.
go to application/config/hooks.php and enter
$hook['pre_system'][] = array(
'class' => 'PHPFatalError',
'function' => 'setHandler',
'filename' => 'PHPFatalError.php',
'filepath' => 'hooks'
);
after that go to hooks directory and add your handling of the error:
<?php
class PHPFatalError {
public function setHandler() {
register_shutdown_function('handleShutdown');
}
}
function handleShutdown() {
if (($error = error_get_last())) {
ob_start();
echo "<pre>";
var_dump($error);
echo "</pre>";
$message = ob_get_clean();
sendEmail($message);
ob_start();
echo '{"status":"error","message":"Internal application error!"}';
ob_flush();
exit();
}
}
as you can see we are using the register_shutdown_function to run a function that checks if an error had occured and if it had send it via email to the developer.
This setup is working flawlessly for over 2 years in several CI projects that I have been working with.
I've found this answer under an other question (https://stackoverflow.com/a/3675357), and i think it is also useful for anyone reading this question.
"For codeigniter specific error handling, you can override its 'Exception' library class by creating My_Exception class in your application/libraries folder. Copy original library function signatures into it and put in your code. It will surely work."
Simply you can handle all type of error in one file which is display
your client because of php error or any other error is not good to
display client.
Simply place file Common_Exceptions.php in core folder . Common is my because in config file I have declare $config['subclass_prefix'] = 'Common_';
Copy system\core\Exceptions.php file and paste in core\Common_Excepions file (class Common_Exceptions extends CI_Exceptions) and do your change in this file and call view for your want and display to client when error come.
NOTE: $config['log_threshold'] = 1; enable in config file for errorlog write and after you see what error come.
One more suggestion on view file which is display when error is come there place time so when you see in log then match this time and find which error is come that time

Detecting the rmdir error with php

rmdir() displays a few warnings like the dir does not exist, or permissions did not allow. How can I capture which reason for failure and react to it?
rmdir does not throw Exception so you cannot catch them with try/catch. What you can do is use error_get_last function to do what you need.
Try something like this:
if (!#rmdir('/root')) {
$error = error_get_last();
if (preg_match('/something/', $error['message'])) {
// do something
} elseif (preg_match('/somethingelse/', $error['message'])) {
// do something
}
}
You can check beforehand if you are allowed to do some kind of action like file_exists() and is_ dir() to check if a directory exists and fileperms() or just is_ writable() to check if you can write a directory.
You can also try to "catch" the error like with exceptions. you can specify a custom error handler, but this seems to be a bit overkill.

Categories