Refactor universal error handling