/// <summary> /// Handles the web exception. /// </summary> /// <param name="exception">The exception.</param> /// <param name="serializer"></param> /// <exception cref="RedmineTimeoutException">Timeout!</exception> /// <exception cref="NameResolutionFailureException">Bad domain name!</exception> /// <exception cref="NotFoundException"></exception> /// <exception cref="InternalServerErrorException"></exception> /// <exception cref="UnauthorizedException"></exception> /// <exception cref="ForbiddenException"></exception> /// <exception cref="ConflictException">The page that you are trying to update is staled!</exception> /// <exception cref="RedmineException"> /// </exception> /// <exception cref="NotAcceptableException"></exception> public static void HandleWebException(this WebException exception, IRedmineSerializer serializer) { if (exception == null) { return; } var innerException = exception.InnerException ?? exception; switch (exception.Status) { case WebExceptionStatus.Timeout: throw new RedmineTimeoutException(nameof(WebExceptionStatus.Timeout), innerException); case WebExceptionStatus.NameResolutionFailure: throw new NameResolutionFailureException("Bad domain name.", innerException); case WebExceptionStatus.ProtocolError: { var response = (HttpWebResponse)exception.Response; switch ((int)response.StatusCode) { case (int)HttpStatusCode.NotFound: throw new NotFoundException(response.StatusDescription, innerException); case (int)HttpStatusCode.InternalServerError: throw new InternalServerErrorException(response.StatusDescription, innerException); case (int)HttpStatusCode.Unauthorized: throw new UnauthorizedException(response.StatusDescription, innerException); case (int)HttpStatusCode.Forbidden: throw new ForbiddenException(response.StatusDescription, innerException); case (int)HttpStatusCode.Conflict: throw new ConflictException("The page that you are trying to update is staled!", innerException); case 422: var errors = GetRedmineExceptions(exception.Response, serializer); var message = string.Empty; if (errors != null) { foreach (var error in errors) { message = message + error.Info + Environment.NewLine; } } throw new RedmineException("Invalid or missing attribute parameters: " + message, innerException); case (int)HttpStatusCode.NotAcceptable: throw new NotAcceptableException(response.StatusDescription, innerException); } } break; default: throw new RedmineException(exception.Message, innerException); } }
/// <summary> /// Initializes a new instance of the <see cref="RedmineManager" /> class. /// </summary> /// <param name="host">The host.</param> /// <param name="mimeFormat">The MIME format.</param> /// <param name="verifyServerCert">if set to <c>true</c> [verify server cert].</param> /// <param name="proxy">The proxy.</param> /// <param name="securityProtocolType">Use this parameter to specify a SecurityProtcolType. Note: it is recommended to leave this parameter at its default value as this setting also affects the calling application process.</param> /// <param name="scheme">http or https. Default is https.</param> /// <param name="timeout">The webclient timeout. Default is 100 seconds.</param> /// <exception cref="Redmine.Net.Api.Exceptions.RedmineException"> /// Host is not defined! /// or /// The host is not valid! /// </exception> public RedmineManager(string host, MimeFormat mimeFormat = MimeFormat.Xml, bool verifyServerCert = true, IWebProxy proxy = null, SecurityProtocolType securityProtocolType = default, string scheme = "https", TimeSpan?timeout = null) { if (string.IsNullOrEmpty(host)) { throw new RedmineException("Host is not defined!"); } PageSize = 25; Scheme = scheme; Host = host; MimeFormat = mimeFormat; Timeout = timeout; Proxy = proxy; if (mimeFormat == MimeFormat.Xml) { Format = "xml"; Serializer = new XmlRedmineSerializer(); } else { Format = "json"; Serializer = new JsonRedmineSerializer(); } if (securityProtocolType == default) { securityProtocolType = ServicePointManager.SecurityProtocol; } SecurityProtocolType = securityProtocolType; ServicePointManager.SecurityProtocol = securityProtocolType; if (!verifyServerCert) { ServicePointManager.ServerCertificateValidationCallback += RemoteCertValidate; } }
/// <summary> /// Gets the redmine exceptions. /// </summary> /// <param name="webResponse">The web response.</param> /// <param name="serializer"></param> /// <returns></returns> private static IEnumerable <Error> GetRedmineExceptions(this WebResponse webResponse, IRedmineSerializer serializer) { using (var responseStream = webResponse.GetResponseStream()) { if (responseStream == null) { return(null); } using (var streamReader = new StreamReader(responseStream)) { var responseContent = streamReader.ReadToEnd(); if (responseContent.IsNullOrWhiteSpace()) { return(null); } try { var result = serializer.DeserializeToPagedResults <Error>(responseContent); return(result.Items); } catch (Exception) { throw; } } } }