HttpErrorCodes
using Relativity.Transfer.Http.Resources;
using System;
using System.Net;
namespace Relativity.Transfer.Http
{
internal static class HttpErrorCodes
{
public static string GetErrorMessage(Exception e)
{
if (e == null)
throw new ArgumentNullException("e");
string message = e.Message;
WebException ex = e as WebException;
if (ex != null) {
switch (ex.Status) {
case WebExceptionStatus.ConnectFailure:
return HttpStrings.ConnectionError;
case WebExceptionStatus.ConnectionClosed:
return HttpStrings.ConnectionLostError;
case WebExceptionStatus.NameResolutionFailure:
return HttpStrings.DnsError;
case WebExceptionStatus.Timeout:
return HttpStrings.ConnectionTimeoutError;
case WebExceptionStatus.ProtocolError: {
HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse;
if (httpWebResponse != null) {
switch (httpWebResponse.StatusCode) {
case HttpStatusCode.Forbidden:
return HttpStrings.ForbiddenError;
case HttpStatusCode.ServiceUnavailable:
return HttpStrings.ServiceUnavailableError;
default:
return HttpStrings.TransferFailedExceptionMessage;
}
}
return HttpStrings.TransferFailedExceptionMessage;
}
default:
return message;
}
}
if (e is OverflowException)
return HttpStrings.BufferOverflowExceptionMessage;
return message;
}
public static int? GetErrorCode(Exception e)
{
if (e == null)
throw new ArgumentNullException("e");
int hResult = e.HResult;
WebException ex = e as WebException;
if (ex == null)
return hResult;
HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse;
if (httpWebResponse == null)
return hResult;
return (int)httpWebResponse.StatusCode;
}
}
}