diff --git a/arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs b/arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs index cb83a01c..7fc20e1b 100644 --- a/arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs +++ b/arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs @@ -2,9 +2,14 @@ using ArangoDBNetStandard.CursorApi; using ArangoDBNetStandard.CursorApi.Models; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; +using System.Text; using System.Threading.Tasks; +using ArangoDBNetStandard.Serialization; +using ArangoDBNetStandard.Transport; +using Moq; using Xunit; namespace ArangoDBNetStandardTest.CursorApi @@ -159,6 +164,55 @@ public async Task PostCursorAsync_ShouldThrow_WhenAqlIsNotValid() Assert.Equal(1203, ex.ApiError.ErrorNum); } + [Fact] + public async Task PostCursorAsync_ShouldThrow_WhenErrorDeserializationFailed() + { + var mockTransport = new Mock(); + + var mockResponse = new Mock(); + + var mockResponseContent = new Mock(); + + string mockJsonError = "{ errorNum: \"some_error\" }"; + + mockResponseContent.Setup(x => x.ReadAsStreamAsync()) + .Returns(Task.FromResult( + new MemoryStream(Encoding.UTF8.GetBytes(mockJsonError)))); + + mockResponse.Setup(x => x.Content) + .Returns(mockResponseContent.Object); + + mockResponse.Setup(x => x.IsSuccessStatusCode) + .Returns(false); + + mockTransport.Setup(x => x.PostAsync( + It.IsAny(), + It.IsAny())) + .Returns(Task.FromResult(mockResponse.Object)); + + var cursorApi = new CursorApiClient(mockTransport.Object); + + var ex = await Assert.ThrowsAsync(async () => + { + await cursorApi.PostCursorAsync("RETURN true"); + }); + + Assert.NotNull(ex.Message); + Assert.NotNull(ex.InnerException); + } + + [Fact] + public async Task PostCursorAsync_ShouldThrowException_WhenResponseDeserializationFailed() + { + var ex = await Assert.ThrowsAsync(async () => + { + await _cursorApi.PostCursorAsync("RETURN null"); + }); + + Assert.NotNull(ex.Message); + Assert.NotNull(ex.InnerException); + } + [Fact] public async Task PutCursorAsync_ShouldSucceed() { diff --git a/arangodb-net-standard/ApiClientBase.cs b/arangodb-net-standard/ApiClientBase.cs index 7f1eb85d..746fa42c 100644 --- a/arangodb-net-standard/ApiClientBase.cs +++ b/arangodb-net-standard/ApiClientBase.cs @@ -28,8 +28,15 @@ public ApiClientBase(IApiClientSerialization serialization) protected async Task GetApiErrorException(IApiClientResponse response) { var stream = await response.Content.ReadAsStreamAsync(); - var error = _serialization.DeserializeFromStream(stream); - return new ApiErrorException(error); + try + { + var error = _serialization.DeserializeFromStream(stream); + return new ApiErrorException(error); + } + catch (Exception e) + { + throw new SerializationException($"An error occured while Deserializing an error response from Arango. See InnerException for more details.", e); + } } protected void ValidateDocumentId(string documentId) @@ -43,15 +50,29 @@ protected void ValidateDocumentId(string documentId) protected T DeserializeJsonFromStream(Stream stream) { - return _serialization.DeserializeFromStream(stream); + try + { + return _serialization.DeserializeFromStream(stream); + } + catch (Exception e) + { + throw new SerializationException($"An error occured while Deserializing the data response from Arango. See InnerException for more details.", e); + } } protected byte[] GetContent(T item, bool useCamelCasePropertyNames, bool ignoreNullValues) { - return _serialization.Serialize( - item, - useCamelCasePropertyNames, - ignoreNullValues); + try + { + return _serialization.Serialize( + item, + useCamelCasePropertyNames, + ignoreNullValues); + } + catch (Exception e) + { + throw new SerializationException($"A serialization error occured while preparing a request for Arango. See InnerException for more details.", e); + } } } } diff --git a/arangodb-net-standard/Serialization/SerializationException.cs b/arangodb-net-standard/Serialization/SerializationException.cs new file mode 100644 index 00000000..2ee5658d --- /dev/null +++ b/arangodb-net-standard/Serialization/SerializationException.cs @@ -0,0 +1,13 @@ +using System; +using System.Runtime.Serialization; + +namespace ArangoDBNetStandard.Serialization +{ + [Serializable] + public class SerializationException : Exception + { + public SerializationException(string message, Exception innerException) : base(message, innerException) + { + } + } +} \ No newline at end of file