Пример #1
0
        /// <summary>
        /// Write the formatted JSON response for this exception, and all inner exceptions.
        /// </summary>
        /// <param name="ex">The exception response object to serialize.</param>
        /// <returns>The serialized JSON string.</returns>
        public static string WriteJson(ExceptionInfo ex)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            MeteredStringBuilder jsonBuilder = new MeteredStringBuilder(TEXT_ENCODING, MAX_PAYLOAD_SIZE);
            string json = AppendJson(ex, 0, false, MAX_PAYLOAD_SIZE - jsonBuilder.SizeInBytes);

            if (json != null && jsonBuilder.HasRoomForString(json))
            {
                jsonBuilder.Append(json);
            }
            else
            {
                jsonBuilder.Append(TRUNCATED_MESSAGE);
            }
            return(jsonBuilder.ToString());
        }
Пример #2
0
        private static string AppendJson(ExceptionInfo ex, int tab, bool appendComma, int remainingRoom)
        {
            if (remainingRoom <= 0)
            {
                return(null);
            }

            MeteredStringBuilder jsonBuilder = new MeteredStringBuilder(TEXT_ENCODING, remainingRoom);
            int nextTabDepth     = tab + 1;
            int nextNextTabDepth = nextTabDepth + 1;

            List <string> jsonElements = new List <string>();

            // Grab the elements we want to capture
            string               message         = JsonExceptionWriterHelpers.EscapeStringForJson(ex.ErrorMessage);
            string               type            = JsonExceptionWriterHelpers.EscapeStringForJson(ex.ErrorType);
            string               stackTrace      = ex.StackTrace;
            ExceptionInfo        innerException  = ex.InnerException;
            List <ExceptionInfo> innerExceptions = ex.InnerExceptions;

            // Create the JSON lines for each non-null element
            string messageJson = null;

            if (message != null)
            {
                // Trim important for Aggregate Exceptions, whose
                // message contains multiple lines by default
                messageJson = TabString($"\"{ERROR_MESSAGE}\": \"{message}\"", nextTabDepth);
            }

            string typeJson       = TabString($"\"{ERROR_TYPE}\": \"{type}\"", nextTabDepth);
            string stackTraceJson = GetStackTraceJson(stackTrace, nextTabDepth);


            // Add each non-null element to the json elements list
            if (typeJson != null)
            {
                jsonElements.Add(typeJson);
            }
            if (messageJson != null)
            {
                jsonElements.Add(messageJson);
            }
            if (stackTraceJson != null)
            {
                jsonElements.Add(stackTraceJson);
            }

            // Exception JSON body, comma delimited
            string exceptionJsonBody = string.Join("," + Environment.NewLine, jsonElements);

            jsonBuilder.AppendLine(TabString("{", tab));
            jsonBuilder.Append(exceptionJsonBody);

            bool hasInnerException     = innerException != null;
            bool hasInnerExceptionList = innerExceptions != null && innerExceptions.Count > 0;

            // Before we close, check for inner exception(s)
            if (hasInnerException)
            {
                // We have to add the inner exception, which means we need
                // another comma after the exception json body
                jsonBuilder.AppendLine(",");

                jsonBuilder.Append(TabString($"\"{INNER_EXCEPTION}\": ", nextTabDepth));

                string innerJson = AppendJson(innerException, nextTabDepth, hasInnerExceptionList, remainingRoom - jsonBuilder.SizeInBytes);
                if (innerJson != null && jsonBuilder.HasRoomForString(innerJson))
                {
                    jsonBuilder.Append(innerJson);
                }
                else
                {
                    jsonBuilder.AppendLine(TRUNCATED_MESSAGE);
                }
            }

            if (hasInnerExceptionList)
            {
                jsonBuilder.Append(TabString($"\"{INNER_EXCEPTIONS}\": [", nextTabDepth));

                for (int i = 0; i < innerExceptions.Count; i++)
                {
                    var    isLastOne       = i == innerExceptions.Count - 1;
                    var    innerException2 = innerExceptions[i];
                    string innerJson       = AppendJson(innerException2, nextNextTabDepth, !isLastOne, remainingRoom - jsonBuilder.SizeInBytes);
                    if (innerJson != null && jsonBuilder.HasRoomForString(innerJson))
                    {
                        jsonBuilder.Append(innerJson);
                    }
                    else
                    {
                        jsonBuilder.AppendLine(TabString(TRUNCATED_MESSAGE, nextNextTabDepth));
                        break;
                    }
                }

                jsonBuilder.AppendLine(TabString($"]", nextTabDepth));
            }

            if (!hasInnerException && !hasInnerExceptionList)
            {
                // No inner exceptions = no trailing comma needed
                jsonBuilder.AppendLine();
            }

            jsonBuilder.AppendLine(TabString("}" + (appendComma ? "," : ""), tab));
            return(jsonBuilder.ToString());
        }