Skip to content

Commit

Permalink
Merge pull request #86 from koculu/85-enhancement-improve-validation-…
Browse files Browse the repository at this point in the history
…error-messages

Improve type mismatch validation errors.
  • Loading branch information
koculu authored Aug 30, 2024
2 parents 2f00997 + 05b53e5 commit 5407da7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
25 changes: 20 additions & 5 deletions src/ZoneTree/Core/ZoneTreeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,42 @@ void ValidateZoneTreeMeta()
Version.Parse(version),
ZoneTreeInfo.ProductVersion);

if (!string.Equals(ZoneTreeMeta.KeyType, typeof(TKey).SimplifiedFullName(), StringComparison.Ordinal))
if (!string.Equals(
ZoneTreeMeta.KeyType,
typeof(TKey).SimplifiedFullName(),
StringComparison.Ordinal))
throw new TreeKeyTypeMismatchException(
ZoneTreeMeta.KeyType,
typeof(TKey).SimplifiedFullName());

if (!string.Equals(ZoneTreeMeta.ValueType, typeof(TValue).SimplifiedFullName(), StringComparison.Ordinal))
if (!string.Equals(
ZoneTreeMeta.ValueType,
typeof(TValue).SimplifiedFullName(),
StringComparison.Ordinal))
throw new TreeValueTypeMismatchException(
ZoneTreeMeta.ValueType,
typeof(TValue).SimplifiedFullName());

if (!string.Equals(ZoneTreeMeta.ComparerType, Options.Comparer.GetType().SimplifiedFullName(), StringComparison.Ordinal))
if (!string.Equals(
ZoneTreeMeta.ComparerType,
Options.Comparer.GetType().SimplifiedFullName(),
StringComparison.Ordinal))
throw new TreeComparerMismatchException(
ZoneTreeMeta.ComparerType,
Options.Comparer.GetType().SimplifiedFullName());

if (!string.Equals(ZoneTreeMeta.KeySerializerType, Options.KeySerializer.GetType().SimplifiedFullName(), StringComparison.Ordinal))
if (!string.Equals(
ZoneTreeMeta.KeySerializerType,
Options.KeySerializer.GetType().SimplifiedFullName(),
StringComparison.Ordinal))
throw new TreeKeySerializerTypeMismatchException(
ZoneTreeMeta.KeySerializerType,
Options.KeySerializer.GetType().SimplifiedFullName());

if (!string.Equals(ZoneTreeMeta.ValueSerializerType, Options.ValueSerializer.GetType().SimplifiedFullName(), StringComparison.Ordinal))
if (!string.Equals(
ZoneTreeMeta.ValueSerializerType,
Options.ValueSerializer.GetType().SimplifiedFullName(),
StringComparison.Ordinal))
throw new TreeValueSerializerTypeMismatchException(
ZoneTreeMeta.ValueSerializerType,
Options.ValueSerializer.GetType().SimplifiedFullName());
Expand Down
4 changes: 3 additions & 1 deletion src/ZoneTree/Exceptions/TreeComparerMismatchException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public sealed class TreeComparerMismatchException : ZoneTreeException
{
public TreeComparerMismatchException(string expectedComparerType, string givenComparerType)
: base($"Tree comparer does not match.\r\n expected: {expectedComparerType}\r\n given: {givenComparerType}")
: base($"Tree comparer does not match.\r\nValue in metadata (JSON): {expectedComparerType}\r\nValue in Runtime: {givenComparerType}\r\n" +
"This could be due to a class rename. If the type mismatch is intentional (e.g., after a refactor), " +
"you may fix this error by manually editing the metadata JSON file.")
{
ExpectedComparerType = expectedComparerType;
GivenComparerType = givenComparerType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public sealed class TreeKeySerializerTypeMismatchException : ZoneTreeException
{
public TreeKeySerializerTypeMismatchException(string expectedType, string givenType)
: base($"Tree key serializer type does not match.\r\n expected: {expectedType}\r\n given: {givenType}")
: base($"Tree key serializer type does not match.\r\nValue in metadata (JSON): {expectedType}\r\nValue in Runtime: {givenType}\r\n" +
"This could be due to a class rename. If the type mismatch is intentional (e.g., after a refactor), " +
"you may fix this error by manually editing the metadata JSON file.")
{
ExpectedType = expectedType;
GivenType = givenType;
Expand Down
4 changes: 3 additions & 1 deletion src/ZoneTree/Exceptions/TreeKeyTypeMismatchException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public sealed class TreeKeyTypeMismatchException : ZoneTreeException
{
public TreeKeyTypeMismatchException(string expectedKeyType, string givenKeyType)
: base($"Tree key type does not match.\r\n expected: {expectedKeyType}\r\n given: {givenKeyType}")
: base($"Tree key type does not match.\r\nValue in metadata (JSON): {expectedKeyType}\r\nValue in Runtime: {givenKeyType}\r\n" +
"This could be due to a class rename. If the type mismatch is intentional (e.g., after a refactor), " +
"you may fix this error by manually editing the metadata JSON file.")
{
ExpectedKeyType = expectedKeyType;
GivenKeyType = givenKeyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
public sealed class TreeValueSerializerTypeMismatchException : ZoneTreeException
{
public TreeValueSerializerTypeMismatchException(string expectedType, string givenType)
: base($"Tree value serializer type does not match.\r\n expected: {expectedType}\r\n given: {givenType}")
: base($"Tree value serializer type does not match.\r\nValue in metadata (JSON): {expectedType}\r\nValue in Runtime: {givenType}\r\n" +
"This could be due to a class rename. If the type mismatch is intentional (e.g., after a refactor), " +
"you may fix this error by manually editing the metadata JSON file.")
{
ExpectedType = expectedType;
GivenType = givenType;
}

public string ExpectedType { get; }
public string GivenType { get; }
}
}
4 changes: 3 additions & 1 deletion src/ZoneTree/Exceptions/TreeValueTypeMismatchException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
public sealed class TreeValueTypeMismatchException : ZoneTreeException
{
public TreeValueTypeMismatchException(string expectedValueType, string givenValueType)
: base($"Tree value type does not match.\r\n expected: {expectedValueType}\r\n given: {givenValueType}")
: base($"Tree value type does not match.\r\nValue in metadata (JSON): {expectedValueType}\r\nValue in Runtime: {givenValueType}\r\n" +
"This could be due to a class rename. If the type mismatch is intentional (e.g., after a refactor), " +
"you may fix this error by manually editing the metadata JSON file.")
{
ExpectedValueType = expectedValueType;
GivenValueType = givenValueType;
Expand Down

0 comments on commit 5407da7

Please sign in to comment.