Exemple #1
0
        //TODO remove a the logMessage set, logger service might be a good candidate
        /// <summary>
        /// Handles all exceptions in application
        /// </summary>
        /// <param name="context">The HttpContext for the request</param>
        /// <param name="exception">The exception that was thrown</param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            _trackingService.AddTrackingIdToContext(context);
            var logMessage = "An unhandled exception has occurred: " + exception.Message;
            var code       = (int)HttpStatusCode.InternalServerError;

            var result = HttpErrorResponse.GetHttpErrorResponse(exception);

            if (exception is ReplayDomainException)
            {
                logMessage = "A replay domain validation exception has occurred: " + exception.Message;
                code       = DomainException.HttpErrorCode;
            }
            else if (exception is DomainException)
            {
                logMessage = "A domain validation exception has occurred: " + exception.Message;
                code       = DomainException.HttpErrorCode;
            }

            _logger.LogError(exception, logMessage);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = code;

            return(context.Response.WriteAsync(result));
        }
Exemple #2
0
        /// <summary>
        /// Handles all exceptions in application
        /// </summary>
        /// <param name="context">The HttpContext for the request</param>
        /// <param name="exception">The exception that was thrown</param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception, String requestBody)
        {
            var logMessage = "An unhandled exception has occurred: " + exception.Message;
            var code       = (int)HttpStatusCode.BadRequest;
            var logInfo    = false;

            var result = HttpErrorResponse.GetHttpErrorResponse(exception);

            if (exception is ReplayDomainException)
            {
                logMessage = "A replay domain validation exception has occurred: " + exception.Message;
                code       = DomainException.HttpErrorCode;
                logInfo    = true;
            }
            else if (exception is DomainException)
            {
                logMessage = "A domain validation exception has occurred: " + exception.Message;
                code       = DomainException.HttpErrorCode;
            }
            else if (exception is InvalidOperationException)
            {
                logMessage = "Authentication invalid: " + exception.Message;
                code       = (int)HttpStatusCode.Unauthorized;
            }

            // Custom exceptions with above IF..

            if (logInfo)
            {
                _logger.LogInformation(exception, logMessage,
                                       string.Format("Original Request: {0} {1}://{2}{3} {4}",
                                                     context.Request.Method,
                                                     context.Request.Scheme,
                                                     context.Request.Host,
                                                     context.Request.Path,
                                                     context.Request.QueryString),
                                       requestBody);
            }
            else
            {
                _logger.LogError(exception, logMessage,
                                 string.Format("Original Request: {0} {1}://{2}{3} {4}",
                                               context.Request.Method,
                                               context.Request.Scheme,
                                               context.Request.Host,
                                               context.Request.Path,
                                               context.Request.QueryString),
                                 requestBody);
            }

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = code;

            return(context.Response.WriteAsync(result));
        }