-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored exception handling classes
- Loading branch information
Showing
32 changed files
with
422 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using Fauna.Exceptions; | ||
using NUnit.Framework; | ||
|
||
namespace Fauna.Tests; | ||
|
||
[TestFixture] | ||
public class AbortExceptionTests | ||
{ | ||
private class TestClass | ||
{ | ||
public string Name { get; set; } | ||
public int Age { get; set; } | ||
|
||
} | ||
|
||
[Test] | ||
public void Ctor_InitializesPropertiesCorrectly() | ||
{ | ||
var expectedQueryFailure = CreateQueryFailure(@"{{\""@int\"":\""123\""}}"); | ||
var expectedMessage = "Test message"; | ||
var expectedInnerException = new Exception("Inner exception"); | ||
|
||
var actual1 = new AbortException(expectedQueryFailure, expectedMessage); | ||
var actual2 = new AbortException(expectedQueryFailure, expectedMessage, expectedInnerException); | ||
|
||
Assert.AreEqual(expectedQueryFailure.ErrorInfo.Abort, actual1.QueryFailure.ErrorInfo.Abort); | ||
Assert.AreEqual(expectedMessage, actual1.Message); | ||
|
||
Assert.AreEqual(expectedQueryFailure.ErrorInfo.Abort, actual2.QueryFailure.ErrorInfo.Abort); | ||
Assert.AreEqual(expectedMessage, actual2.Message); | ||
Assert.AreEqual(expectedInnerException, actual2.InnerException); | ||
} | ||
|
||
[Test] | ||
public void GetData_WithIntData_ReturnsDeserializedObject() | ||
{ | ||
var expected = 123; | ||
var queryFailure = CreateQueryFailure(@$"{{\""@int\"":\""{expected}\""}}"); | ||
var exception = new AbortException(queryFailure, "Test message"); | ||
|
||
var actual = exception.GetData(); | ||
|
||
Assert.IsNotNull(actual); | ||
Assert.IsInstanceOf<int>(actual); | ||
Assert.AreEqual(expected, (int)actual!); | ||
} | ||
|
||
|
||
[Test] | ||
public void GetDataT_WithIntData_ReturnsDeserializedTypedObject() | ||
{ | ||
var expected = 123; | ||
var queryFailure = CreateQueryFailure(@$"{{\""@int\"":\""{expected}\""}}"); | ||
var exception = new AbortException(queryFailure, "Test message"); | ||
|
||
var actual = exception.GetData<int>(); | ||
|
||
Assert.IsNotNull(actual); | ||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
[Test] | ||
public void GetData_WithPocoData_ReturnsDeserializedObject() | ||
{ | ||
var expected = new TestClass { Name = "John", Age = 105 }; | ||
var queryFailure = CreateQueryFailure(@$"{{\""@object\"":{{\""Name\"":\""{expected.Name}\"",\""Age\"":{{\""@int\"":\""{expected.Age}\""}}}}}}"); | ||
var exception = new AbortException(queryFailure, "Test message"); | ||
|
||
var actual = exception.GetData() as IDictionary<string, object>; | ||
|
||
Assert.IsNotNull(actual); | ||
Assert.AreEqual(expected.Name, actual["Name"]); | ||
Check warning on line 72 in Fauna.Test/Exceptions/AbortException.Tests.cs GitHub Actions / dotnet-test (6.0.x)
|
||
Assert.AreEqual(expected.Age, actual["Age"]); | ||
} | ||
|
||
[Test] | ||
public void GetDataT_WithPocoData_ReturnsDeserializedTypedObject() | ||
{ | ||
var expected = new TestClass { Name = "John", Age = 105 }; | ||
var queryFailure = CreateQueryFailure(@$"{{\""@object\"":{{\""Name\"":\""{expected.Name}\"",\""Age\"":{{\""@int\"":\""{expected.Age}\""}}}}}}"); | ||
var exception = new AbortException(queryFailure, "Test message"); | ||
|
||
var actual = exception.GetData<TestClass>(); | ||
|
||
Assert.IsNotNull(actual); | ||
Assert.AreEqual(expected.Name, actual.Name); | ||
Check warning on line 86 in Fauna.Test/Exceptions/AbortException.Tests.cs GitHub Actions / dotnet-test (6.0.x)
|
||
Assert.AreEqual(expected.Age, actual.Age); | ||
} | ||
|
||
[Test] | ||
public void GetData_WithNullAbortData_ReturnsNull() | ||
{ | ||
var queryFailure = CreateQueryFailure(null); | ||
var exception = new AbortException(queryFailure, "Test message"); | ||
|
||
var result = exception.GetData(); | ||
|
||
Assert.IsNull(result); | ||
} | ||
|
||
[Test] | ||
public void GetData_CachesDataCorrectly() | ||
{ | ||
var mockData = new TestClass { Name = "John", Age = 105 }; | ||
var queryFailure = CreateQueryFailure(@$"{{\""@object\"":{{\""Name\"":\""{mockData.Name}\"",\""Age\"":{{\""@int\"":\""{mockData.Age}\""}}}}}}"); | ||
var exception = new AbortException(queryFailure, "Test message"); | ||
|
||
var callResult1 = exception.GetData<TestClass>(); | ||
var typedCallResult1 = exception.GetData(); | ||
var callResult2 = exception.GetData<TestClass>(); | ||
var typedCallResult2 = exception.GetData(); | ||
|
||
Assert.AreSame(callResult1, callResult2); | ||
Assert.AreSame(typedCallResult1, typedCallResult2); | ||
Assert.AreNotSame(callResult1, typedCallResult1); | ||
Assert.AreNotSame(callResult2, typedCallResult2); | ||
} | ||
|
||
private QueryFailure CreateQueryFailure(string abortData) | ||
{ | ||
var rawResponseText = $@"{{ | ||
""error"": {{ | ||
""code"": ""abort"", | ||
""message"": ""Query aborted."", | ||
""abort"": ""{abortData}"" | ||
}}, | ||
""summary"": ""error: Query aborted."", | ||
""txn_ts"": 1702346199930000, | ||
""stats"": {{ | ||
""compute_ops"": 1, | ||
""read_ops"": 0, | ||
""write_ops"": 0, | ||
""query_time_ms"": 105, | ||
""contention_retries"": 0, | ||
""storage_bytes_read"": 261, | ||
""storage_bytes_write"": 0, | ||
""rate_limits_hit"": [] | ||
}}, | ||
""schema_version"": 0 | ||
}}"; | ||
return new QueryFailure(rawResponseText); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Fauna.Serialization; | ||
|
||
namespace Fauna.Exceptions; | ||
|
||
/// <summary> | ||
/// Represents an exception that occurs when the FQL `abort` function is called. | ||
/// This exception captures the data provided during the abort operation. | ||
/// </summary> | ||
public class AbortException : QueryRuntimeException | ||
{ | ||
private readonly Dictionary<Type, object?> cache = new(); | ||
private static readonly Type NonTypedKey = typeof(object); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AbortException"/> class with a specified error message and query failure details. | ||
/// </summary> | ||
/// <param name="queryFailure">The <see cref="QueryFailure"/> object containing details about the query failure.</param> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
public AbortException(QueryFailure queryFailure, string message) | ||
: base(queryFailure, message) { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AbortException"/> class with a specified error message, a reference to the inner exception, and query failure details. | ||
/// </summary> | ||
/// <param name="queryFailure">The <see cref="QueryFailure"/> object containing details about the query failure.</param> | ||
/// <param name="message">The error message that explains the reason for the exception.</param> | ||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param> | ||
public AbortException(QueryFailure queryFailure, string message, Exception innerException) | ||
: base(queryFailure, message, innerException) { } | ||
|
||
/// <summary> | ||
/// Retrieves the deserialized data associated with the abort operation as an object. | ||
/// </summary> | ||
/// <returns>The deserialized data as an object, or null if no data is available.</returns> | ||
public object? GetData() | ||
{ | ||
if (!cache.TryGetValue(NonTypedKey, out var cachedData)) | ||
{ | ||
var abortDataString = QueryFailure.ErrorInfo.Abort.ToString(); | ||
if (!string.IsNullOrEmpty(abortDataString)) | ||
{ | ||
cachedData = Serializer.Deserialize(abortDataString); | ||
cache[NonTypedKey] = cachedData; | ||
} | ||
} | ||
return cachedData; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the deserialized data associated with the abort operation as a specific type. | ||
/// </summary> | ||
/// <typeparam name="T">The type to which the data should be deserialized.</typeparam> | ||
/// <returns>The deserialized data as the specified type, or null if no data is available.</returns> | ||
public T? GetData<T>() | ||
{ | ||
var typeKey = typeof(T); | ||
if (!cache.TryGetValue(typeKey, out var cachedData)) | ||
{ | ||
var abortDataString = QueryFailure.ErrorInfo.Abort.ToString(); | ||
if (!string.IsNullOrEmpty(abortDataString)) | ||
{ | ||
T? deserializedResult = Serializer.Deserialize<T>(abortDataString); | ||
cache[typeKey] = deserializedResult; | ||
return deserializedResult; | ||
} | ||
} | ||
return (T?)cachedData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Fauna.Exceptions; | ||
|
||
public class AuthenticationException : ServiceException | ||
{ | ||
public AuthenticationException(QueryFailure queryFailure, string message) | ||
: base(queryFailure, message) { } | ||
|
||
public AuthenticationException(QueryFailure queryFailure, string message, Exception innerException) | ||
: base(queryFailure, message, innerException) { } | ||
} |
Oops, something went wrong.