/// <summary>
 /// Initializes static members of the <see cref="GraphQLResponseOptions"/> class.
 /// </summary>
 static GraphQLResponseOptions()
 {
     Default = new GraphQLResponseOptions()
     {
         ExposeMetrics    = false,
         ExposeExceptions = false,
     };
 }
        /// <summary>
        /// Creates a new dictionary of properties for the message map
        /// Spec: https://graphql.github.io/graphql-spec/June2018/#sec-Errors .
        /// </summary>
        /// <param name="writer">The json writer to output the reslts to.</param>
        /// <param name="message">The message to render into the output.</param>
        /// <param name="options">A set options to customize how the response is serialized to the stream.</param>
        protected virtual void WriteMessage(Utf8JsonWriter writer, IGraphMessage message, GraphQLResponseOptions options)
        {
            writer.WriteStartObject();
            this.WritePreEncodedString(writer, "message", message.Message);

            if (message.Origin.Location != SourceLocation.None)
            {
                writer.WriteStartArray("locations");
                writer.WriteStartObject();
                writer.WriteNumber("line", message.Origin.Location.LineNumber);
                writer.WriteNumber("column", message.Origin.Location.LinePosition);
                writer.WriteEndObject();
                writer.WriteEndArray(); // locations
            }

            if (message.Origin.Path != SourcePath.None)
            {
                writer.WriteStartArray("path");
                foreach (var loc in message.Origin.Path)
                {
                    if (loc is int i)
                    {
                        writer.WriteNumberValue(i);
                    }
                    else if (loc is string str)
                    {
                        this.WritePreEncodedStringValue(writer, str);
                    }
                    else
                    {
                        writer.WriteNullValue();
                    }
                }

                writer.WriteEndArray(); // path
            }

            writer.WriteStartObject("extensions");
            this.WriteLeaf(writer, "code", message.Code);

            var timestamp = this.TimeLocalizer?.Invoke(message.TimeStamp) ?? message.TimeStamp;

            this.WriteLeaf(writer, "timestamp", timestamp);
            this.WriteLeaf(writer, "severity", message.Severity);

            if (message.MetaData != null && message.MetaData.Count > 0)
            {
                writer.WriteStartObject("metaData");
                foreach (var item in message.MetaData)
                {
                    this.WriteLeaf(writer, item.Key, item.Value, true);
                }

                writer.WriteEndObject(); // metadata
            }

            if (options.ExposeExceptions && message.Exception != null)
            {
                writer.WriteStartObject("exception");
                this.WriteLeaf(writer, "type", message.Exception.GetType().FriendlyName());
                this.WriteLeaf(writer, "message", message.Exception.Message);

                const int maxStackTrace     = 4000;
                var       stacktraceMessage = message.Exception.StackTrace;
                if (stacktraceMessage?.Length > maxStackTrace)
                {
                    stacktraceMessage  = stacktraceMessage.Substring(0, maxStackTrace);
                    stacktraceMessage += $"[cut at {maxStackTrace} characters]";
                }

                if (!string.IsNullOrWhiteSpace(stacktraceMessage))
                {
                    this.WriteLeaf(writer, "stacktrace", stacktraceMessage);
                }

                writer.WriteEndObject(); // exception
            }

            writer.WriteEndObject(); // extensions
            writer.WriteEndObject(); // message
        }