Consolidate database error handling logic