aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2021-01-10 14:29:09 -0800
committerGravatar MarkKoz <[email protected]>2021-01-10 17:04:59 -0800
commitfd7e6c03cee43d0d4eb5a4a9821b9b6b4932c55e (patch)
tree9ad9abd81a099f9caab73100e9401daef70b55e6
parentRead the Python path and args from the config file (diff)
Add error handling to reading of the nsjail config
-rw-r--r--snekbox/nsjail.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py
index 6972a40..d5659e5 100644
--- a/snekbox/nsjail.py
+++ b/snekbox/nsjail.py
@@ -47,8 +47,22 @@ class NsJail:
def _read_config() -> NsJailConfig:
"""Read the NsJail config at `NSJAIL_CFG` and return a protobuf Message object."""
config = NsJailConfig()
- with open(NSJAIL_CFG, encoding="utf-8") as f:
- text_format.Parse(f.read(), config)
+
+ try:
+ with open(NSJAIL_CFG, encoding="utf-8") as f:
+ config_text = f.read()
+ except FileNotFoundError:
+ log.fatal(f"The NsJail config at {NSJAIL_CFG!r} could not be found.")
+ sys.exit(1)
+ except OSError as e:
+ log.fatal(f"The NsJail config at {NSJAIL_CFG!r} could not read.", exc_info=e)
+ sys.exit(1)
+
+ try:
+ text_format.Parse(config_text, config)
+ except text_format.ParseError as e:
+ log.fatal(f"The NsJail config at {NSJAIL_CFG!r} could not be parsed.", exc_info=e)
+ sys.exit(1)
return config