At least, your application should not "break" : when an error is detected (be it because you detected a problem could happen and avoided it, or because a problem did happen), you should display some nice error message, and, eventually, log the technical informations of the error.
About bloating the code : I would not put too much tests and all that in my code : it would end up in something hard to understand and maintain -- which is important !
What I generally try to do is :
- Test for errors in the user-supplied data
- Using a specific class, for instance, so those checks are not in the middle of the code that deals with database and business rules.
- Those tests are relatively precise, in order to generate useful error messages for the user
- For instance : "You should not input more than 20 characters"
- Or "there is already a user with that e-mail address"
- Basically, the important thing here is the user.
- When user-supplied data seems OK, I work with it.
- And there, if some error happens, it'll most likely be a technical error
- Which should be logged
- And only a "oops, an error occured" should be displayed to the user.
- Which means that, here, tests are not as precise : we only need to know if it works or not -- not necessarily in great details.
Of course, in the end, you should ensure that the data in the DB is correct, and that you don't save only half of the data.
A common way of dealing with technical errors is using exceptions ; here is a very basic idea :
try {
// Begin transaction to the DB
// Some code that might fail and throw an Exception
// Some other code that might fail and throw an Exception
// Code here will not be executed if an Exception has been thrown
// Commit DB transaction
} catch (Exception $e) {
// Rollback transaction (cancels the queries that were sent to the DB)
// Log technical informations to a file
// Display a nice message
}
The great thing is that it allows one to place all error-handling code in a single place, and put less testing code in the middle of the important stuff ; i.e. "let it fail, we'll deal with the problems later"