Skip to content

Commit 3c9fee5

Browse files
committed
Added additional error handling for parse errors
1 parent bac613f commit 3c9fee5

6 files changed

Lines changed: 58 additions & 16 deletions

File tree

Deployment/Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Version 2.0.0.0
33
- Added AwaitableQueryDispatcher
44
- Dropped support for Silverligt and Windows Phone 7
55
- Changed minimum .Net Framework Version to 4.5
6+
- Added more detailed exception in cases when there is a response parsing error
67

78
Version 1.2.2.0
89
==========================================

Deployment/Deploy.target

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PropertyGroup>
66
<RootDirectory>..</RootDirectory>
77
<Config>Release</Config>
8-
<ReleaseVersion>1.2.2.0</ReleaseVersion>
8+
<ReleaseVersion>2.0.0.0</ReleaseVersion>
99
<ReleaseVersionEscaped>$(ReleaseVersion.Replace(".", "_"))</ReleaseVersionEscaped>
1010
<ReleaseName>TS3QueryLib.Net.V$(ReleaseVersionEscaped)</ReleaseName>
1111
<ReleaseDirectory>Releases\$(ReleaseVersionEscaped)</ReleaseDirectory>

TS3QueryLib.Core.Framework/AwaitableQueryDispatcher.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Net.Sockets;
7+
using System.Runtime.CompilerServices;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using TS3QueryLib.Core.Common;
@@ -146,8 +147,14 @@ public async Task<string> SendAsync(string messageToSend)
146147

147148
protected static async Task SendAsync(StreamWriter writer, string messageToSend)
148149
{
149-
await writer.WriteLineAsync(messageToSend).ConfigureAwait(false);
150-
await writer.FlushAsync().ConfigureAwait(false);
150+
ConfiguredTaskAwaitable? writeLineAwaitable = writer?.WriteLineAsync(messageToSend).ConfigureAwait(false);
151+
152+
if (writeLineAwaitable.HasValue)
153+
await writeLineAwaitable.Value;
154+
155+
ConfiguredTaskAwaitable? flushAwaitable = writer?.FlushAsync().ConfigureAwait(false);
156+
if (flushAwaitable.HasValue)
157+
await flushAwaitable.Value;
151158
}
152159

153160
public void Disconnect()
@@ -197,7 +204,7 @@ protected async void ReadLoop()
197204
ReceivedLines.Clear();
198205
}
199206

200-
string responseText = string.Join("\r\n", ReceivedLines.Concat(new[] { message }));
207+
string responseText = string.Join("\n\r", ReceivedLines.Concat(new[] { message }));
201208
MessageResponses.Enqueue(responseText);
202209
ReceivedLines.Clear();
203210

@@ -231,7 +238,8 @@ protected async void ReadLoop()
231238

232239
protected async Task<string> ReadLineAsync(bool throwOnEmptyMessage = true)
233240
{
234-
string message = await ClientReader.ReadLineAsync().ConfigureAwait(false);
241+
ConfiguredTaskAwaitable<string>? readLineAwaitable = ClientReader?.ReadLineAsync().ConfigureAwait(false);
242+
string message = readLineAwaitable.HasValue ? await readLineAwaitable.Value : null;
235243

236244
if (message != null)
237245
return message;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace TS3QueryLib.Core.Common.Exceptions
5+
{
6+
public class ParseException : Exception
7+
{
8+
public ParseException()
9+
{
10+
}
11+
12+
public ParseException(string message) : base(message)
13+
{
14+
}
15+
16+
public ParseException(string message, Exception innerException) : base(message, innerException)
17+
{
18+
}
19+
20+
protected ParseException(SerializationInfo info, StreamingContext context) : base(info, context)
21+
{
22+
}
23+
}
24+
}

TS3QueryLib.Core.Framework/Common/Responses/ResponseBase.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using TS3QueryLib.Core.CommandHandling;
3+
using TS3QueryLib.Core.Common.Exceptions;
34

45
namespace TS3QueryLib.Core.Common.Responses
56
{
@@ -23,21 +24,28 @@ public abstract class ResponseBase<T> : IDump, IResponse where T : ResponseBase<
2324

2425
public static T Parse(string response, params object[] additionalStates)
2526
{
26-
T instance = (T) Activator.CreateInstance(typeof(T));
27-
instance.ResponseText = response;
28-
29-
if (response != null)
27+
try
3028
{
31-
string body, statusLine;
32-
SplitResponse(response, out body, out statusLine);
33-
instance.BodyText = body;
34-
instance.StatusText = statusLine;
29+
T instance = (T)Activator.CreateInstance(typeof(T));
30+
instance.ResponseText = response;
31+
32+
if (response != null)
33+
{
34+
SplitResponse(response, out var body, out var statusLine);
35+
instance.BodyText = body;
36+
instance.StatusText = statusLine;
37+
38+
instance.DetermineErrorDetails(statusLine);
39+
instance.FillFrom(response, additionalStates);
40+
}
3541

36-
instance.DetermineErrorDetails(statusLine);
37-
instance.FillFrom(response, additionalStates);
42+
return instance;
43+
}
44+
catch (Exception e)
45+
{
46+
throw new ParseException($"Error while trying to parse the response.\n\nRaw-Response:"+response, e);
3847
}
3948

40-
return instance;
4149
}
4250

4351
#endregion

TS3QueryLib.Core.Framework/TS3QueryLib.Core.Framework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<Compile Include="CommandHandling\Command.cs" />
8484
<Compile Include="Common\Entities\ChannelListEntryBase.cs" />
8585
<Compile Include="Common\Entities\ClientModificationBase.cs" />
86+
<Compile Include="Common\Exceptions\ParseException.cs" />
8687
<Compile Include="Common\Notification\NotificationsBase.cs" />
8788
<Compile Include="Common\SharedCommandName.cs" />
8889
<Compile Include="Server\CommandName.cs" />

0 commit comments

Comments
 (0)