protected override IActionResult CreateResult(HttpContext context)
        {
            var errorContext = new ErrorResponseContext(context.Request, Code, Message, messageDetail: null);
            var result       = Options.ErrorResponses.MethodNotAllowed(errorContext);

            return(allowedMethods.Length == 0 ? result : new AllowHeaderResult(result, allowedMethods));
        }
        static TError NewInnerError <TError>(ErrorResponseContext context, Func <ErrorResponseContext, TError> create)
        {
            Contract.Requires(context != null);
            Contract.Requires(create != null);

            if (IsNullOrEmpty(context.MessageDetail))
            {
                return(default);
Example #3
0
 /// <summary>
 /// Creates and returns a new error response given the provided context.
 /// </summary>
 /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to generate the response.</param>
 /// <returns>The generated <see cref="IActionResult">response</see>.</returns>
 public virtual IActionResult CreateResponse(ErrorResponseContext context)
 {
     Arg.NotNull(context, nameof(context));
     return(new ObjectResult(CreateErrorContent(context))
     {
         StatusCode = context.StatusCode
     });
 }
 /// <summary>
 /// Creates and returns a new HTTP 405 (Method Not Allowed) given the provided context.
 /// </summary>
 /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to generate response.</param>
 /// <returns>The generated <see cref="IActionResult">response</see>.</returns>
 public virtual IActionResult MethodNotAllowed(ErrorResponseContext context)
 {
     Arg.NotNull(context, nameof(context));
     return(new ObjectResult(CreateErrorContent(context))
     {
         StatusCode = Status405MethodNotAllowed
     });
 }
Example #5
0
        /// <inheritdoc />
        public virtual IActionResult CreateResponse(ErrorResponseContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(NewResult(context));
        }
Example #6
0
        /// <summary>
        /// Creates and returns new problem details.
        /// </summary>
        /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to generate response.</param>
        /// <returns>New <see cref="ProblemDetails">problem details</see>.</returns>
        protected virtual ProblemDetails NewProblemDetails(ErrorResponseContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var httpContext = context.Request.HttpContext;
            var factory     = httpContext.RequestServices.GetRequiredService <ProblemDetailsFactory>();

            return(factory.CreateProblemDetails(httpContext, context.StatusCode, context.ErrorCode, default, context.Message, default));
Example #7
0
        /// <summary>
        /// Creates and returns a new error response given the provided context.
        /// </summary>
        /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to generate the response.</param>
        /// <returns>The generated <see cref="IActionResult">response</see>.</returns>
        public virtual IActionResult CreateResponse(ErrorResponseContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(new ObjectResult(CreateErrorContent(context))
            {
                StatusCode = context.StatusCode
            });
        }
Example #8
0
        /// <summary>
        /// Creates the default error content using the given context.
        /// </summary>
        /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to create the error content.</param>
        /// <returns>An <see cref="object"/> representing the error content.</returns>
        protected virtual object CreateErrorContent(ErrorResponseContext context)
        {
            Arg.NotNull(context, nameof(context));
            Contract.Ensures(Contract.Result <object>() != null);

            return(new
            {
                Error = new
                {
                    Code = NullIfEmpty(context.ErrorCode),
                    Message = NullIfEmpty(context.Message),
                    InnerError = NewInnerError(context, c => new { Message = c.MessageDetail }),
                },
            });
        }
Example #9
0
        /// <summary>
        /// Creates the default error content using the given context.
        /// </summary>
        /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to create the error content.</param>
        /// <returns>An <see cref="object"/> representing the error content.</returns>
        protected virtual object CreateErrorContent(ErrorResponseContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(new
            {
                Error = new
                {
                    Code = NullIfEmpty(context.ErrorCode),
                    Message = NullIfEmpty(context.Message),
                    InnerError = NewInnerError(context, c => new { Message = c.MessageDetail }),
                },
            });
        }
Example #10
0
        static TError NewInnerError <TError>(ErrorResponseContext context, Func <ErrorResponseContext, TError> create)
        {
            Contract.Requires(context != null);
            Contract.Requires(create != null);

            if (IsNullOrEmpty(context.MessageDetail))
            {
                return(default(TError));
            }

            var environment = (IHostingEnvironment)context.Request.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment));

            if (environment?.IsDevelopment() == true)
            {
                return(create(context));
            }

            return(default(TError));
        }
Example #11
0
        /// <summary>
        /// Creates the default error content using the given context.
        /// </summary>
        /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to create the error content.</param>
        /// <returns>A <see cref="IDictionary{TKey, TValue}">collection</see> of <see cref="KeyValuePair{TKey, TValue}">key/value pairs</see>
        /// representing the error content.</returns>
        protected virtual IDictionary <string, object> CreateErrorContent(ErrorResponseContext context)
        {
            Arg.NotNull(context, nameof(context));
            Contract.Ensures(Contract.Result <IDictionary <string, object> >() != null);

            var comparer = StringComparer.OrdinalIgnoreCase;
            var error    = new Dictionary <string, object>(comparer);
            var root     = new Dictionary <string, object>(comparer)
            {
                ["Error"] = error
            };

            if (!IsNullOrEmpty(context.ErrorCode))
            {
                error["Code"] = context.ErrorCode;
            }

            if (!IsNullOrEmpty(context.Message))
            {
                error["Message"] = context.Message;
            }

            if (!IsNullOrEmpty(context.MessageDetail))
            {
                var environment = (IHostingEnvironment)context.Request.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment));

                if (environment?.IsDevelopment() == true)
                {
                    error["InnerError"] = new Dictionary <string, object>(comparer)
                    {
                        ["Message"] = context.MessageDetail
                    };
                }
            }

            return(root);
        }
        static object CreateErrorContent(ErrorResponseContext context)
        {
            Contract.Requires(context != null);
            Contract.Ensures(Contract.Result <object>() != null);

            var error = new Dictionary <string, object>();
            var root  = new Dictionary <string, object>()
            {
                ["Error"] = error
            };

            if (!IsNullOrEmpty(context.Code))
            {
                error["Code"] = context.Code;
            }

            if (!IsNullOrEmpty(context.Message))
            {
                error["Message"] = context.Message;
            }

            if (!IsNullOrEmpty(context.MessageDetail))
            {
                var environment = (IHostingEnvironment)context.Request.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment));

                if (environment?.IsDevelopment() == true)
                {
                    error["InnerError"] = new Dictionary <string, object>()
                    {
                        ["Message"] = context.MessageDetail
                    };
                }
            }

            return(root);
        }
 /// <summary>
 /// Creates and returns a new HTTP 400 (Bad Request) given the provided context.
 /// </summary>
 /// <param name="context">The <see cref="ErrorResponseContext">error context</see> used to generate response.</param>
 /// <returns>The generated <see cref="IActionResult">response</see>.</returns>
 public virtual IActionResult BadRequest(ErrorResponseContext context)
 {
     Arg.NotNull(context, nameof(context));
     return(new BadRequestObjectResult(CreateErrorContent(context)));
 }
Example #14
0
 static TError NewInnerError <TError>(ErrorResponseContext context, Func <ErrorResponseContext, TError> create)
 {
     if (IsNullOrEmpty(context.MessageDetail))
     {
         return(default !);
Example #15
0
        protected override IActionResult CreateResult(HttpContext context)
        {
            var errorContext = new ErrorResponseContext(context.Request, Code, Message, messageDetail: null);

            return(Options.ErrorResponses.BadRequest(errorContext));
        }