mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-22 12:05:51 -04:00
Fix Discord bot not handling http error responses properly when running on modern dotnet versions
This commit is contained in:
parent
82ac48db61
commit
3445209af9
@ -70,40 +70,10 @@ namespace MCGalaxy.Modules.Relay.Discord
|
|||||||
msg.ProcessResponse(resp);
|
msg.ProcessResponse(resp);
|
||||||
break;
|
break;
|
||||||
} catch (WebException ex) {
|
} catch (WebException ex) {
|
||||||
string err = HttpUtil.GetErrorResponse(ex);
|
bool canRetry = HandleErrorResponse(ex, msg, retry);
|
||||||
HttpUtil.DisposeErrorResponse(ex);
|
HttpUtil.DisposeErrorResponse(ex);
|
||||||
HttpStatusCode status = GetStatus(ex);
|
|
||||||
|
|
||||||
// 429 errors simply require retrying after sleeping for a bit
|
if (!canRetry) return;
|
||||||
if (status == (HttpStatusCode)429) {
|
|
||||||
SleepForRetryPeriod(ex.Response);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 500 errors might be temporary Discord outage, so still retry a few times
|
|
||||||
if (status >= (HttpStatusCode)500 && status <= (HttpStatusCode)504) {
|
|
||||||
LogWarning(ex);
|
|
||||||
LogResponse(err);
|
|
||||||
if (retry >= 2) return;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If unable to reach Discord at all, immediately give up
|
|
||||||
if (ex.Status == WebExceptionStatus.NameResolutionFailure) {
|
|
||||||
LogWarning(ex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// May be caused by connection dropout/reset, so still retry a few times
|
|
||||||
if (ex.InnerException is IOException) {
|
|
||||||
LogWarning(ex);
|
|
||||||
if (retry >= 2) return;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
LogError(ex, msg);
|
|
||||||
LogResponse(err);
|
|
||||||
return;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
LogError(ex, msg);
|
LogError(ex, msg);
|
||||||
return;
|
return;
|
||||||
@ -115,6 +85,40 @@ namespace MCGalaxy.Modules.Relay.Discord
|
|||||||
if (remaining == "1") SleepForRetryPeriod(res);
|
if (remaining == "1") SleepForRetryPeriod(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool HandleErrorResponse(WebException ex, DiscordApiMessage msg, int retry) {
|
||||||
|
string err = HttpUtil.GetErrorResponse(ex);
|
||||||
|
HttpStatusCode status = GetStatus(ex);
|
||||||
|
|
||||||
|
// 429 errors simply require retrying after sleeping for a bit
|
||||||
|
if (status == (HttpStatusCode)429) {
|
||||||
|
SleepForRetryPeriod(ex.Response);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 500 errors might be temporary Discord outage, so still retry a few times
|
||||||
|
if (status >= (HttpStatusCode)500 && status <= (HttpStatusCode)504) {
|
||||||
|
LogWarning(ex);
|
||||||
|
LogResponse(err);
|
||||||
|
return retry < 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If unable to reach Discord at all, immediately give up
|
||||||
|
if (ex.Status == WebExceptionStatus.NameResolutionFailure) {
|
||||||
|
LogWarning(ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// May be caused by connection dropout/reset, so still retry a few times
|
||||||
|
if (ex.InnerException is IOException) {
|
||||||
|
LogWarning(ex);
|
||||||
|
return retry < 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogError(ex, msg);
|
||||||
|
LogResponse(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static HttpStatusCode GetStatus(WebException ex) {
|
static HttpStatusCode GetStatus(WebException ex) {
|
||||||
if (ex.Response == null) return 0;
|
if (ex.Response == null) return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user