Refactor error handling code in parser