factor out common parse and error handling logic