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)
/// <param name="reader"></param>
public static void ThrowDataException(Exception ex, int index, IDataReader reader)
{
string name = "(n/a)", value = "(n/a)";
if (reader != null && index >= 0 && index < reader.FieldCount)
Exception toThrow;
try
{
name = reader.GetName(index);
object val = reader.GetValue(index);
if (val == null || val is DBNull)
string name = "(n/a)", value = "(n/a)";
if (reader != null && index >= 0 && index < reader.FieldCount)
{
value = "<null>";
}
else
{
value = Convert.ToString(val) + " - " + Type.GetTypeCode(val.GetType());
name = reader.GetName(index);
object val = reader.GetValue(index);
if (val == null || val is DBNull)
{
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)
{
......
......@@ -111,11 +111,28 @@ insert Posts ([Text],CreationDate, LastChangeDate) values (replicate('x', 2000),
private static void RunTests()
{
var tester = new Tests();
int fail = 0;
foreach (var method in typeof(Tests).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
Console.Write("Running " + method.Name);
method.Invoke(tester, null);
Console.WriteLine(" - OK!");
try
{
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