What is the best way to catch DB\SQL\Mapper errors?

486 views
Skip to first unread message

Lee Blue

unread,
Feb 14, 2014, 1:58:57 AM2/14/14
to f3-fra...@googlegroups.com
Suppose you have code that saves a mapper to a table and the table has a unique index on one of the columns, perhaps an "email" column. If your mapper attempts to insert a new row into the table with a duplicate email then you get an error:

Internal Server Error

PDOStatement: Duplicate entry 'exa...@example.com' for key 'email'

What's the best way to catch this error and do something about it? Suppose this was a form post to create a new record, it would be nice to show an error message about entering a different email address.

If the code was something like this:

$user = new \DB\SQL\Mapper($db, 'users');

$user->email = 'exa...@example.com';

$user->password = 'secret';

$user->save(); // This line needs some sort of condition to catch errors

What are the best practices for catching insert failures and handling them appropriately?

Thanks for the help!

Sascha

unread,
Feb 14, 2014, 2:34:33 AM2/14/14
to f3-fra...@googlegroups.com
Well, if you ask for best practice, I'd say you should use try..catch: http://php.net/manual/en/language.exceptions.php

F3 also supports your own error handlers with ONERROR http://fatfreeframework.com/quick-reference#onerror

stu warneski

unread,
Feb 15, 2014, 12:39:18 PM2/15/14
to f3-fra...@googlegroups.com
I suggest the best practice for this would be to use jQuery to fire an ajax call to a route when the user loses focus in the form field for this email address.

that event would send an ajax call to the scripts, which queries the db for a matching record. 
if a matching record was found, the php script would return a string via ajax, then you would use javascript to inform user that email was already in use.

Sascha

unread,
Feb 15, 2014, 1:16:42 PM2/15/14
to f3-fra...@googlegroups.com
I think you didn't unterstand what he's looking for. The problem is, that the script always causes an error 500. Means, you will never have any chance to return json. Well, F3 returns the error 500 as json, but that's the way you should do it.

Lee Blue

unread,
Feb 15, 2014, 1:22:32 PM2/15/14
to f3-fra...@googlegroups.com
I ended up just writing logic in the app to check for duplicate emails before attempting to save. I couldn't use a try/catch block because it was throwing a hard Internal Server Error, not an exception. 

Thank you for the suggestions! The F3 community is cool :)

bioshock

unread,
Feb 16, 2014, 9:47:57 AM2/16/14
to f3-fra...@googlegroups.com
I am having the same problem. i would like to catch and act accordingly for PDO errors , but it seems F3 is treating them as NON recoverable Error. Any Suggestion on how to approach to  this kind of situations?

Lee Blue

unread,
Feb 17, 2014, 12:43:55 PM2/17/14
to f3-fra...@googlegroups.com
I decided to write as much logic into the application code as I could - like checking for duplicates, data validation, etc. Then, in theory, the only DB failures would be hard system failures like a lost connection or something. I'll write an error handler to show a nicely designed error page if a major system problem like a lost database connection happens.

xfra35

unread,
Feb 24, 2014, 6:35:32 AM2/24/14
to f3-fra...@googlegroups.com
In order to catch PDO errors you need to activate PDO exceptions. This can be done like this:
$db=new \DB\SQL($dsn,$user,$pwd,array(\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION));

then:
try {
  $db->exec($sql);
} catch(\PDOException $e) {
  //PDO exception caught
Reply all
Reply to author
Forward
0 new messages