Esempio n. 1
0
        static HttpError CreateWebApiError(ErrorResponseContext context)
        {
            Contract.Requires(context != null);
            Contract.Ensures(Contract.Result <HttpError>() != null);

            var error = new HttpError();
            var root  = new HttpError()
            {
                ["Error"] = error
            };

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

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

            if (!IsNullOrEmpty(context.MessageDetail) && context.Request.ShouldIncludeErrorDetail() == true)
            {
                error["InnerError"] = new HttpError(context.MessageDetail);
            }

            return(root);
        }
        /// <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="HttpError">HTTP error</see> representing the error content.</returns>
        protected virtual HttpError CreateErrorContent(ErrorResponseContext context)
        {
            Arg.NotNull(context, nameof(context));
            Contract.Ensures(Contract.Result <HttpError>() != null);

            return(IsODataRequest(context) ? CreateODataError(context) : CreateWebApiError(context));
        }
Esempio n. 3
0
        static HttpResponseMessage CreateErrorResponse(ErrorResponseContext context, HttpStatusCode statusCode)
        {
            Contract.Requires(context != null);
            Contract.Ensures(Contract.Result <HttpResponseMessage>() != null);

            var error = IsODataRequest(context) ? CreateODataError(context) : CreateWebApiError(context);

            return(context.Request.CreateErrorResponse(statusCode, error));
        }
Esempio n. 4
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 response.</param>
        /// <returns>The generated <see cref="HttpResponseMessage">response</see>.</returns>
        public virtual HttpResponseMessage CreateResponse(ErrorResponseContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(context.Request.CreateErrorResponse(context.StatusCode, CreateErrorContent(context)));
        }
Esempio n. 5
0
        static bool IsODataRequest(ErrorResponseContext context)
        {
            Contract.Requires(context != null);

            var request     = context.Request;
            var routeValues = request.GetRouteData();

            if (routeValues == null)
            {
                return(false);
            }

            if (!routeValues.Values.ContainsKey("odataPath"))
            {
                return(false);
            }

            return(request.GetConfiguration()?.Formatters.JsonFormatter == null);
        }
Esempio n. 6
0
        static HttpError CreateODataError(ErrorResponseContext context)
        {
            var error = new HttpError();

            if (!IsNullOrEmpty(context.ErrorCode))
            {
                error[HttpErrorKeys.ErrorCodeKey] = context.ErrorCode;
            }

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

            if (!IsNullOrEmpty(context.MessageDetail) && context.Request.ShouldIncludeErrorDetail() == true)
            {
                error[HttpErrorKeys.MessageDetailKey] = context.MessageDetail;
            }

            return(error);
        }
Esempio n. 7
0
        static bool IsODataRequest(ErrorResponseContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var request     = context.Request;
            var routeValues = request.GetRouteData();

            if (routeValues == null)
            {
                return(false);
            }

            if (!routeValues.Values.ContainsKey("odataPath"))
            {
                return(false);
            }

            return(request.GetConfiguration()?.Formatters.JsonFormatter == null);
        }
Esempio n. 8
0
 static HttpResponseMessage CreateODataBadRequest(ErrorResponseContext context) =>
 context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, CreateODataError(context));
Esempio n. 9
0
 /// <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="HttpResponseMessage">response</see>.</returns>
 public virtual HttpResponseMessage MethodNotAllowed(ErrorResponseContext context)
 {
     Arg.NotNull(context, nameof(context));
     return(CreateErrorResponse(context, HttpStatusCode.MethodNotAllowed));
 }
Esempio n. 10
0
 /// <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="HttpResponseMessage">response</see>.</returns>
 public virtual HttpResponseMessage BadRequest(ErrorResponseContext context)
 {
     Arg.NotNull(context, nameof(context));
     return(CreateErrorResponse(context, HttpStatusCode.BadRequest));
 }
 /// <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 response.</param>
 /// <returns>The generated <see cref="HttpResponseMessage">response</see>.</returns>
 public virtual HttpResponseMessage CreateResponse(ErrorResponseContext context)
 {
     Arg.NotNull(context, nameof(context));
     return(context.Request.CreateErrorResponse(context.StatusCode, CreateErrorContent(context)));
 }
Esempio n. 12
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="HttpError">HTTP error</see> representing the error content.</returns>
 protected virtual HttpError CreateErrorContent(ErrorResponseContext context) =>
 IsODataRequest(context) ? CreateODataError(context) : CreateWebApiError(context);