diff --git a/source/dnetd/app.d b/source/dnetd/app.d index 30e86af..65a693c 100644 --- a/source/dnetd/app.d +++ b/source/dnetd/app.d @@ -32,5 +32,6 @@ void main() } catch(GeneralException e) { + logger.log(e.toString()); } } diff --git a/source/dnetd/config.d b/source/dnetd/config.d index 7d81b19..0a5cc3e 100644 --- a/source/dnetd/config.d +++ b/source/dnetd/config.d @@ -4,11 +4,17 @@ module dnetd.config; import dnetd.app : logger; -import std.json : JSONValue, JSONException; +import std.json : JSONValue, JSONException, parseJSON; import dnetd.exceptions; +import std.stdio : File; /** +* In its instance-form this represents a read-in and parsed configuration +* of the configuration file in an OOP format, with the ability to write back +* any modifications made to it (TODO: Add this). * +* In static use it provides the ability to translate between the JSON configuration +* file and the instance (OOP) form. */ public final class Configuration { @@ -39,7 +45,8 @@ public final class Configuration } catch(JSONException e) { - logger.log("Error whilst parsing the configuration: "~e.toString()); + /* TODO: Throw proper error here saying which key is missing */ + throw new ConfigurationError(e); } return config; @@ -54,8 +61,32 @@ public final class Configuration */ public JSONValue readConfig(string path) { + File file; + file.open(path); /* TODO:Check this for errors */ + /* TODO: Only open with read rights */ + + /* Allocate a buffer for the file */ + byte[] contents; + contents.length = file.size(); /* TODO: Check size here */ + + /* TODO: Check this */ + /* TODO: Technically the below is fine */ + file.rawRead(contents); + JSONValue config; + try + { + config = parseJSON(cast(string)contents); + } + catch(JSONException e) + { + /* TODO: Get specific error here to show where config syntax is wrong */ + + + throw new ConfigurationError(e); + } + return config; } @@ -66,7 +97,13 @@ public JSONValue readConfig(string path) */ public final class ConfigurationError : GeneralException { - this() + this(string message) { + super(message); + } + + this(JSONException e) + { + super("JSON configuration has a syntax error ("~e.msg~")"); } } diff --git a/source/dnetd/exceptions.d b/source/dnetd/exceptions.d index 0e6b51c..5b45006 100644 --- a/source/dnetd/exceptions.d +++ b/source/dnetd/exceptions.d @@ -10,7 +10,24 @@ import std.exception; */ public class GeneralException : Exception { - this() + /* Name of exception type and description */ + private string errorTypeName; + private string message; + + /** + * TODO: Make this take in a `string message` + */ + this(string message) { + import std.string : split; + string[] fragments = split(this.classinfo.name, "."); + errorTypeName = fragments[fragments.length-1]; + this.message = message; + super(null); + } + + public override string toString() + { + return errorTypeName~": "~message; } }