datasnakes / renv

Creating virtual environments for R.
MIT License
17 stars 0 forks source link

Work out error handling when calling R from subprocess. #49

Open grabear opened 5 years ago

grabear commented 5 years ago

Is this the only exception we might hit?

Originally posted by @santina in https://github.com/datasnakes/renv/pull/42

grabear commented 5 years ago

What if we actually got an error in the system_r_call? i.e. stdout is not actually the information we want. We should handle that.

Originally posted by @santina in https://github.com/datasnakes/renv/pull/42

sdhutchins commented 5 years ago

I think with the proc.communicate function it's typically best to use the TimeoutExpired exception since that's the main thing that function depends on is a timeout.

However, you could do a bigger catch like...

try:
    recommended_pkgs = sp.Popen([Rcmd], stderr=sp.PIPE, stdout=sp.PIPE, 
                                shell=True, encoding='utf-8')
    try:
        stdout, stderr = recommended_pkgs.communicate(timeout=15)
    except TimeoutExpired:
        recommended_pkgs.kill()
        stdout, stderr = recommended_pkgs.communicate()
    return stdout, stderr
except subprocess.CalledProcessError as err:
  # There was an error - command exited with non-zero code
  logger.error(err)
sdhutchins commented 5 years ago

It may be better to write functions for both.