Ejemplo n.º 1
0
        /// <summary>
        /// Handles exceptions accross the application and returns correct Http Status Code if possible
        /// </summary>
        /// <param name="actionExecutedContext">Instance of <see cref="HttpActionExecutedContext"/></param>
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            //TODO: Assign correct status codes to all custom exceptions
            if (actionExecutedContext.Exception is NsiBaseException customException)
            {
                _logger.LogException(customException, actionExecutedContext.Request, customException.Severity);

                HttpStatusCode statusCode = HttpStatusCode.BadRequest;

                if (customException is NsiNotAuthorizedException)
                {
                    statusCode = HttpStatusCode.Unauthorized;
                }
                else if (customException is NsiNotFoundException)
                {
                    statusCode = HttpStatusCode.NotFound;
                }
                else if (customException is NsiArgumentNullException || customException is NsiArgumentException)
                {
                    statusCode = HttpStatusCode.BadRequest;
                }

                CreateResponseOnException(actionExecutedContext, statusCode, customException.Message);
            }
            else
            {
                CreateResponseOnException(actionExecutedContext, HttpStatusCode.InternalServerError, ExceptionMessages.UnhandledExceptionMessage);
                _logger.LogException(actionExecutedContext.Exception, actionExecutedContext.Request, Common.Enumerations.SeverityEnum.Error);
            }
        }