sharee.bike-App/TINKLib/Repository/Exception/WebExceptionHelper.cs

54 lines
2.1 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System.Net;
2021-06-26 20:57:55 +02:00
namespace TINK.Repository.Exception
2021-05-13 20:03:07 +02:00
{
public static class WebExceptionHelper
{
/// <summary> Gets if a exception is caused by an error connecting to copri (LAN or mobile data off/ not reachable, proxy, ...).</summary>
2021-06-26 20:57:55 +02:00
/// <param name="exception">Expection to check.</param>
2021-05-13 20:03:07 +02:00
/// <returns>True if exception if caused by an connection error. </returns>
2021-06-26 20:57:55 +02:00
public static bool GetIsConnectFailureException(this System.Exception exception)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
#if !USCSHARP9
if (!(exception is WebException webException))
#else
if (exception is not WebException webException)
#endif
2021-05-13 20:03:07 +02:00
{
return false;
}
2021-06-26 20:57:55 +02:00
return webException.Status == WebExceptionStatus.ConnectFailure // Happens if WLAN and mobile data is off/ Router denies internet access/ ...
|| webException.Status == WebExceptionStatus.NameResolutionFailure // Happens sometimes when not WLAN and no mobil connection are available (bad connection in lift).
|| webException.Status == WebExceptionStatus.ReceiveFailure; // Happened when modile was connected to WLAN
2021-05-13 20:03:07 +02:00
}
/// <summary> Gets if a exception is caused by clicking too fast.</summary>
2021-06-26 20:57:55 +02:00
/// <param name="exception">Expection to check.</param>
2021-05-13 20:03:07 +02:00
/// <returns>True if exception if caused by a fast click sequence. </returns>
2021-06-26 20:57:55 +02:00
public static bool GetIsForbiddenException(this System.Exception exception)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
#if !USCSHARP9
if (!(exception is WebException webException))
#else
if (exception is not WebException webException)
#endif
2021-05-13 20:03:07 +02:00
{
return false;
}
2021-06-26 20:57:55 +02:00
#if !USCSHARP9
if (!(webException?.Response is HttpWebResponse response))
#else
if (webException?.Response is not HttpWebResponse response)
#endif
2021-05-13 20:03:07 +02:00
{
return false;
}
2021-06-26 20:57:55 +02:00
return webException.Status == WebExceptionStatus.ProtocolError
&& response.StatusCode == HttpStatusCode.Forbidden;
2021-05-13 20:03:07 +02:00
}
}
}