From d45f89c95b0f61c6b850f73807e9b264796e08a2 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Tue, 2 Sep 2014 10:48:05 +0100 Subject: [PATCH] More helpful error messages for missing config --- synapse/config/_base.py | 25 +++++++++++++++++++++++-- synapse/config/server.py | 2 +- synapse/config/tls.py | 8 +++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/synapse/config/_base.py b/synapse/config/_base.py index 08de6ee5e..91c0229d8 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py @@ -21,6 +21,10 @@ import os import yaml +class ConfigError(Exception): + pass + + class Config(object): def __init__(self, args): pass @@ -29,8 +33,25 @@ class Config(object): def abspath(file_path): return os.path.abspath(file_path) if file_path else file_path - @staticmethod - def read_file(file_path): + @classmethod + def check_file(cls, file_path, config_name): + if file_path is None: + raise ConfigError( + "Missing config for %s." + " Try running again with --generate-config" + % (config_name,) + ) + if not os.path.exists(file_path): + raise ConfigError( + "File % config for %s doesn't exist." + " Try running again with --generate-config" + % (config_name,) + ) + return cls.abspath(file_path) + + @classmethod + def read_file(cls, file_path, config_name): + cls.check_file(file_path, config_name) with open(file_path) as file_stream: return file_stream.read() diff --git a/synapse/config/server.py b/synapse/config/server.py index 1f20d55d8..a406cc892 100644 --- a/synapse/config/server.py +++ b/synapse/config/server.py @@ -60,7 +60,7 @@ class ServerConfig(Config): " service on the given port.") def read_signing_key(self, signing_key_path): - signing_key_base64 = self.read_file(signing_key_path) + signing_key_base64 = self.read_file(signing_key_path, "signing_key") signing_key_bytes = decode_base64(signing_key_base64) return nacl.signing.SigningKey(signing_key_bytes) diff --git a/synapse/config/tls.py b/synapse/config/tls.py index 005fc1d16..e78ad32e8 100644 --- a/synapse/config/tls.py +++ b/synapse/config/tls.py @@ -31,7 +31,9 @@ class TlsConfig(Config): self.tls_private_key = self.read_tls_private_key( args.tls_private_key_path ) - self.tls_dh_params_path = self.abspath(args.tls_dh_params_path) + self.tls_dh_params_path = self.check_path( + args.tls_dh_params_path, "tls_dh_params" + ) @classmethod def add_arguments(cls, parser): @@ -45,11 +47,11 @@ class TlsConfig(Config): help="PEM dh parameters for ephemeral keys") def read_tls_certificate(self, cert_path): - cert_pem = self.read_file(cert_path) + cert_pem = self.read_file(cert_path, "tls_certificate") return crypto.load_certificate(crypto.FILETYPE_PEM, cert_pem) def read_tls_private_key(self, private_key_path): - private_key_pem = self.read_file(private_key_path) + private_key_pem = self.read_file(private_key_path, "tls_private_key") return crypto.load_privatekey(crypto.FILETYPE_PEM, private_key_pem) @classmethod