Welcome Guest, Not a member yet? Register   Sign In
Handle Database Errors properly
#1

[eluser]luster[/eluser]
In my model how do I catch and handle database errors properly?

I have:

function add_question($insert_array)
{
$this->db->insert('questions', $insert_array);

$insert_array['id'] = $this->db->insert_id();


return $insert_array;
}

I'm calling this via AJAX and when there is a DB error it blows up. I cannot get try catch to work. What is going on and what is the best practice for handling DB errors?

Thanks,

Justin
#2

[eluser]CroNiX[/eluser]
Code:
if ($this->db->insert(‘questions’, $insert_array) === FALSE)
{
  $error_message = $this->db->_error_message();
  $error_number = $this->db->_error_number();
} else {
  //successful insert
}
#3

[eluser]luster[/eluser]
Thanks for responding, but this does not work. As soon as the insert command runs HTML for the error is generated and code execution does not continue.

Here is the HTML error that is generated:

---------------------------------------------------------
A Database Error Occurred
Error Number: 1054

Unknown column 'list_name' in 'field list'

INSERT INTO `questions` (`name`, `type`, `header1`, `list_name`) VALUES ('Q1', 2, '', 'Q1list')

Filename: C:\wamp\www\codeigniter\system\database\DB_driver.php

Line Number: 330
---------------------------------------------------------

But I'd like to catch this and handle it.

Any ideas?
#4

[eluser]CroNiX[/eluser]
Do you have database debugging turned off?
#5

[eluser]luster[/eluser]
After looking in DB_driver.php under the "system" folder it looks like this is killing me:

function display_error (...)
{


echo $error->show_error($heading, $message, 'error_db');
exit;

}

Looks like for any DB errors it is displaying the error an then exiting. I know that some consider displaying error messages to be a security issue. Is there anyway to turn this off and to be able to handle the error myself?
#6

[eluser]luster[/eluser]
Turning off the database debugger worked. Thank you. Do you turn this off during development? What is best practice?
#7

[eluser]CroNiX[/eluser]
I use the ENVIRONMENT constant to set debugging on during development (so I can see errors) and off (since there shouldn't be any) for production. On production, it sends me an email alert if there is an error.
#8

[eluser]luster[/eluser]
Sorry, I'm new to this. I really appreciate your help.

So I can see that setting this:

define('ENVIRONMENT', 'production');

will disable all error output. Is there one convenient place in the framework where I can then log or email errors to myself or do I have to go throughout the code catching all of my errors and reporting them?
#9

[eluser]CroNiX[/eluser]
It depends on how you want to do it. Personally, I alter the db class which isn't advised as it makes updates harder since you'd have to manually reincorporate any changes. However, since there isn't a way to easily extend or alter the db class like you can with all other CI libraries, which is stupid, it is easier than modifying all of your db calls.

I just changed it to check for if debugging is on, if it is it processes the error normally and displays it. If not and the environment is production, it emails the error to an email address set in config.

Note: This might have changed with "drivers", but I haven't looked into that (don't know if you'd have to create an entirely new driver or if you can just extend an existing one like you can just extend (most) core classes)
#10

[eluser]luster[/eluser]
Okay, again, thank you very much. You are great.




Theme © iAndrew 2016 - Forum software by © MyBB