Skip to content

Commit 094e642

Browse files
committed
Create and throw dedicated serialization exception. fix #274
1 parent f1fbee5 commit 094e642

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

arangodb-net-standard.Test/CursorApi/CursorApiClientTest.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
using ArangoDBNetStandard.CursorApi;
33
using ArangoDBNetStandard.CursorApi.Models;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Linq;
67
using System.Net;
8+
using System.Text;
79
using System.Threading.Tasks;
10+
using ArangoDBNetStandard.Serialization;
11+
using ArangoDBNetStandard.Transport;
12+
using Moq;
813
using Xunit;
914

1015
namespace ArangoDBNetStandardTest.CursorApi
@@ -159,6 +164,55 @@ public async Task PostCursorAsync_ShouldThrow_WhenAqlIsNotValid()
159164
Assert.Equal(1203, ex.ApiError.ErrorNum);
160165
}
161166

167+
[Fact]
168+
public async Task PostCursorAsync_ShouldThrow_WhenErrorDeserializationFailed()
169+
{
170+
var mockTransport = new Mock<IApiClientTransport>();
171+
172+
var mockResponse = new Mock<IApiClientResponse>();
173+
174+
var mockResponseContent = new Mock<IApiClientResponseContent>();
175+
176+
string mockJsonError = "{ errorNum: \"some_error\" }";
177+
178+
mockResponseContent.Setup(x => x.ReadAsStreamAsync())
179+
.Returns(Task.FromResult<Stream>(
180+
new MemoryStream(Encoding.UTF8.GetBytes(mockJsonError))));
181+
182+
mockResponse.Setup(x => x.Content)
183+
.Returns(mockResponseContent.Object);
184+
185+
mockResponse.Setup(x => x.IsSuccessStatusCode)
186+
.Returns(false);
187+
188+
mockTransport.Setup(x => x.PostAsync(
189+
It.IsAny<string>(),
190+
It.IsAny<byte[]>()))
191+
.Returns(Task.FromResult(mockResponse.Object));
192+
193+
var cursorApi = new CursorApiClient(mockTransport.Object);
194+
195+
var ex = await Assert.ThrowsAsync<SerializationException>(async () =>
196+
{
197+
await cursorApi.PostCursorAsync<object>("RETURN true");
198+
});
199+
200+
Assert.NotNull(ex.Message);
201+
Assert.NotNull(ex.InnerException);
202+
}
203+
204+
[Fact]
205+
public async Task PostCursorAsync_ShouldThrowException_WhenResponseDeserializationFailed()
206+
{
207+
var ex = await Assert.ThrowsAsync<SerializationException>(async () =>
208+
{
209+
await _cursorApi.PostCursorAsync<int>("RETURN null");
210+
});
211+
212+
Assert.NotNull(ex.Message);
213+
Assert.NotNull(ex.InnerException);
214+
}
215+
162216
[Fact]
163217
public async Task PutCursorAsync_ShouldSucceed()
164218
{

arangodb-net-standard/ApiClientBase.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ public ApiClientBase(IApiClientSerialization serialization)
2828
protected async Task<ApiErrorException> GetApiErrorException(IApiClientResponse response)
2929
{
3030
var stream = await response.Content.ReadAsStreamAsync();
31-
var error = _serialization.DeserializeFromStream<ApiErrorResponse>(stream);
32-
return new ApiErrorException(error);
31+
try
32+
{
33+
var error = _serialization.DeserializeFromStream<ApiErrorResponse>(stream);
34+
return new ApiErrorException(error);
35+
}
36+
catch (Exception e)
37+
{
38+
throw new SerializationException($"An error occured while Deserializing an error response from Arango. See InnerException for more details.", e);
39+
}
3340
}
3441

3542
protected void ValidateDocumentId(string documentId)
@@ -43,15 +50,29 @@ protected void ValidateDocumentId(string documentId)
4350

4451
protected T DeserializeJsonFromStream<T>(Stream stream)
4552
{
46-
return _serialization.DeserializeFromStream<T>(stream);
53+
try
54+
{
55+
return _serialization.DeserializeFromStream<T>(stream);
56+
}
57+
catch (Exception e)
58+
{
59+
throw new SerializationException($"An error occured while Deserializing the data response from Arango. See InnerException for more details.", e);
60+
}
4761
}
4862

4963
protected byte[] GetContent<T>(T item, bool useCamelCasePropertyNames, bool ignoreNullValues)
5064
{
51-
return _serialization.Serialize<T>(
52-
item,
53-
useCamelCasePropertyNames,
54-
ignoreNullValues);
65+
try
66+
{
67+
return _serialization.Serialize<T>(
68+
item,
69+
useCamelCasePropertyNames,
70+
ignoreNullValues);
71+
}
72+
catch (Exception e)
73+
{
74+
throw new SerializationException($"A serialization error occured while preparing a request for Arango. See InnerException for more details.", e);
75+
}
5576
}
5677
}
5778
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace ArangoDBNetStandard.Serialization
5+
{
6+
[Serializable]
7+
public class SerializationException : Exception
8+
{
9+
public SerializationException(string message, Exception innerException) : base(message, innerException)
10+
{
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)