/// <summary>
            /// Executes the <param name="method"></param> with exception handling.
            /// </summary>
            /// <typeparam name="TResult">The type of the result.</typeparam>
            /// <param name="method">The method.</param>
            /// <returns>The object of type TResult.</returns>
            private static async Task <TResult> ExecuteWithExceptionHandlingAsync <TResult>(Func <Task <TResult> > method)
            {
                try
                {
                    return(await method());
                }
                catch (WebException ex)
                {
                    using (var streamReader = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        var response = streamReader.ReadToEnd();

                        // Deserializes to exception
                        RetailProxyException crtException = null;
                        if (DefaultExceptionHandlingBehavior.TryDeserializeFromJsonString(response, out crtException) && crtException != null)
                        {
                            throw crtException;
                        }
                        else
                        {
                            // If exception is not mapped to a commerce runtime exception, throws the local exception based on HTTP status code.
                            CommunicationExceptionHelper.ThrowAsRetailProxyExceptionOnHttpStatuCode(ex, (int)((HttpWebResponse)ex.Response).StatusCode);
                            throw ex;
                        }
                    }
                }
            }
Пример #2
0
            /// <summary>
            /// Tries to throws a commerce exception, if the result could be serialized as a commerce exception; Proceeds silently, otherwise.
            /// </summary>
            /// <param name="result">The serialized return result.</param>
            private static void TryThrowAsCommerceException(string result)
            {
                if (!string.IsNullOrWhiteSpace(result))
                {
                    RetailProxyException crtException = null;

                    if (DefaultExceptionHandlingBehavior.TryDeserializeFromJsonString(result, out crtException) &&
                        crtException != null)
                    {
                        throw crtException;
                    }
                }
            }
            /// <summary>
            /// Traverses and throws the commerce exception from the input exception hierarchy.
            /// </summary>
            /// <param name="exception">The exception to traverse.</param>
            public static void ThrowAsCommerceException(Exception exception)
            {
                Exception tempException = exception == null ? null : exception.InnerException;

                while (tempException != null && !string.IsNullOrWhiteSpace(tempException.Message))
                {
                    // Deserializes to exception
                    RetailProxyException crtException = null;
                    if (DefaultExceptionHandlingBehavior.TryDeserializeFromJsonString(tempException.Message, out crtException) &&
                        crtException != null)
                    {
                        throw crtException;
                    }

                    tempException = tempException.InnerException;
                }
            }
            /// <summary>
            /// Executes an action with the proper error handling logic.
            /// </summary>
            /// <typeparam name="T">The return type.</typeparam>
            /// <param name="action">The action to be performed.</param>
            /// <returns>The returned value from the action.</returns>
            private static Task <T> Execute <T>(Func <T> action)
            {
                return(Task.Run(() =>
                {
                    try
                    {
                        return action();
                    }
                    catch (Exception ex)
                    {
                        string exceptionResponse = ex.SerializeToCommerceException();

                        RetailProxyException crtException = null;
                        if (DefaultExceptionHandlingBehavior.TryDeserializeFromJsonString(exceptionResponse, out crtException) && crtException != null)
                        {
                            throw crtException;
                        }

                        throw new RetailProxyException(string.Format("Unexpected exception payload {0}.", exceptionResponse));
                    }
                }));
            }