Esempio n. 1
0
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Exception.GetType() != typeof(BadFormatException))
            {
                var errorTag = ErrorTagGenerator.NewErrorTag();

                actionExecutedContext.Response = actionExecutedContext.Request.CreateInternalServerErrorResponseMessage(
                    "An unhandeled exception of type " + actionExecutedContext.Exception.GetType() +
                    " occured.", errorTag);

                Logger.Error(
                    string.Format("Internal server error: {0} [ITAG:{1}]", actionExecutedContext.Exception.GetType(),
                                  errorTag),
                    actionExecutedContext.Exception);

                base.OnException(actionExecutedContext);
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="request"></param>
        /// <param name="errorMessage">Message</param>
        /// <param name="httpStatusCode">The status code to return, ands to set at 'ErrorCode' on the returned <see cref="Error"/>-object</param>
        /// <param name="errorTag">Unique tag to identify the error. If left as defualt null or string.Empty one will be generated</param>
        /// <param name="errorCode">A code for this type of error. If left default null or string.Empty the value (int)<see cref="httpStatusCode"/> will be used</param>
        /// <returns></returns>
        private static Error GetErrorMessage(this HttpRequestMessage request, string errorMessage, HttpStatusCode httpStatusCode, string errorTag = null, string errorCode = null)
        {
            if (string.IsNullOrEmpty(errorTag))
            {
                errorTag = ErrorTagGenerator.NewErrorTag();
            }

            if (string.IsNullOrEmpty(errorCode))
            {
                errorCode = ((int)httpStatusCode).ToString();
            }

            var error = new Error(errorMessage, errorTag, errorCode)
            {
                HttpStatusCode = SetStatusToOkIfRequested(request, httpStatusCode), Time = DateTime.Now.ToString()
            };

            return(error);
        }
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var acceptHeader = request.Headers.Accept;

            if (!IsRequestedMediaTypeAccepted(acceptHeader))
            {
                return(Task <HttpResponseMessage> .Factory.StartNew(() => request.CreateNotAcceptableResponseMessage("The requested content type specified in the Accept header is not valid.", ErrorTagGenerator.NewErrorTag(), "")));
            }

            return(base.SendAsync(request, cancellationToken));
        }
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Exception.GetType() == typeof(BadFormatException))
            {
                var errorMessage = actionExecutedContext.Exception.Message;

                actionExecutedContext.Response = actionExecutedContext.Request.CreateBadRequestErrorResponseMessage(errorMessage, ErrorTagGenerator.NewErrorTag(), ((int)HttpStatusCode.BadRequest).ToString());
            }

            base.OnException(actionExecutedContext);
        }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var stateErrors = GetModelStateErrors(actionContext);

            if (stateErrors != null)
            {
                var stateErrorMessage = string.Join(",", stateErrors);
                var errorMessage      = "The request was badly formatted and had the following errors: " + stateErrorMessage +
                                        " Use the error tag when contacting Norsk Rikstoto for further information.";

                actionContext.Response = actionContext.Request.CreateBadRequestErrorResponseMessage(errorMessage, ErrorTagGenerator.NewErrorTag(), ((int)HttpStatusCode.BadRequest).ToString());
            }
        }