improved error handling in generated stub to prevent runtime errors