/// <summary>
            /// Resolves an input exception to an instance of <see cref="CommerceRuntimeException"/>.
            /// </summary>
            /// <param name="exception">The exception to be resolved.</param>
            /// <returns>The instance of <see cref="CommerceRuntimeException"/>.</returns>
            private static CommerceException ResolveToCommerceException(this Exception exception)
            {
                var crtException = exception as CommerceException;

                if (crtException != null)
                {
                    // Gets the exception type's name.
                    string typeName = crtException.GetType().Name.Replace(CommerceExceptionTypeName, RetailProxyExceptionTypeName);

                    // Hides the exception if the type name is not recognized by client (not exposed via retail server).
                    if (!KnownCommerceExceptionTypes.Value.ContainsKey(typeName))
                    {
                        crtException = new CommerceException(InternalServerErrorResourceId, exception.Message);
                    }
                }
                else
                {
                    crtException = new CommerceException(InternalServerErrorResourceId, exception.Message);
                }

                if (string.IsNullOrWhiteSpace(crtException.LocalizedMessage))
                {
                    // Translates the error message.
                    string cultureName = CommerceRuntimeManager.Locale;
                    crtException.TranslateUserMessage(ExceptionLocalizerInstance.Value, cultureName);
                }

                return(crtException);
            }
            /// <summary>
            /// Serializes the commerce runtime exception into commerce error string based on the provided serialization function.
            /// </summary>
            /// <param name="exception">The <see cref="CommerceRuntimeException"/> to be serialized.</param>
            /// <param name="serialize">The serialization function.</param>
            /// <returns>The serialized error in <see cref="CommerceError"/> format.</returns>
            private static string SerializeToCommerceError(this CommerceException exception, Func <object, string> serialize)
            {
                string exceptionName = exception.GetType().Name;

                // Then serializes the commerce runtime exception.
                string exceptionString = serialize(exception);
                var    error           = new CommerceError(exceptionName, exceptionString);

                // Finally, serializes the commerce error, which will be the object exposed to the client through the wire.
                return(serialize(error));
            }
Esempio n. 3
0
            /// <summary>
            /// Logs exception event.
            /// </summary>
            /// <param name="exception">Exception instance to log.</param>
            /// <param name="correlationId">The correlation identifier.</param>
            private static void LogRequestExecutionFailure(Exception exception, Guid correlationId)
            {
                string            exceptionTypeName = exception.GetType().FullName;
                CommerceException commerceException = exception as CommerceException;

                if (commerceException == null)
                {
                    // Tracing low-level exception as 'Error' because it is either a bug (NullReferenceException, ArgumentNullException etc.)
                    // or external dependency that is not wrapped by try/catch block.
                    RetailLogger.Log.CrtExecuteRequestErrorFailure(correlationId, exception, exceptionTypeName, errorResourceId: string.Empty);
                }
                else
                {
                    switch (commerceException.Severity)
                    {
                    case ExceptionSeverity.Informational:
                    {
                        RetailLogger.Log.CrtExecuteRequestInformationalFailure(correlationId, commerceException, exceptionTypeName, commerceException.ErrorResourceId);
                        break;
                    }

                    case ExceptionSeverity.Warning:
                    {
                        RetailLogger.Log.CrtExecuteRequestWarningFailure(correlationId, commerceException, exceptionTypeName, commerceException.ErrorResourceId);
                        break;
                    }

                    case ExceptionSeverity.Critical:
                    {
                        RetailLogger.Log.CrtExecuteRequestCriticalFailure(correlationId, commerceException, exceptionTypeName, commerceException.ErrorResourceId);
                        break;
                    }

                    default:
                    {
                        // For levels 'Error' and 'None' use default trace level 'Error'.
                        RetailLogger.Log.CrtExecuteRequestErrorFailure(correlationId, commerceException, exceptionTypeName, commerceException.ErrorResourceId);
                        break;
                    }
                    }
                }
            }
            /// <summary>
            /// Creates serialized <see cref="CommerceRuntimeException"/> based on the given exception object.
            /// </summary>
            /// <param name="exception">The exception object.</param>
            /// <returns>The serialized <see cref="CommerceRuntimeException"/> object.</returns>
            internal static string SerializeToCommerceException(this Exception exception)
            {
                CommerceException crtException = exception.ResolveToCommerceException();

                return(crtException.SerializeToCommerceError(SerializeObjectToJson));
            }