public void ShouldRethrowExceptionInActionWhenItIsAWebFaultExceptionWithString()
        {
            var expectedException = new WebFaultException<string>(string.Empty, HttpStatusCode.BadRequest);

            WebFaultException<string> returnedException = _webFaultExceptionCreator.CreateWebFaultException(expectedException, string.Empty);

            Assert.AreEqual(expectedException, returnedException, "Expected WebFaultException<string> was not rethrown.");
        }
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var operationAction = OperationContext.Current.IncomingMessageHeaders.Action;

            if (operationAction == null)
            {
                var requestBinding = OperationContext.Current.EndpointDispatcher.ChannelDispatcher.BindingName;
                if (requestBinding.Contains("WebHttpBinding"))
                    operationAction = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
            }

            DispatchOperation operation = OperationContext.Current
                                               .EndpointDispatcher
                                               .DispatchRuntime
                                               .Operations
                                               .FirstOrDefault(o => o.Action == operationAction);

            if (_useJsonFault)
            {
                var details = error.InnerException != null
                    ? "InnerException available, see server logs for further details"
                    : string.Empty;

                var webHttpError = new WebHttpError { Error = "Unknown Exception", Detail = details };

                var webFaultException = new WebFaultException<WebHttpError>(webHttpError,
                    HttpStatusCode.InternalServerError);

                fault = Message.CreateMessage(version, operationAction, webFaultException);
            }
            else
            {
                var detail = error as FaultException;

                var faultDetail = detail ?? new FaultException(new FaultReason("Unknown Exception"), FaultCode.CreateReceiverFaultCode("Unhandled", ""));

                fault = Message.CreateMessage(version, operationAction, faultDetail, new DataContractJsonSerializer(faultDetail.GetType()));
            }

            var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);

            fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);

            var rmp = new HttpResponseMessageProperty
            {
                StatusCode = HttpStatusCode.InternalServerError,

                StatusDescription = "See server logs for further details"
            };

            rmp.Headers[HttpResponseHeader.ContentType] = "application/json";

            fault.Properties.Add(HttpResponseMessageProperty.Name, rmp);

            if (WebOperationContext.Current != null)
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
        }
Exemple #3
0
        public void ShouldThrowWebFaultExceptionWhenRePublishThrows()
        {
            var expectedException = new WebFaultException<string>(null, HttpStatusCode.Accepted);
            MockedPackagePublisher.Setup(pp => pp.PublishPackage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<PackageLogAction>()))
                .Throws(new Exception());
            MockedWebFaultExceptionFactory.Setup(wfef => wfef.CreateWebFaultException(It.IsAny<Exception>(), It.IsAny<string>())).Returns(expectedException);

            TestDelegate methodThatShouldThrow = () => PackagePublishingService.RePublish(null, null, null);

            ExceptionAssert.Throws(methodThatShouldThrow, expectedException, "Incorrect exception thrown.");
        }
        /// <summary>Creates a fault message from the specified parameters.</summary>
        /// <param name="fex">The fault exception to create a message from.</param>
        /// <param name="version">The SOAP version of the message.</param>
        /// <returns>A fault message.</returns>
        protected override Message CreateMessage(FaultException fex, MessageVersion version)
        {
            var fexd=fex as WebFaultException<Ows100.ExceptionReport>;
            if (fexd==null)
            {
                var oex=new OwsException(OwsExceptionCode.NoApplicableCode, fex);
                fexd=new WebFaultException<Ows100.ExceptionReport>((Ows100.ExceptionReport)oex, HttpStatusCode.InternalServerError);
            }

            return WebOperationContext.Current.CreateXmlResponse<Ows100.ExceptionReport>(fexd.Detail);
        }
        public override Exception HandlerException(Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation input, Exception exception)
        {
            //if (exception is GenericException)
            //    exception = exception;

            if (exception is WebFaultException<ExceptionDetail>)
                throw exception;

            if (!(exception is GenericException))
                exception = new GenericException(exception);

            ExceptionDetail detail = new ExceptionDetail(exception);

            var result = new WebFaultException<ExceptionDetail>(detail, HttpStatusCode.BadRequest);
            throw result;
        }
        // This method validates users. It allows in two users,
        // test1 and test2 with passwords 1tset and 2tset respectively.
        // This code is for illustration purposes only and
        // MUST NOT be used in a production environment because it
        // is NOT secure.
        public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }

            if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset"))
            {
                //  throw new SecurityTokenException("Unknown Username or Password");

                // throw new ArgumentException();
                //throw new SecurityTokenValidationException("yee haww");
                //  throw new FaultException("Unknown Username or Incorrect Password");
                var rejectEx = new WebFaultException(HttpStatusCode.Unauthorized);
                rejectEx.Data.Add("HttpStatusCode", rejectEx.StatusCode);
                throw rejectEx;
            }
        }
        public override Exception HandlerException(Microsoft.Practices.Unity.InterceptionExtension.IMethodInvocation input, Exception exception)
        {
            if (exception is WebFaultException<ExceptionDetail>)
            {

                string note = FormmatException(exception.StackTrace,exception.Message);
                ExceptionLog.Write(note);
                throw exception;
            }
            if (!(exception is GenericException))
            {
                exception = new GenericException(exception);
                string notepad = FormmatException(exception.StackTrace, exception.Message);
                ExceptionLog.Write(notepad);
            }
            ExceptionDetail detail = new ExceptionDetail(exception);

            var result = new WebFaultException<ExceptionDetail>(detail, HttpStatusCode.BadRequest);

            throw result;
        }
        private void ThrowRejection(HttpStatusCode status, string message)
        {
            var rejectEx = new WebFaultException<String>(message, status);
            rejectEx.Data.Add("HttpStatusCode", rejectEx.StatusCode);

            //this exception is expected, as it triggers the authentication
            //to ignore this error while running in VS debugger uncheck the "break when this exception is user handled" box
            throw rejectEx;
        }
Exemple #9
0
        /// <summary>Creates a fault message from the specified exception.</summary>
        /// <param name="error">The exception that has been raised.</param>
        /// <param name="version">The SOAP version of the message.</param>
        /// <param name="fault">The fault message that is returned to the client.</param>
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var aex=error as AggregateException;
            if (aex!=null)
                if (aex.InnerExceptions.Count>1)
                    error=new OwsException(OwsExceptionCode.NoApplicableCode, _Reason, error);
                else
                    error=aex.InnerExceptions[0];

            var fex=error as FaultException;
            if (fex==null)
            {
                if (error is NotImplementedException)
                    error=new OwsException(OwsExceptionCode.OperationNotSupported, _Reason, error);

                var oex=error as OwsException;
                if (oex!=null)
                {
                    // [OGC 06-121r9, §8.6]
                    var status=HttpStatusCode.InternalServerError;
                    switch (oex.Code)
                    {
                    case OwsExceptionCode.InvalidParameterValue:
                    case OwsExceptionCode.InvalidUpdateSequence:
                    case OwsExceptionCode.MissingParameterValue:
                        status=HttpStatusCode.BadRequest;
                        break;
                    case OwsExceptionCode.OperationNotSupported:
                    case OwsExceptionCode.OptionNotSupported:
                        status=HttpStatusCode.NotImplemented;
                        break;
                    }
                    fex=new WebFaultException<Ows100.ExceptionReport>((Ows100.ExceptionReport)oex, status);
                } else
                {
                    FaultCode fc=FaultCode.CreateReceiverFaultCode("InternalServerError", "http://schemas.microsoft.com/2009/WebFault");
                    fex=new FaultException(_Reason, fc);
                }
            }

            fault=CreateMessage(fex, version);
        }
 private void InitializeFromWebFaultException(WebFaultException webFaultException)
 {
     this.StatusCode = webFaultException.StatusCode;
     this.DetailObject = null;
     this.DetailType = null;
 }
        public void ShouldThrowWithGivenStatusCodeWhenGivenWebFaultException()
        {
            const HttpStatusCode expectedCode = HttpStatusCode.BadGateway;
            WebFaultException originalException = new WebFaultException(expectedCode);

            WebFaultException<string> webFaultException = _webFaultExceptionCreator.CreateWebFaultException(originalException, string.Empty);

            Assert.AreEqual(expectedCode, webFaultException.StatusCode, "Incorrect status code returned.");
        }