示例#1
0
        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (!(context.Result is BadRequestObjectResult badRequest))
            {
                return;
            }

            if (badRequest.Value is SerializableError errors)
            {
                // make controller actions that do this:
                //   `return BadRequest(ModelState);`
                // as though they did this instead:
                //   `return BadRequest(new ValidationProblemDetails(ModelState));`

                var problemDetails = ToValidationProblemDetails(errors);
                context.Result = badRequest = new BadRequestObjectResult(problemDetails);
                ProblemDetailsHelper.SetType(problemDetails, badRequest.StatusCode.HasValue == true ? badRequest.StatusCode.Value : default);
            }

            if (badRequest.Value is Microsoft.AspNetCore.Mvc.ProblemDetails details)
            {
                // keep consistent with asp.net core 2.2 conventions that adds a tracing value
                ProblemDetailsHelper.SetTraceId(details, context.HttpContext);
            }
        }
        public void Configure(ProblemDetailsOptions options)
        {
            // This is the default behavior; only include exception details in a development environment.
            options.IncludeExceptionDetails = ctx => Environment.IsDevelopment();

            options.ShouldLogUnhandledException = (ctx, e, d) => IsServerError(d.Status);

            options.MapStatusCode = (ctx, statusCode) => new StatusCodeProblemDetails(statusCode);

            options.IsProblem = IsProblem;

            options.OnBeforeWriteDetails = (ctx, details) =>
            {
                // keep consistent with asp.net core 2.2 conventions that adds a tracing value
                ProblemDetailsHelper.SetTraceId(details, ctx);

                if (details is ArkProblemDetails)
                {
                    var path = LinkGenerator.GetLink(details as ArkProblemDetails, ctx);
                    details.Type = details.Type ?? path;
                }
            };

            _configureExceptionProblemDetails(options);
        }
示例#3
0
        public void Configure(ProblemDetailsOptions options)
        {
            // This is the default behavior; only include exception details in a development environment.
            options.IncludeExceptionDetails = (ctx, ex) => !Environment.IsProduction();

            options.ShouldLogUnhandledException = (ctx, e, d) => _isServerError(d.Status);

            options.IsProblem = _isProblem;

            options.OnBeforeWriteDetails = (ctx, details) =>
            {
                // keep consistent with asp.net core 2.2 conventions that adds a tracing value
                ProblemDetailsHelper.SetTraceId(details, ctx);

                if (details is ArkProblemDetails)
                {
                    var path = LinkGenerator.GetLink(details as ArkProblemDetails, ctx);
                    details.Type = details.Type ?? path;
                }

                if (details is BusinessRuleProblemDetails br)
                {
                    var path = LinkGenerator.GetLink(br.Violation, ctx);
                    details.Type = path;
                }
            };

            _configureExceptionProblemDetails(options);
        }