/// <summary>
        /// Instead of returning whole error page, when error is thrown, this method creates response with status code 500 and sets the response content to <see cref="ContractException"/>,
        /// where code is set to GenericError and description is set to error message.
        /// </summary>
        /// <param name="context">Exception context</param>
        public override void OnException(ExceptionContext context)
        {
            base.OnException(context);

            var logger       = context.HttpContext.RequestServices.GetService <ILogger <ApiExceptionFilterAttribute> >();
            var localization = context.HttpContext.RequestServices.GetService <ILocalizationService>();

            if (logger.IsEnabled(LogLevel.Error))
            {
                logger.LogError(context.Exception, "Unhandled exception");
            }

            var contractException = new ContractException
            {
                Code = DataResultErrorCode.GenericError,
                //Description = context.Exception.Message, // Don't use internal technical message
                Description = localization.Translate("unknown-error-msg", "error"),
            };

            context.Result = new ObjectResult(contractException)
            {
                StatusCode = 500
            };

            context.ExceptionHandled = true;
        }
 public static void Range(uint actual, uint inclusiveMin, uint exclusiveMax)
 {
     if (!(actual >= inclusiveMin) || !(actual < exclusiveMax))
     {
         throw ContractException.GenerateException(actual, inclusiveMin, exclusiveMax);
     }
 }
        public static void NotEqualTo <T>(T actual, T expected)
        {
            var equality = EqualityComparer <T> .Default;

            if (equality.Equals(actual, expected))
            {
                throw ContractException.GenerateException(actual, expected);
            }
        }
        void ISafeSerializationData.CompleteDeserialization(object obj)
        {
            // This is called when the exception has been deserialized, and tells
            // this exception data object to push its value back into the deserialized
            // exception object.
            ContractException exception = obj as ContractException;

            exception.m_data = this;
        }
Beispiel #5
0
        public void DefaultConstructorWorks()
        {
            var ex = new ContractException(ContractFailureKind.Assert, "Contract failed", null, null, null);

            Assert.True((object)ex is ContractException, "is ContractException");
            Assert.True(ex.Kind == ContractFailureKind.Assert, "ContractFailureKind");
            Assert.True(ex.InnerException == null, "InnerException");
            Assert.True(ex.Condition == null, "Condition");
            Assert.True(ex.UserMessage == null, "UserMessage");
            Assert.AreEqual("Contract failed", ex.Message);
        }
Beispiel #6
0
        public void TypePropertiesAreCorrect()
        {
            Assert.AreEqual("System.Diagnostics.Contracts.ContractException", typeof(ContractException).FullName, "Name");
            Assert.True(typeof(ContractException).IsClass, "IsClass");
            Assert.AreEqual(typeof(Exception), typeof(ContractException).BaseType, "BaseType");
            object d = new ContractException(ContractFailureKind.Assert, "Contract failed", null, null, null);

            Assert.True(d is ContractException, "is ContractException");
            Assert.True(d is Exception, "is Exception");

            var interfaces = typeof(ContractException).GetInterfaces();

            Assert.AreEqual(0, interfaces.Length, "Interfaces length");
        }
Beispiel #7
0
        public void Lambda()
        {
            int a = 12;
            int b = 18;
            ContractException e = null;

            try
            {
                Contract.Assert(() => a > b);
                Assert.Fail("expected exception not thrown");
            }
            catch (ContractException ex)
            {
                e = ex;
            }
            Assert.That(e.Message, Contains.Substring("12 > 18"));
        }
        public void ContractExceptionShouldThrowCorrectly()
        {
            ContractException ex      = new ContractException("Test message");
            Action            exThrow = () => throw ex;

            exThrow.Should().Throw <ContractException>()
            .WithMessage("Test Message");

            // should serailize
            MemoryStream    mem = new MemoryStream();
            BinaryFormatter b   = new BinaryFormatter();

            Action serialize = () => b.Serialize(mem, ex);

            serialize.Should().NotThrow();

            // deserialize
            mem.Seek(0, SeekOrigin.Begin);
            ContractException deserializedEx = null;
            Action            deserialize    = () => deserializedEx = (ContractException)b.Deserialize(mem);

            deserialize.Should().NotThrow();
            deserializedEx.Message.Should().Be(ex.Message);
        }
Beispiel #9
0
            void ISafeSerializationData.CompleteDeserialization(object obj)
            {
                ContractException ex = obj as ContractException;

                ex.m_data = this;
            }