Skip to content

Commit

Permalink
Merge pull request #25 from aksio-insurtech:fix/stuff
Browse files Browse the repository at this point in the history
Adding custom exception
  • Loading branch information
einari authored Dec 11, 2023
2 parents 0fab230 + 21a78b6 commit 3e59dc8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Source/TypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public override Type Deserialize(BsonDeserializationContext context, BsonDeseria
switch (bsonType)
{
case BsonType.String:
return Type.GetType(context.Reader.ReadString()) ?? throw new InvalidOperationException("Could not deserialize type.");

default:
throw new NotSupportedException($"Cannot deserialize a {bsonType} to a {nameof(Type)}.");
var type = context.Reader.ReadString();
return Type.GetType(type) ?? throw new UnknownType(type);
}

throw new UnknownType(bsonType.ToString());
}

/// <inheritdoc/>
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Type value)
{
context.Writer.WriteString(value.GetTypeString());
}
}
}
18 changes: 18 additions & 0 deletions Source/UnknownType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Aksio.MongoDB;

/// <summary>
/// Exception that gets thrown when an unknown type is encountered.
/// </summary>
public class UnknownType : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="UnknownType"/> class.
/// </summary>
/// <param name="type">String representation of the unknown type.</param>
public UnknownType(string type) : base($"Unknown type: {type}")
{
}
}

0 comments on commit 3e59dc8

Please sign in to comment.