PHP cannot catch Error from within included file - php

i try to implement a fallback solution for when my database is not available. Somhow i do not manage to catch the error.
Call to create connection:
if(!$this->connection = mysqli_connect(
$this->host,
$this->name,
$this->pass,
$this->db
)) { throw new Exception('Unable to connect to Database'); }
init.php to include dbClass
require_once __DIR__ . '/../classes/Database.php';
$db = new Database();
$connection = $db->getConnection();
actual usage with try catch wrap
try {
include __DIR__ . '/../out/script/content/config/init_direct.php';
... do stuff regulary
}catch (Exception $e) {
... do fallback stuff
}
i do not get into the catch block. for test purpose i just set the database offline.

there are several problems with your approach
First, your problem is insufficient debugging. You just assume that exception has been thrown, but in reality it weren't.
Second, this happened because mysqli_connect returns an object, not boolean you expect.
Third, as you've been told in the comments, errors aren't exceptions and you cannot catch them.
Anyway, with mysqli you don't need to throw exceptions manually - this extension can throw them by itself, so, all you need is to set the proper mode up - an exception will be thrown which will be caught all right.

Related

PHP when to use try/catch?

I was just wondering something. In index.php, I am currently doing something like this
function performFtpOperation() {
global $config;
try {
$ftp = new FTP\FtpClient();
$ftp->connect($config::FTP_SERVER);
$ftp->login($config::FTP_USER, $config::FTP_PASSWORD);
} catch (Exception $e) {
echo 'Error: ', $e->getMessage();
}
}
What I was wondering is if that try catch block is needed? Reason I question it is because my FTP class throws errors if something goes wrong. For instance this is the connect function
public function connect($host, $ssl = false, $port = 21, $timeout = 90)
{
if ($ssl) {
$this->conn = #$this->ftp->ssl_connect($host, $port, $timeout);
} else {
$this->conn = #$this->ftp->connect($host, $port, $timeout);
}
if (!$this->conn) {
throw new Exception('Unable to connect');
}
return $this;
}
So would a try/catch be needed if errors are handled within the class?
Thanks
When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block.
If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.
Proper exception code should include:
Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
Catch - A "catch" block retrieves an exception and creates an object containing the exception information
The connect class throws exceptions that you need to catch and handle somewhere in your code. It is up to you where to handle it depending on your application design and requirements.
If you decided that you wanted to handle them in the performFtpOperation function, then your use of try...catch there is correct.
If you don't handle them in the performFtpOperation function, then they will bubble up to the code that calls the performFtpOperation function and you can catch & handle them there if you like using try...catch similar to how you did it here. Just remember that you need to catch them somewhere.
Well since you are throwing an exception from within your FTP code, then Yes.
You need to try catch if you work with exeptions, otherwise you generate fatal errors and your script/site will stop work at this point, so try/catch is a part of the exeption handling.
you can inform yourself about this in the php manual
http://php.net/manual/en/language.exceptions.php

how to redirect to a page if PDO transaction fails?

I need to redirect to a separate page to show error. header("Location: errorpage.php?errorcode=11"); after does not seem to work.
<?php
$db = new PDO("mysql:host=localhost;dbname=mydbase;charset=utf8", "user", "password");
try {
$db->beginTransaction();
$db->exec("SOME QUERY");
$db->commit();
}
catch(PDOException $ex) {
$db->rollBack();
//Something went wrong so I need to redirect
header("Location: errorpage.php?errorcode=11");
}
PDO's error handling is a little unusual. It has modes for throwing real exceptions, issuing PHP warnings, or just being silent.
Silent is the default. What has happened here is that no exception is ever thrown because you did not configure PDO to throw one. So the catch block is never entered and header() is never called. Setup your $db object to throw exceptions:
// Ensure PHP's native error handling is showing
// on screen (to catch problems with header() itself)
error_reporting(E_ALL);
// Always in development, disabled in production
ini_set('display_errors', 1);
$db = new PDO("mysql:host=localhost;dbname=mydbase;charset=utf8", "user", "password");
// Turn on exceptions for PDO so the try/catch is meaningful
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$db->beginTransaction();
$db->exec("SOME QUERY");
$db->commit();
}
catch(PDOException $ex) {
$db->rollBack();
//Something went wrong so I need to redirect
header("Location: errorpage.php?errorcode=11");
// Always make an explicit call to exit() after a redirection header.
exit();
}
About PDO::exec():
I know this is example code, but exec() is not usually the right method to use. If you are doing DDL statements, the transaction won't work since MySQL doesn't support that, and if you are doing things like INSERT/UPDATE/DELETE with any user input, you should be doing prepare()/execute() to create an injection-safe prepared statement instead.

Uncaught PDOException reveals username and password

try {
self::$dbinstance = new PDO(
"mysql:host=$c[host];dbname=$c[dbname]", $c['user'], $c['password']
);
self::$dbinstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo "Errors" . $e->getMessage();
}
In the above code, if PDO fails to connect to the host, a fatal error reveals the username and password.
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003]
Can't connect to MySQL server on '172.25.102.65' (10060)' in
D:\xampp\htdocs\mytest\wh_client_2.1\classes\importmodule-class.php:33 Stack trace: #0
D:\xampp\htdocs\mytest\wh_client_2.1\classes\importmodule-class.php(33): PDO-
>__construct('mysql:host=172....', 'host', 'password') #1
One possible way is to turn the display_error=0 off in php.ini, but this way I won't able to know that when my host is not responding.
Is there a way I can modify the error message?
There is a difference between error handling and error reporting.
Error handling is the process of preventing your end users to see any stack trace, vital information or automatically generated error messages. It can also modify the way your script runs by using a try catch block.
Error reporting defines which information will be reported by a given script.
To handle errors properly, I think that ini_set('display_errors',0); is the better approach. You do not want any error message displaying on the screen.
However, I want to have all possible information on errors, so I use error_reporting(E_ALL);.
Errors are written in a file, error_log, which usually resides at the same level as your index.php (or any PHP file called directly). You can also access it from your cPanel.
Your error is probably uncaught because your code is in a namespace, whereas you want to catch the global namespace PDOException. Use a \ to indicate your script you're looking for the global PDOException. Once you catch your error, you can echo the content you want, using the normal methods of the PDOException class.
try {
$db = new PDO (/*connection infos*/);
}
catch (\PDOException $e) {
switch ($e->errorCode()) {
case 'HY000':
// Or whatever error you are looking for
// here it's the general error code
mail('your#email.com','connection problem',$e->getTraceAsString());
$db = new PDO (/*rollback connection infos of a local database*/);
break;
}
}
That would send you a mail, containing the trace of the error, preventing your user from seeing it while telling you something is wrong.
Here is the reference for the error codes returned by PDO statements.
When your host is not responding you will know all right - your host will stop responding. Then you have to peek into the error log and find the error message with the particular error.
So, just keep with display_errors=0 as it's a must-have in a production environment anyway.
No, don't try to throw the exception as it will spit out such critical information... Handle them with some appropriate custom error messages and handle those exceptions inside your custom logging functions...
You must be doing something similar to this...
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'uname', 'pass');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->query('bla bla bla bla'); //<---- This will definitely fail!!!!
} catch(PDOException $ex) {
echo "An error occurred!";
file_put_contents('somefile.txt', $ex->getMessage(), FILE_APPEND);
}
As you can see, the above query is indeed going to fail. So the end user will be seeing just An error occurred! message, but the error will be logged to your somefile.txt file.
You can do something like this:
<?php
// connect
try
{
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
}
catch (PDOException $e)
{
$dbh = NULL;
}
// check if connected
if($dbh)
{
// run queries
}
else
{
die('Oops! Our server has encountered an error, please try again later');
}
?>

How to force PDOException / reach catch block (php)

Here is my basic php code:
try{
$db = new PDO("mysql:dbname=name;host=localhost", $db_username, $db_password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//run queries etc...
} catch (PDOException $e) {
//handle and log error appropriately
}
Is there an easy way to reach the catch block or force a PDOException so that I can test and/or debug the page in the event that a database error occurs?
Is there an easy way to reach the catch block
Yes. Like in any other case, just force an intentional error.
or force a PDOException
Just like any other exception, throw new PDOException('test');
} catch (PDOException $e) {
//handle and log error appropriately
}
This is wrong way of handling errors. Create and install an exception handler. While you have to keep try..catch for another purpose.
Make a fake (bad) connection. If your db name is 'name', put 'name1'.
-EDIT- What's with the downvote? It works... Would've commented, but I can't yet.

try catch vs if else in PDO and some other things

There is this question that I found:
What is the advantage of using try {} catch {} versus if {} else {}
If you can add anything to it then please do as I am new to PDO, also what does this mean;
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
On the MySQL website it says "ensures the creation of exceptions rather than errors" but I do not understand this, can anyone please elaborate on it please.
Exceptions are catchable via try/catch and are classes with properties, while proceedural errors cannot and are not. Proceedural errors are instead handled by PHP's native error handling.
You can observe the behavior difference by manually triggering them.
to throw an Exception:
throw new Exception();
to trigger a proceedural error:
trigger_error($message, U_USER_ERROR);
Exceptions are bassically the OO way of error handling. Find more information about Exceptions here: http://php.net/manual/en/language.exceptions.php
Well, they are pretty similar. In PHP native error handling you can trow your own errors with trigger_error($error, [$level]) as you can throw your own exceptions with throw new MyException($error); you can set a default error handler with set_error_handler() which will manage all PHP error (except parsing) your own way as you can set a default exception handler with set_exception_handler(). Both PHP native errors and exceptions are automatically triggered/thrown somehow: PHP native errors compiling the script, exceptions if you are using specific items such as (PDO) or something.
Now let's try the same code with different approaches:
With PHP native errors you can do things such as:
$db = new Database();
if ($db === NULL) { trigger_error("Cannot connect to the database", E_USER_ERROR); }
$result = $db->query("UPDATE charlieiscool SET iloveunicorns = 1 WHERE userid = 1");
if (!$result) { trigger_error("Error updating table", E_USER_ERROR); }
$file = 'log.php';
if (!file_exists($file) or !file_get_contents($file)) { trigger_error("$file not found", E_USER_ERROR); }
require($file);
I think this does not really need any explanation. If an error is triggered, the entire script is skipped and you see the error. There are no more things you can do; you could set E_USER_ERROR or E_USER_NOTICE or E_USER_WARNING and handle them differently, but you have not that big choice. Now take a look at a possible OOP approach with try{} catch() {} blocks:
try {
$db = new Database();
if (!$db) { throw new DBEx("Cannot connect to the database"); }
$result = $db->query("UPDATE charlieiscool SET iloveunicorns = 1 WHERE userid = 1");
if (!$result) { throw new QueryEx("Query failed"); }
} catch (PDOException $e) {
echo $e->getMessage();
} catch (DBEx $e) {
$e->customFunction();
} catch (QueryEx) {
$e->customFunctionForQuery();
}
try {
$file = 'log.php';
if (!file_exists($file) or !file_get_contents($file)) { throw new FileEx("$file does not exists"); }
require($file);
} catch (FileEx) {
$e->fileGenerate();
$e->logError();
}
The main difference is that if the first try{} block throw an exception the second try{} will be executed any way. In fact if an exception is thrown, only the rest of the script inside that try{} block will be skipped.
Another difference (the one i love most) is that you can create several classes (extending mains Exception or PDOException or others) and customize very much your error handling behavior. You have unlimited possibilities to customize your classes, adding functions, editing already existing ones. You can add specific function (such as $e->fileGenerate();) and call them inside the catch() {} block where needed.
Notice also that if you want your entire script to stop if an error occurred, means that that error needs trigger_error(); instead if you want that an error only stops a specific block of code related to that error then it's time for try and catch.
You should not use one replacing the other, you should use one beside the other evaluating each errors as it is.
By the way PDO::setAttribute() change default values and options in your database handler. You can for example change your default fetch (used in PDOStatement::fetch()) with $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);.
References:
PDO::setAttribute()
set_error_handler()
set_exception_handler()
Exceptions
Predefined exceptions

Categories