Example #1
0
        public void Null()
        {
#if NETCOREAPP
            AspNetExceptionConverter.ToContentResult(null);
#else
            AspNetExceptionConverter.ToHttpResponseMessage(null);
#endif
        }
Example #2
0
        public void ArgumentNullException()
        {
            var message   = Guid.NewGuid().ToString();
            var exception = new ArgumentNullException("the-parameter-name", message);

#if NETCOREAPP
            var result = AspNetExceptionConverter.ToContentResult(exception);
#else
            var result = AspNetExceptionConverter.ToHttpResponseMessage(exception);
#endif
            // ReSharper disable once PossibleInvalidOperationException
            Assert.AreEqual((int)HttpStatusCode.InternalServerError, (int)result.StatusCode);
        }
Example #3
0
        public void RequestPostponedException()
        {
            var id        = Guid.NewGuid().ToString();
            var exception = new RequestPostponedException(id);

#if NETCOREAPP
            var result = AspNetExceptionConverter.ToContentResult(exception);
#else
            var result = AspNetExceptionConverter.ToHttpResponseMessage(exception);
#endif
            // ReSharper disable once PossibleInvalidOperationException
            Assert.AreEqual((int)HttpStatusCode.Accepted, (int)result.StatusCode);
        }
Example #4
0
        protected static async Task ConvertExceptionToResponseAsync(HttpContext context, Exception exception, CancellationToken cancellationToken)
        {
            if (exception is RequestPostponedException rpe && rpe.ReentryAuthentication == null)
            {
                rpe.ReentryAuthentication = CalculateReentryAuthentication(context.Request);
            }
            var response = AspNetExceptionConverter.ToContentResult(exception);

            FulcrumAssert.IsTrue(response.StatusCode.HasValue, CodeLocation.AsString());
            Debug.Assert(response.StatusCode.HasValue);
            context.Response.StatusCode  = response.StatusCode.Value;
            context.Response.ContentType = response.ContentType;
            await context.Response.WriteAsync(response.Content, cancellationToken : cancellationToken);
        }
        /// <summary>Create a bad request response if the model is not valid.</summary>
        /// <param name="actionContext">The action context.</param>
        //TODO: Temporary solution for this. Its not good enough, need more digging into error handling in .NETCore
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            InternalContract.RequireNotNull(actionContext, nameof(actionContext));
            if (actionContext.ModelState.IsValid)
            {
                return;
            }

            var fulcrumException = new FulcrumServiceContractException("ignore")
            {
                Code             = ModelValidationErrorCode,
                TechnicalMessage = actionContext.ModelState.ToString()
            };

            actionContext.Result = AspNetExceptionConverter.ToContentResult(fulcrumException);
        }
Example #6
0
        public async Task InvokeAsync(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception exception)
            {
                Log.LogError($"The web service had an internal exception ({exception.Message})", exception);

                var response = AspNetExceptionConverter.ToContentResult(exception);
                Log.LogInformation(
                    $"Exception ({exception.Message}) was converted to an HTTP response ({response.StatusCode}).");

                FulcrumAssert.IsTrue(response.StatusCode.HasValue, CodeLocation.AsString());
                Debug.Assert(response.StatusCode.HasValue);
                context.Response.StatusCode  = response.StatusCode.Value;
                context.Response.ContentType = response.ContentType;
                await context.Response.WriteAsync(response.Content);
            }
        }