Improving error handling – The beginning

For now there is not much error-handling implemented for all those database related functions, which is really bad.

Say I want to create a new blog post with content in German and English which is also associated to the category „General“, this results in three sql-statements that have to be triggered and are partly dependent on the results of each other.
First we create a new article in the database, for creating the content we need the ID of the new article and for creating the association of article and categories we also need the articleId. But do we want to create parts of all this if other parts fail or do we want to see an error message and no changes on the database.

At the moment there is literally no error handling. If the execution of a statement fails the following statements are not executed and the user will see a simple message that states that there was an error while trying to do that statement.
I tried to improve this by returning the error message which is returned from PDO::errorInfo; while implementing this I realized that this will work fine if there are no dependent queries or return value is boolean for success but in every other case I’ll still run into problems…

One problem is that I haven’t utilized transactions in the first place and therefore I might leave the database with obsolete, redundant and/or incomplete data.
To change this I added transactions for the parts of the code where I do have those dependent queries. This is alright as I keep my database a little cleaner, but as a trade off I now have a PDO::beginTransaction right before I try to execute the first query and one PDO::rollBack in each error-case (which typically is part of an if-else) plus an additional PDO::commit if every query was successful.
For the first case where I started to implement this the result was having one beginTransaction, one commit and three rollBack-statements. Furthermore returning the error-messages from errorInfo for this case meant switching most of the queries from returning data in the success-case and a boolean on errors to returning data on success AND data (error messages are strings too) on errors too.

First thought: Switch from associative arrays to objects for the return values of the database-queries. Well, this is an approach, but I still got to check for the return value type on each error-case and still keep those redundant rollBacks.
Then a sudden realization occurred: Why on earth am I not using exceptions?

Yeah, that could have been taken into account previously, but I just didn’t think about it earlier and, as the last PHP Version I was working with was PHP4, I didn’t even knew there are exceptions in PHP. Funny things occur, the previous day I read an article on PDO which featured some examples with exceptions.

So I threw away the changes I made today, which was not much of code, and will be redo this with exceptions and maybe also with objects for the return values.

Cheers
Sven


Leave a Reply

*