Refactor error handling in CLI