Commit cc293ed9 authored by Marc Gravell's avatar Marc Gravell

ThrowDataException could go screwy if the reader was closed; compensate

parent c7bb6424
...@@ -1778,21 +1778,29 @@ static List<FieldInfo> GetSettableFields(Type t) ...@@ -1778,21 +1778,29 @@ static List<FieldInfo> GetSettableFields(Type t)
/// <param name="reader"></param> /// <param name="reader"></param>
public static void ThrowDataException(Exception ex, int index, IDataReader reader) public static void ThrowDataException(Exception ex, int index, IDataReader reader)
{ {
string name = "(n/a)", value = "(n/a)"; Exception toThrow;
if (reader != null && index >= 0 && index < reader.FieldCount) try
{ {
name = reader.GetName(index); string name = "(n/a)", value = "(n/a)";
object val = reader.GetValue(index); if (reader != null && index >= 0 && index < reader.FieldCount)
if (val == null || val is DBNull)
{ {
value = "<null>"; name = reader.GetName(index);
} object val = reader.GetValue(index);
else if (val == null || val is DBNull)
{ {
value = Convert.ToString(val) + " - " + Type.GetTypeCode(val.GetType()); value = "<null>";
}
else
{
value = Convert.ToString(val) + " - " + Type.GetTypeCode(val.GetType());
}
} }
toThrow = new DataException(string.Format("Error parsing column {0} ({1}={2})", index, name, value), ex);
} catch
{ // throw the **original** exception, wrapped as DataException
toThrow = new DataException(ex.Message, ex);
} }
throw new DataException(string.Format("Error parsing column {0} ({1}={2})", index, name, value), ex); throw toThrow;
} }
private static void EmitInt32(ILGenerator il, int value) private static void EmitInt32(ILGenerator il, int value)
{ {
......
...@@ -111,11 +111,28 @@ insert Posts ([Text],CreationDate, LastChangeDate) values (replicate('x', 2000), ...@@ -111,11 +111,28 @@ insert Posts ([Text],CreationDate, LastChangeDate) values (replicate('x', 2000),
private static void RunTests() private static void RunTests()
{ {
var tester = new Tests(); var tester = new Tests();
int fail = 0;
foreach (var method in typeof(Tests).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) foreach (var method in typeof(Tests).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{ {
Console.Write("Running " + method.Name); Console.Write("Running " + method.Name);
method.Invoke(tester, null); try
Console.WriteLine(" - OK!"); {
method.Invoke(tester, null);
Console.WriteLine(" - OK!");
} catch (Exception ex)
{
fail++;
Console.WriteLine(" - " + ex.Message);
}
}
Console.WriteLine();
if(fail == 0)
{
Console.WriteLine("(all tests successful)");
}
else
{
Console.WriteLine("#### FAILED: {0}", fail);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment