fix(api): duplicate exception raising and close file

This commit is contained in:
jo 2022-01-15 18:17:33 +01:00 committed by Kyle Robbertze
parent ad7686e8a7
commit 401808d7d1
2 changed files with 12 additions and 19 deletions

View file

@ -1,23 +1,24 @@
import configparser
import random
import string
import sys
from configparser import ConfigParser
def read_config_file(config_path):
def read_config_file(config_filepath):
"""Parse the application's config file located at config_path."""
config = configparser.ConfigParser()
config = ConfigParser()
try:
config.readfp(open(config_path))
except IOError as e:
with open(config_filepath, encoding="utf-8") as config_file:
config.read_file(config_file)
except IOError as error:
print(
"Failed to open config file at {}: {}".format(config_path, e.strerror),
f"Unable to read config file at {config_filepath}: {error.strerror}",
file=sys.stderr,
)
raise e
except Exception as e:
print(e.strerror, file=sys.stderr)
raise e
return ConfigParser()
except Exception as error:
print(error, file=sys.stderr)
raise error
return config