Exemplo n.º 1
0
        public override void OnException(HttpActionExecutedContext context)
        {
            if (context.Exception is ODataException)
            {
                var ex = context.Exception as ODataException;

                _traceSource.TraceException(ex, ex.Message);

                context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }
        public void WriteError(ErrorRecord errorRecord, bool writeToTraceSource)
        {
            Contract.Requires(null != errorRecord);
            Contract.Requires(null != errorRecord.Exception);

            if (writeToTraceSource && null != TraceSource)
            {
                TraceSource.TraceException(errorRecord.Exception);
            }

            base.WriteError(errorRecord);
        }
        public override void OnException(HttpActionExecutedContext context)
        {
            if (null == context || null == context.Exception || !(context.Exception is HttpStatusException))
            {
                return;
            }

            var ex      = context.Exception as HttpStatusException;
            var message = string.Format(
                "{0}-EX [{1}] {2}"
                ,
                context.ActionContext.Request.GetCorrelationId().ToString()
                ,
                (int)ex.StatusCode
                ,
                ex.Message
                );

            _traceSource.TraceException(ex, message);

            context.Response = context.Request.CreateErrorResponse(ex.StatusCode, ex.Message);
        }
Exemplo n.º 4
0
        public override void OnException(HttpActionExecutedContext context)
        {
            if ((null == context.Exception) || (CONTRACT_REQUIRES_EXCEPTION_FULLNAME != context.Exception.GetType().FullName))
            {
                return;
            }

            var ex = context.Exception;

            Contract.Assert(null != ex);

            var exMessage  = string.IsNullOrWhiteSpace(ex.Message) ? string.Empty : ex.Message;
            var httpParams = exMessage.Split(CONTRACT_EXCEPTION_MESSAGE_HTTP_STATUS_CODE_DELIMITER);

            var statusCodeSegment = CONTRACT_EXCEPTION_MESSAGE_HTTP_STATUS_CODE_SEGMENT < httpParams.Length ? httpParams[CONTRACT_EXCEPTION_MESSAGE_HTTP_STATUS_CODE_SEGMENT].Trim() : null;
            int statusCode;
            var isValidNumber = int.TryParse(statusCodeSegment, out statusCode);

            if (!isValidNumber || 100 > statusCode || 599 < statusCode)
            {
                statusCode = 500;
            }

            var message = string.Format("{0}-EX {1}", Trace.CorrelationManager.ActivityId, exMessage);

            if (statusCode >= 500)
            {
                _traceSource.TraceException(ex, message);
            }
            else
            {
                _traceSource.TraceException(ex, message);

                var innerException = ex.InnerException;
                while (null != innerException)
                {
                    message = string.Format("{0}-EX {1}", Trace.CorrelationManager.ActivityId, innerException.Message);
                    _traceSource.TraceException(innerException, message);
                    innerException = innerException.InnerException;
                }
            }

            if (CONTRACT_EXCEPTION_MESSAGE_HTTP_STATUS_CODE_SEGMENT >= httpParams.Length)
            {
                context.Response = context.Request.CreateErrorResponse(
                    HttpStatusCode.InternalServerError
                    ,
                    new ODataError
                {
                    Message = string.Concat("[ActivityID: ", Trace.CorrelationManager.ActivityId, "] ", ex.Message)
                });
                return;
            }

            string statusMessage;

            if (2 < httpParams.Length && !string.IsNullOrWhiteSpace(httpParams[2].Trim()))
            {
                statusMessage = string.Concat("[ActivityID: ", Trace.CorrelationManager.ActivityId, "] ", httpParams[2].Trim());
            }
            else
            {
                statusMessage = string.Concat("[ActivityID: ", Trace.CorrelationManager.ActivityId, "] ", httpParams[0].Trim());
            }

            context.Response = context.Request.CreateErrorResponse(
                (HttpStatusCode)statusCode,
                new ODataError
            {
                Message = statusMessage
            });
        }