internal void SetProtocolToRequest(ActionExecutingContext context, RequestMessage requestMessage)
        {
            var protocol = context.HttpContext.Request.Headers[Headers.Protocol].ToString();

            if (string.IsNullOrWhiteSpace(protocol) || !Guid.TryParse(protocol, out _))
            {
                protocol = Guid.NewGuid().ToString("N");
                context.HttpContext.Request.Headers.Add(Headers.Protocol, protocol);
                context.HttpContext.Request.Headers[Headers.Protocol] = protocol;
            }

            requestMessage.AddHeader(Headers.Protocol, protocol);
        }
        internal void SetCustomHeaders(ActionExecutingContext context, RequestMessage requestMessage)
        {
            if (requestMessage == null)
            {
                return;
            }

            foreach (string headerKeyExtract in HeaderKeysToExtract)
            {
                var value = context.HttpContext.Request.Headers[headerKeyExtract].ToString();

                if (!string.IsNullOrWhiteSpace(value))
                {
                    requestMessage.AddHeader(headerKeyExtract, value);
                }
            }
        }
        private async Task HandleExceptionAsync(HttpContext context, Exception ex)
        {
            if (ex is AggregateException)
            {
                ex = ex.InnerException;
            }

            var errorMessage = ViewDetailsOnResponse ? ex.ToString() : Messages.ErrorDefault;

            var protocol = context.Request.Headers[Headers.Protocol].ToString();

            if (string.IsNullOrWhiteSpace(protocol) || !Guid.TryParse(protocol, out Guid protocolParsed))
            {
                protocol = Guid.NewGuid().ToString("N");

                if (string.IsNullOrWhiteSpace(protocol))
                {
                    context.Request.Headers.Add(Headers.Protocol, protocol);
                }
                else
                {
                    context.Request.Headers[Headers.Protocol] = protocol;
                }
            }

            var requestMessage = new RequestMessage();

            requestMessage.AddHeader(Headers.Protocol, protocol);

            var result = new ResultResponseMessage(requestMessage);

            result.CreateResponseInternalServerError(errorMessage);

            var jsonResult = JsonSerializer.Serialize(result, new JsonSerializerOptions
            {
                IgnoreNullValues = true,
                Encoder          = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            });

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)result.HttpStatusCode;
            await context.Response.WriteAsync(jsonResult);
        }