提交 fca49ebc 编写于 作者: N Nick Craver

SplitOn: improve error message

Fixes #866. This provides a more informative error, saying _which_ splitOn column couldn't be found in the result set.
上级 ffc02eb2
...@@ -1679,7 +1679,7 @@ private static int GetNextSplitDynamic(int startIdx, string splitOn, IDataReader ...@@ -1679,7 +1679,7 @@ private static int GetNextSplitDynamic(int startIdx, string splitOn, IDataReader
{ {
if (startIdx == reader.FieldCount) if (startIdx == reader.FieldCount)
{ {
throw MultiMapException(reader); throw MultiMapException(reader, splitOn);
} }
if (splitOn == "*") if (splitOn == "*")
...@@ -1713,7 +1713,7 @@ private static int GetNextSplit(int startIdx, string splitOn, IDataReader reader ...@@ -1713,7 +1713,7 @@ private static int GetNextSplit(int startIdx, string splitOn, IDataReader reader
} }
} }
throw MultiMapException(reader); throw MultiMapException(reader, splitOn);
} }
private static CacheInfo GetCacheInfo(Identity identity, object exampleParameters, bool addToCache) private static CacheInfo GetCacheInfo(Identity identity, object exampleParameters, bool addToCache)
...@@ -1831,16 +1831,18 @@ private static void PassByPosition(IDbCommand cmd) ...@@ -1831,16 +1831,18 @@ private static void PassByPosition(IDbCommand cmd)
return reader => handler.Parse(type, reader.GetValue(startBound)); return reader => handler.Parse(type, reader.GetValue(startBound));
} }
private static Exception MultiMapException(IDataRecord reader) private static Exception MultiMapException(IDataRecord reader, string splitOnColumnName = null)
{ {
bool hasFields = false; bool hasFields = false;
try { hasFields = reader != null && reader.FieldCount != 0; } try { hasFields = reader != null && reader.FieldCount != 0; }
catch { /* don't throw when trying to throw */ } catch { /* don't throw when trying to throw */ }
if (hasFields) if (hasFields)
{ {
#pragma warning disable CA2208 // Instantiate argument exceptions correctly return new ArgumentException(
return new ArgumentException("When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn"); string.IsNullOrEmpty(splitOnColumnName)
#pragma warning restore CA2208 // Instantiate argument exceptions correctly ? "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id"
: $"Multi-map error: splitOn column '{splitOnColumnName}' was not found - please ensure your splitOn parameter is set and in the correct order",
"splitOn");
} }
else else
{ {
......
...@@ -194,6 +194,13 @@ public void TestMultiMapperIsNotConfusedWithUnorderedCols() ...@@ -194,6 +194,13 @@ public void TestMultiMapperIsNotConfusedWithUnorderedCols()
Assert.Equal("a", result.Item2.Name); Assert.Equal("a", result.Item2.Name);
} }
[Fact]
public void TestMultiMapperSplitOnError()
{
var ex = Assert.Throws<ArgumentException>(() => connection.Query<Foo1, Bar1, Tuple<Foo1, Bar1>>("select 1 as Id, 2 as BarId", Tuple.Create, splitOn: "DoesntExist").First());
Assert.StartsWith("Multi-map error: splitOn column 'DoesntExist' was not found - please ensure your splitOn parameter is set and in the correct order", ex.Message);
}
[Fact] [Fact]
public void TestMultiMapDynamic() public void TestMultiMapDynamic()
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册