mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-07 16:06:15 +00:00
Prevent potential duplicate reading of data on error
This commit is contained in:
parent
12fe94cbff
commit
5c51822996
@ -187,6 +187,7 @@ namespace CryptoExchange.Net
|
|||||||
protected async Task<CallResult<T>> DeserializeAsync<T>(Stream stream, JsonSerializer? serializer = null, int? requestId = null, long? elapsedMilliseconds = null)
|
protected async Task<CallResult<T>> DeserializeAsync<T>(Stream stream, JsonSerializer? serializer = null, int? requestId = null, long? elapsedMilliseconds = null)
|
||||||
{
|
{
|
||||||
serializer ??= defaultSerializer;
|
serializer ??= defaultSerializer;
|
||||||
|
string? data = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -197,7 +198,7 @@ namespace CryptoExchange.Net
|
|||||||
// in order to log/return the json data
|
// in order to log/return the json data
|
||||||
if (ClientOptions.OutputOriginalData || log.Level <= LogLevel.Debug)
|
if (ClientOptions.OutputOriginalData || log.Level <= LogLevel.Debug)
|
||||||
{
|
{
|
||||||
var data = await reader.ReadToEndAsync().ConfigureAwait(false);
|
data = await reader.ReadToEndAsync().ConfigureAwait(false);
|
||||||
log.Write(LogLevel.Debug, $"{(requestId != null ? $"[{requestId}] ": "")}Response received{(elapsedMilliseconds != null ? $" in {elapsedMilliseconds}" : " ")}ms: {data}");
|
log.Write(LogLevel.Debug, $"{(requestId != null ? $"[{requestId}] ": "")}Response received{(elapsedMilliseconds != null ? $" in {elapsedMilliseconds}" : " ")}ms: {data}");
|
||||||
var result = Deserialize<T>(data, serializer, requestId);
|
var result = Deserialize<T>(data, serializer, requestId);
|
||||||
if(ClientOptions.OutputOriginalData)
|
if(ClientOptions.OutputOriginalData)
|
||||||
@ -212,41 +213,48 @@ namespace CryptoExchange.Net
|
|||||||
}
|
}
|
||||||
catch (JsonReaderException jre)
|
catch (JsonReaderException jre)
|
||||||
{
|
{
|
||||||
string data;
|
if (data == null)
|
||||||
if (stream.CanSeek)
|
|
||||||
{
|
{
|
||||||
// If we can seek the stream rewind it so we can retrieve the original data that was sent
|
if (stream.CanSeek)
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
{
|
||||||
data = await ReadStreamAsync(stream).ConfigureAwait(false);
|
// If we can seek the stream rewind it so we can retrieve the original data that was sent
|
||||||
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
data = await ReadStreamAsync(stream).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
data = "[Data only available in Debug LogLevel]";
|
||||||
}
|
}
|
||||||
else
|
|
||||||
data = "[Data only available in Debug LogLevel]";
|
|
||||||
log.Write(LogLevel.Error, $"{(requestId != null ? $"[{requestId}] " : "")}Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}, data: {data}");
|
log.Write(LogLevel.Error, $"{(requestId != null ? $"[{requestId}] " : "")}Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}, data: {data}");
|
||||||
return new CallResult<T>(new DeserializeError($"Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}", data));
|
return new CallResult<T>(new DeserializeError($"Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}", data));
|
||||||
}
|
}
|
||||||
catch (JsonSerializationException jse)
|
catch (JsonSerializationException jse)
|
||||||
{
|
{
|
||||||
string data;
|
if (data == null)
|
||||||
if (stream.CanSeek)
|
|
||||||
{
|
{
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
if (stream.CanSeek)
|
||||||
data = await ReadStreamAsync(stream).ConfigureAwait(false);
|
{
|
||||||
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
data = await ReadStreamAsync(stream).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
data = "[Data only available in Debug LogLevel]";
|
||||||
}
|
}
|
||||||
else
|
|
||||||
data = "[Data only available in Debug LogLevel]";
|
|
||||||
|
|
||||||
log.Write(LogLevel.Error, $"{(requestId != null ? $"[{requestId}] " : "")}Deserialize JsonSerializationException: {jse.Message}, data: {data}");
|
log.Write(LogLevel.Error, $"{(requestId != null ? $"[{requestId}] " : "")}Deserialize JsonSerializationException: {jse.Message}, data: {data}");
|
||||||
return new CallResult<T>(new DeserializeError($"Deserialize JsonSerializationException: {jse.Message}", data));
|
return new CallResult<T>(new DeserializeError($"Deserialize JsonSerializationException: {jse.Message}", data));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
string data;
|
if (data == null)
|
||||||
if (stream.CanSeek) {
|
{
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
if (stream.CanSeek)
|
||||||
data = await ReadStreamAsync(stream).ConfigureAwait(false);
|
{
|
||||||
|
stream.Seek(0, SeekOrigin.Begin);
|
||||||
|
data = await ReadStreamAsync(stream).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
data = "[Data only available in Debug LogLevel]";
|
||||||
}
|
}
|
||||||
else
|
|
||||||
data = "[Data only available in Debug LogLevel]";
|
|
||||||
|
|
||||||
var exceptionInfo = ex.ToLogString();
|
var exceptionInfo = ex.ToLogString();
|
||||||
log.Write(LogLevel.Error, $"{(requestId != null ? $"[{requestId}] " : "")}Deserialize Unknown Exception: {exceptionInfo}, data: {data}");
|
log.Write(LogLevel.Error, $"{(requestId != null ? $"[{requestId}] " : "")}Deserialize Unknown Exception: {exceptionInfo}, data: {data}");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user