Database error handling code