Skip to content

Commit 80d2f23

Browse files
committed
Removing mapper in case of an exception while building the mapper
1 parent 61ddcf3 commit 80d2f23

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

LiteDB/Client/Mapper/BsonMapper.GetEntityMapper.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,24 @@ internal EntityMapper GetEntityMapper(Type type)
2828
mapper = new EntityMapper(type, cts.Token);
2929
if (_entities.TryAdd(type, mapper))
3030
{
31-
this.BuildEntityMapper(mapper);
31+
try
32+
{
33+
this.BuildEntityMapper(mapper);
34+
}
35+
catch (Exception e)
36+
{
37+
if (_entities.TryRemove(type, out var existingMapper))
38+
{
39+
var isSameMapper = ReferenceEquals(mapper, existingMapper);
40+
if (!isSameMapper)
41+
{
42+
// Should never happen, but if it does, we need to put the existing mapper back in the cache
43+
_entities.TryAdd(type, existingMapper);
44+
}
45+
}
46+
47+
throw new LiteException(LiteException.MAPPING_ERROR, $"Error in '{type.Name}' mapping: {e.Message}", e);
48+
}
3249
}
3350
else
3451
{

LiteDB/Utils/LiteException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class LiteException : Exception
5959
public const int ILLEGAL_DESERIALIZATION_TYPE = 218;
6060
public const int ENTITY_INITIALIZATION_FAILED = 219;
6161
public const int MAPPER_NOT_FOUND = 220;
62+
public const int MAPPING_ERROR = 221;
63+
6264

6365
public const int INVALID_DATAFILE_STATE = 999;
6466

0 commit comments

Comments
 (0)