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
{
2022-09-06 16:08:19 +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>
/// <param name="exception">Expection to check.</param>
/// <returns>True if exception if caused by an connection error. </returns>
public static bool GetIsConnectFailureException ( this System . Exception exception )
{
2021-06-26 20:57:55 +02:00
#if ! USCSHARP9
2022-09-06 16:08:19 +02:00
if ( ! ( exception is WebException webException ) )
2021-06-26 20:57:55 +02:00
#else
if ( exception is not WebException webException )
#endif
2022-09-06 16:08:19 +02:00
{
return false ;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +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 mobile was connected to WLAN
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets if a exception is caused by clicking too fast.</summary>
/// <param name="exception">Expection to check.</param>
/// <returns>True if exception if caused by a fast click sequence. </returns>
public static bool GetIsForbiddenException ( this System . Exception exception )
{
2021-06-26 20:57:55 +02:00
#if ! USCSHARP9
2022-09-06 16:08:19 +02:00
if ( ! ( exception is WebException webException ) )
2021-06-26 20:57:55 +02:00
#else
if ( exception is not WebException webException )
#endif
2022-09-06 16:08:19 +02:00
{
return false ;
}
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
#if ! USCSHARP9
2022-09-06 16:08:19 +02:00
if ( ! ( webException ? . Response is HttpWebResponse response ) )
2021-06-26 20:57:55 +02:00
#else
if ( webException ? . Response is not HttpWebResponse response )
#endif
2022-09-06 16:08:19 +02:00
{
return false ;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return webException . Status = = WebExceptionStatus . ProtocolError
& & response . StatusCode = = HttpStatusCode . Forbidden ;
}
}
2021-05-13 20:03:07 +02:00
}