Browse Source

Added HttpException so we can actually catch server errors again!

tags/docs-0.9
Brandon Smith 9 years ago
parent
commit
7b24926310
7 changed files with 41 additions and 15 deletions
  1. +1
    -1
      src/Discord.Net.Commands/Discord.Net.Commands.xproj
  2. +3
    -0
      src/Discord.Net.Net45/Discord.Net.csproj
  3. +1
    -1
      src/Discord.Net/Discord.Net.xproj
  4. +6
    -6
      src/Discord.Net/DiscordClient.cs
  5. +2
    -2
      src/Discord.Net/Helpers/Http.cs
  6. +16
    -0
      src/Discord.Net/Helpers/HttpException.cs
  7. +12
    -5
      src/Discord.Net/project.json

+ 1
- 1
src/Discord.Net.Commands/Discord.Net.Commands.xproj View File

@@ -7,7 +7,7 @@
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>19793545-ef89-48f4-8100-3ebaad0a9141</ProjectGuid>
<RootNamespace>Discord.Net.Commands</RootNamespace>
<RootNamespace>Discord</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>


+ 3
- 0
src/Discord.Net.Net45/Discord.Net.csproj View File

@@ -88,6 +88,9 @@
<Compile Include="..\Discord.Net\Helpers\Http.cs">
<Link>Helpers\Http.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Helpers\HttpException.cs">
<Link>Helpers\HttpException.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Invite.cs">
<Link>Invite.cs</Link>
</Compile>


+ 1
- 1
src/Discord.Net/Discord.Net.xproj View File

@@ -7,7 +7,7 @@
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>acfb060b-ec8a-4926-b293-04c01e17ee23</ProjectGuid>
<RootNamespace>Discord.Net</RootNamespace>
<RootNamespace>Discord</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>


+ 6
- 6
src/Discord.Net/DiscordClient.cs View File

@@ -596,7 +596,7 @@ namespace Discord
{
await DiscordAPI.LeaveServer(serverId);
}
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) {}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) {}
return _servers.Remove(serverId);
}

@@ -626,7 +626,7 @@ namespace Discord
{
var response = await DiscordAPI.DestroyChannel(channelId);
}
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { }
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
return _channels.Remove(channelId);
}

@@ -655,7 +655,7 @@ namespace Discord
{
await DiscordAPI.Unban(serverId, userId);
}
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { }
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
}

//Invites
@@ -718,7 +718,7 @@ namespace Discord
var response = await DiscordAPI.GetInvite(id);
await DiscordAPI.DeleteInvite(response.Code);
}
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { }
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
}

//Chat
@@ -781,8 +781,8 @@ namespace Discord
await DiscordAPI.DeleteMessage(channelId, msgId);
return _messages.Remove(msgId);
}
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { }
catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.InternalServerError) { } //TODO: Remove me - temporary fix for deleting nonexisting messages
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.InternalServerError) { } //TODO: Remove me - temporary fix for deleting nonexisting messages
return null;
}



+ 2
- 2
src/Discord.Net/Helpers/Http.cs View File

@@ -134,14 +134,14 @@ namespace Discord.Helpers
{
response = await _client.SendAsync(msg, HttpCompletionOption.ResponseContentRead);
if (!response.IsSuccessStatusCode)
throw new InvalidOperationException($"The server responded with error {(int)response.StatusCode}.");
throw new HttpException(response.StatusCode);
result = await response.Content.ReadAsStringAsync();
}
else
{
response = await _client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new InvalidOperationException($"The server responded with error {(int)response.StatusCode}.");
throw new HttpException(response.StatusCode);
result = null;
}



+ 16
- 0
src/Discord.Net/Helpers/HttpException.cs View File

@@ -0,0 +1,16 @@
using System;
using System.Net;

namespace Discord.Helpers
{
public class HttpException : Exception
{
public HttpStatusCode StatusCode { get; }

public HttpException(HttpStatusCode statusCode)
: base($"The server responded with error {statusCode}")
{
StatusCode = statusCode;
}
}
}

+ 12
- 5
src/Discord.Net/project.json View File

@@ -14,13 +14,20 @@
},

"dependencies": {
"Newtonsoft.Json": "7.0.1",
"Microsoft.Net.Http": "2.2.22"
"Newtonsoft.Json": "7.0.1"
},

"frameworks": {
"net45": { },
"dnx451": { },
"net45": {
"dependencies": {
"Microsoft.Net.Http": "2.2.22"
}
},
"dnx451": {
"dependencies": {
"Microsoft.Net.Http": "2.2.22"
}
},
"dnxcore50": {
"dependencies": {
"System.Collections.Concurrent": "4.0.10",
@@ -33,4 +40,4 @@
}
}
}
}
}

Loading…
Cancel
Save