Пример #1
0
        public static ApiRequestException Parse <T>(ApiResponse <T> apiResponse)
        {
            ApiRequestException exception;

            var typeInfo = ExceptionInfos
                           .SingleOrDefault(info => apiResponse.Description.Contains(info.ErrorMessage));

            if (typeInfo is null)
            {
                Debug.WriteLine($"Exception type info not found. {apiResponse.ErrorCode} - {apiResponse.Description}");
                exception = new ApiRequestException(apiResponse.Description, apiResponse.ErrorCode, apiResponse.Parameters);
            }
            else
            {
                string errorMessage;
                bool   isBadRequestError = typeInfo.ErrorCode == BadRequestException.BadRequestErrorCode;

                if (isBadRequestError)
                {
                    errorMessage = TruncateBadRequestErrorDescription(apiResponse.Description);
                }
                else
                {
                    errorMessage = TruncateForbiddenErrorDescription(apiResponse.Description);
                }

                exception = Activator.CreateInstance(typeInfo.Type, errorMessage) as ApiRequestException;
            }
            return(exception);
        }
Пример #2
0
        public static ApiRequestException Parse <T>(ApiResponse <T> apiResponse)
        {
            ApiRequestException exception;

            var typeInfo = ExceptionInfos
                           .FirstOrDefault(info => Regex.IsMatch(apiResponse.Description, info.ErrorMessageRegex));

            if (typeInfo is null)
            {
                exception = new ApiRequestException(apiResponse.Description, apiResponse.ErrorCode, apiResponse.Parameters);
            }
            else
            {
                string errorMessage;
                bool   isBadRequestError = typeInfo.ErrorCode == BadRequestException.BadRequestErrorCode;

                if (isBadRequestError)
                {
                    errorMessage = TruncateBadRequestErrorDescription(apiResponse.Description);

                    if (typeInfo.Type == typeof(InvalidParameterException))
                    {
                        string paramName = Regex.Match(apiResponse.Description, typeInfo.ErrorMessageRegex)
                                           .Groups[InvalidParameterException.ParamGroupName]
                                           .Value;
                        exception = new InvalidParameterException(paramName, errorMessage);
                    }
                    else
                    {
                        exception = Activator.CreateInstance(typeInfo.Type, errorMessage) as ApiRequestException;
                    }
                }
                else
                {
                    errorMessage = TruncateForbiddenErrorDescription(apiResponse.Description);
                    exception    = Activator.CreateInstance(typeInfo.Type, errorMessage) as ApiRequestException;
                }
            }
            return(exception);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ReceiveErrorEventArgs"/> class.
 /// </summary>
 /// <param name="apiRequestException">The API request exception.</param>
 internal ReceiveErrorEventArgs(ApiRequestException apiRequestException)
 {
     ApiRequestException = apiRequestException;
 }