Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and throw dedicated serialization exception. #275

Merged
merged 1 commit into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -159,6 +164,57 @@ public async Task PostCursorAsync_ShouldThrow_WhenAqlIsNotValid()
Assert.Equal(1203, ex.ApiError.ErrorNum);
}

[Fact]
public async Task PostCursorAsync_ShouldThrow_WhenErrorDeserializationFailed()
{
var mockTransport = new Mock<IApiClientTransport>();

var mockResponse = new Mock<IApiClientResponse>();

var mockResponseContent = new Mock<IApiClientResponseContent>();

string mockJsonError = "{ errorNum: \"some_error\" }";

mockResponseContent.Setup(x => x.ReadAsStreamAsync())
.Returns(Task.FromResult<Stream>(
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<string>(),
It.IsAny<byte[]>()))
.Returns(Task.FromResult(mockResponse.Object));

var cursorApi = new CursorApiClient(mockTransport.Object);

var ex = await Assert.ThrowsAsync<SerializationException>(async () =>
{
await cursorApi.PostCursorAsync<object>("RETURN true");
});

Assert.NotNull(ex.Message);
Assert.Contains("while Deserializing an error response", ex.Message);
Assert.NotNull(ex.InnerException);
}

[Fact]
public async Task PostCursorAsync_ShouldThrowException_WhenResponseDeserializationFailed()
{
var ex = await Assert.ThrowsAsync<SerializationException>(async () =>
{
await _cursorApi.PostCursorAsync<int>("RETURN null");
});

Assert.NotNull(ex.Message);
Assert.Contains("while Deserializing the data response", ex.Message);
Assert.NotNull(ex.InnerException);
}

[Fact]
public async Task PutCursorAsync_ShouldSucceed()
{
Expand Down
35 changes: 28 additions & 7 deletions arangodb-net-standard/ApiClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ public ApiClientBase(IApiClientSerialization serialization)
protected async Task<ApiErrorException> GetApiErrorException(IApiClientResponse response)
{
var stream = await response.Content.ReadAsStreamAsync();
var error = _serialization.DeserializeFromStream<ApiErrorResponse>(stream);
return new ApiErrorException(error);
try
{
var error = _serialization.DeserializeFromStream<ApiErrorResponse>(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)
Expand All @@ -43,15 +50,29 @@ protected void ValidateDocumentId(string documentId)

protected T DeserializeJsonFromStream<T>(Stream stream)
{
return _serialization.DeserializeFromStream<T>(stream);
try
{
return _serialization.DeserializeFromStream<T>(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>(T item, bool useCamelCasePropertyNames, bool ignoreNullValues)
{
return _serialization.Serialize<T>(
item,
useCamelCasePropertyNames,
ignoreNullValues);
try
{
return _serialization.Serialize<T>(
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);
}
}
}
}
13 changes: 13 additions & 0 deletions arangodb-net-standard/Serialization/SerializationException.cs
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
}