/// <summary>
 /// Called whenever an item is addded the underlying collection.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="message">The message.</param>
 private void Message_ItemAdded(object sender, IGraphMessage message)
 {
     if (message.Severity > this.Severity)
     {
         this.Severity = message.Severity;
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphFieldErrorActionResult"/> class.
 /// </summary>
 /// <param name="message">A custom generated graph message to add to the result.</param>
 public GraphFieldErrorActionResult(IGraphMessage message)
 {
     _customMessage = message;
 }
 /// <summary>
 /// Adds the specified message to the collection.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <returns>IGraphExecutionMessage.</returns>
 public IGraphMessage Add(IGraphMessage message)
 {
     _messages.Add(message);
     return(message);
 }
 /// <summary>
 /// Called whenever the underlying list is fully cleared.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="message">The message that was removed (null for this operation).</param>
 private void Messages_Cleared(object sender, IGraphMessage message)
 {
     this.Severity = GraphMessageSeverity.Trace;
 }
        /// <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
        }
 /// <summary>
 /// Writes the single message to the provided writer.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <param name="message">The message to serialize.</param>
 /// <param name="options">The options to use to govern the exposure of message level
 /// metadata.</param>
 public void WriteSingleMessage(Utf8JsonWriter writer, IGraphMessage message, GraphQLResponseOptions options)
 {
     this.WriteMessage(writer, message, options);
 }
Beispiel #7
0
 /// <summary>
 /// Returns an error indicating that an issue occured.
 /// </summary>
 /// <param name="message">A custom generated message.</param>
 /// <returns>IGraphActionResult.</returns>
 protected virtual IGraphActionResult Error(
     IGraphMessage message)
 {
     return(new GraphFieldErrorActionResult(message));
 }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ArgumentGenerationResult"/> class.
 /// </summary>
 /// <param name="message">The message.</param>
 public ArgumentGenerationResult(IGraphMessage message)
 {
     this.Message = message;
 }
Beispiel #9
0
 /// <summary>
 /// Called whenever the underlying list is fully cleared.
 /// </summary>
 /// <param name="sender">The sender.</param>
 /// <param name="message">The message that was removed (null for this operation).</param>
 private void Messages_Cleared(object sender, IGraphMessage message)
 {
     this.Severity = GraphMessageSeverity.Information;
 }