public async Task InvokeAsync(HttpContext context)
            {
                context.Response.StatusCode = _statusCode;
                FulcrumException fulcrumException = null;

                if (_statusCode >= 500)
                {
                    fulcrumException = new FulcrumAssertionFailedException("Internal error message");
                }
                else if (_statusCode >= 400)
                {
                    fulcrumException = new FulcrumServiceContractException("Client error message");
                }

                if (_statusCode >= 400)
                {
                    await context.Response.WriteAsync("Test");

                    context.Response.Body        = new MemoryStream();
                    context.Response.ContentType = "application/json";
                    var fulcrumError = ExceptionConverter.ToFulcrumError(fulcrumException);
                    var content      = ExceptionConverter.ToJsonString(fulcrumError, Formatting.Indented);
                    await context.Response.WriteAsync(content);
                }
            }
        /// <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 #3
0
        public async Task ConvertNormal()
        {
            var fulcrumException = new FulcrumServiceContractException("Test message");
            var fulcrumError     = new FulcrumError();

            fulcrumError.CopyFrom(fulcrumException);
            Assert.IsNotNull(fulcrumError);
            var json            = JObject.FromObject(fulcrumError);
            var content         = json.ToString(Formatting.Indented);
            var responseMessage = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(content, Encoding.UTF8)
            };
            var result = await ExceptionConverter.ToFulcrumExceptionAsync(responseMessage);

            Assert.AreEqual(fulcrumException.TechnicalMessage, result.TechnicalMessage);
        }
Example #4
0
        /// <summary>Create a bad request response if the model is not valid.</summary>
        /// <param name="actionContext">The action context.</param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            InternalContract.RequireNotNull(actionContext, nameof(actionContext));
            if (actionContext.ModelState.IsValid)
            {
                return;
            }

            var response          = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
            var validationContent = response?.Content?.ReadAsStringAsync().Result;

            FulcrumAssert.IsNotNull(validationContent, $"{Namespace}: 5E706F6E-EA25-4597-BB9D-D002390A1F4C");
            var fulcrumException = new FulcrumServiceContractException("ignore")
            {
                Code             = ModelValidationErrorCode,
                TechnicalMessage = validationContent
            };

            actionContext.Response = Converter.ToHttpResponseMessage(fulcrumException);
        }