Skip to content

Commit

Permalink
Create and throw dedicated serialization exception. fix #274
Browse files Browse the repository at this point in the history
  • Loading branch information
robsiera committed Aug 11, 2020
1 parent f1fbee5 commit 094e642
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
54 changes: 54 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,55 @@ 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.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.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)
{
}
}
}

0 comments on commit 094e642

Please sign in to comment.