Skip to content

Commit fca49eb

Browse files
author
Nick Craver
committed
SplitOn: improve error message
Fixes #866. This provides a more informative error, saying _which_ splitOn column couldn't be found in the result set.
1 parent ffc02eb commit fca49eb

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Dapper/SqlMapper.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ private static int GetNextSplitDynamic(int startIdx, string splitOn, IDataReader
16791679
{
16801680
if (startIdx == reader.FieldCount)
16811681
{
1682-
throw MultiMapException(reader);
1682+
throw MultiMapException(reader, splitOn);
16831683
}
16841684

16851685
if (splitOn == "*")
@@ -1713,7 +1713,7 @@ private static int GetNextSplit(int startIdx, string splitOn, IDataReader reader
17131713
}
17141714
}
17151715

1716-
throw MultiMapException(reader);
1716+
throw MultiMapException(reader, splitOn);
17171717
}
17181718

17191719
private static CacheInfo GetCacheInfo(Identity identity, object exampleParameters, bool addToCache)
@@ -1831,16 +1831,18 @@ private static Func<IDataReader, object> GetHandlerDeserializer(ITypeHandler han
18311831
return reader => handler.Parse(type, reader.GetValue(startBound));
18321832
}
18331833

1834-
private static Exception MultiMapException(IDataRecord reader)
1834+
private static Exception MultiMapException(IDataRecord reader, string splitOnColumnName = null)
18351835
{
18361836
bool hasFields = false;
18371837
try { hasFields = reader != null && reader.FieldCount != 0; }
18381838
catch { /* don't throw when trying to throw */ }
18391839
if (hasFields)
18401840
{
1841-
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
1842-
return new ArgumentException("When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn");
1843-
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
1841+
return new ArgumentException(
1842+
string.IsNullOrEmpty(splitOnColumnName)
1843+
? "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id"
1844+
: $"Multi-map error: splitOn column '{splitOnColumnName}' was not found - please ensure your splitOn parameter is set and in the correct order",
1845+
"splitOn");
18441846
}
18451847
else
18461848
{

tests/Dapper.Tests/MultiMapTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ public void TestMultiMapperIsNotConfusedWithUnorderedCols()
194194
Assert.Equal("a", result.Item2.Name);
195195
}
196196

197+
[Fact]
198+
public void TestMultiMapperSplitOnError()
199+
{
200+
var ex = Assert.Throws<ArgumentException>(() => connection.Query<Foo1, Bar1, Tuple<Foo1, Bar1>>("select 1 as Id, 2 as BarId", Tuple.Create, splitOn: "DoesntExist").First());
201+
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);
202+
}
203+
197204
[Fact]
198205
public void TestMultiMapDynamic()
199206
{

0 commit comments

Comments
 (0)