private Task WriteExceptionAsync(HttpContext httpContext, Exception exception, HttpStatusCode code, CorrelationVector cv, Tag tag)
        {
            var response = httpContext.Response;

            response.ContentType = "application/json";
            response.StatusCode  = (int)code;

            ErrorMessageContractV1 errorMessage;

            if (_verboseOnErrors)
            {
                errorMessage = new ErrorMessageContractV1
                {
                    HttpStatus    = (int)code,
                    RequestUrl    = httpContext.Request.GetDisplayUrl(),
                    Message       = exception.Message,
                    ExceptionType = exception.GetType().FullName,
                    DetailMessage = exception.ToString(),
                    Cv            = cv,
                    Tag           = tag,
                };

                if (cv != null)
                {
                    IEnumerable <EventDetailContractV1> eventData = _eventDataBuffer.SearchForBaseCv(cv.Value).Select(x => x.ConvertTo());
                    if (eventData.Count() > 0)
                    {
                        errorMessage.EventDetails = new List <EventDetailContractV1>(eventData);
                    }
                }
            }
            else
            {
                errorMessage = new ErrorMessageContractV1
                {
                    HttpStatus    = (int)code,
                    RequestUrl    = httpContext.Request.GetDisplayUrl(),
                    Message       = exception.Message,
                    ExceptionType = exception.GetType().FullName,
                    Cv            = cv,
                };
            }

            if (cv != null)
            {
                response.Headers.Add(CvHeader.HeaderKey, new StringValues(cv));
            }

            return(response.WriteAsync(JsonConvert.SerializeObject(errorMessage)));
        }
Beispiel #2
0
        public void OnException(ExceptionContext context)
        {
            HttpStatusCode code = context.Exception.ToHttpStatusCode();

            var errorMessage = new ErrorMessageContractV1
            {
                HttpStatus    = (int)code,
                Message       = context.Exception.Message,
                ExceptionType = context.Exception.GetType().FullName,
                DetailMessage = context.Exception.StackTrace,
            };

            context.Result = new ObjectResult(errorMessage)
            {
                StatusCode   = (int)code,
                DeclaredType = typeof(ErrorMessageContractV1)
            };

            ToolboxEventSource.Log.Error(_context, nameof(GlobalExceptionFilter), context.Exception);
        }