From 0c94c232a44846d8654b4ef9d965fb1c2166ee4c Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 11 Mar 2012 21:05:09 -0400 Subject: [PATCH] better exception messages --- database.d | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/database.d b/database.d index 273a247..e55c05d 100644 --- a/database.d +++ b/database.d @@ -77,14 +77,17 @@ struct Row { package string[] row; package ResultSet resultSet; - string opIndex(size_t idx) { + string opIndex(size_t idx, string file = __FILE__, int line = __LINE__) { if(idx >= row.length) - throw new Exception(text("index ", idx, " is out of bounds on result")); + throw new Exception(text("index ", idx, " is out of bounds on result"), file, line); return row[idx]; } - string opIndex(string idx) { - return row[resultSet.getFieldIndex(idx)]; + string opIndex(string name, string file = __FILE__, int line = __LINE__) { + auto idx = resultSet.getFieldIndex(name); + if(idx >= row.length) + throw new Exception(text("no field ", name, " in result"), file, line); + return row[idx]; } string toString() { @@ -132,11 +135,7 @@ interface ResultSet { } class DatabaseException : Exception { - this(string msg) { - super(msg); - } - - this(string msg, string file, size_t line) { + this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } } @@ -529,9 +528,9 @@ class DataObject { } - string opIndex(string field) { + string opIndex(string field, string file = __FILE__, size_t line = __LINE__) { if(field !in fields) - throw new DatabaseException("No such field in data object: " ~ field); + throw new DatabaseException("No such field in data object: " ~ field, file, line); return fields[field]; }