Refactoring REST error logic