public static async Task TranslateToMessagingExceptionAsync()

in src/Microsoft.Azure.NotificationHubs/ExceptionUtility.cs [35:84]


        public static async Task<Exception> TranslateToMessagingExceptionAsync(this HttpResponseMessage response, string trackingId)
        {
            var responseBody = string.Empty;
            if (response.Content != null)
            {
                responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            var exceptionMessage = FormatExceptionMessage(responseBody, response.StatusCode, response.ReasonPhrase, trackingId);
            var statusCode = response.StatusCode;
            var retryAfter = response.Headers.RetryAfter?.Delta;
            
            switch (statusCode)
            {
                case HttpStatusCode.NotFound:
                case HttpStatusCode.NoContent:
                    return new MessagingEntityNotFoundException(new MessagingExceptionDetail(ExceptionErrorCodes.EndpointNotFound, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId));
                case HttpStatusCode.Conflict:
                    if (response.RequestMessage.Method.Equals(HttpMethod.Delete) ||
                        response.RequestMessage.Method.Equals(HttpMethod.Put) ||
                        exceptionMessage.Contains(ConflictOperationInProgressSubCode))
                    {
                        return new MessagingException(new MessagingExceptionDetail(ExceptionErrorCodes.ConflictGeneric, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId), false);
                    }
                    else
                    {
                        return new MessagingEntityAlreadyExistsException(new MessagingExceptionDetail(ExceptionErrorCodes.ConflictGeneric, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId));
                    }

                case HttpStatusCode.Unauthorized:
                    return new UnauthorizedException(new MessagingExceptionDetail(ExceptionErrorCodes.UnauthorizedGeneric, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId));
                case HttpStatusCode.Forbidden:
                    // Forbidden response code can be returned when service is throttling. When this happens, the `Retry-After` header will be present so we use this to differentiate the failure mode.
                    if (retryAfter != null)
                    {
                        return new QuotaExceededException(new MessagingExceptionDetail(ExceptionErrorCodes.Throttled, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId), retryAfter);
                    }
                    return new UnauthorizedException(new MessagingExceptionDetail(ExceptionErrorCodes.ForbiddenGeneric, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId));
                case (HttpStatusCode)429:
                    return new QuotaExceededException(new MessagingExceptionDetail(ExceptionErrorCodes.Throttled, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId), retryAfter);
                case HttpStatusCode.BadRequest:
                    return new BadRequestException(new MessagingExceptionDetail(ExceptionErrorCodes.BadRequest, exceptionMessage, ErrorLevelType.UserError, statusCode, trackingId));
                case HttpStatusCode.InternalServerError:
                case HttpStatusCode.ServiceUnavailable:
                case HttpStatusCode.GatewayTimeout:
                case HttpStatusCode.RequestTimeout:
                    return new ServerBusyException(new MessagingExceptionDetail(ExceptionErrorCodes.ServerBusy, exceptionMessage, ErrorLevelType.ServerError, statusCode, trackingId));
            }

            return new MessagingException(new MessagingExceptionDetail(ExceptionErrorCodes.UnknownExceptionDetail, exceptionMessage, ErrorLevelType.ServerError, statusCode, trackingId), false);
        }