public void DiscriminatorNotAvailableException_default_ctor()
        {
            // Arrange
            const string expectedMessage = "Exception of type 'OnvifDiscovery.Exceptions.DiscoveryException' was thrown.";

            // Act
            var sut = new DiscoveryException();

            // Assert
            Assert.Null(sut.InnerException);
            Assert.Equal(expectedMessage, sut.Message);
        }
        public void DiscriminatorNotAvailableException_ctor_string()
        {
            // Arrange
            const string expectedMessage = "message";

            // Act
            var sut = new DiscoveryException(expectedMessage);

            // Assert
            Assert.Null(sut.InnerException);
            Assert.Equal(expectedMessage, sut.Message);
        }
        public void DiscriminatorNotAvailableException_serialization_deserialization_test()
        {
            // Arrange
            var innerEx           = new Exception("foo");
            var originalException = new DiscoveryException("message", innerEx);
            var buffer            = new byte[4096];
            var ms        = new MemoryStream(buffer);
            var ms2       = new MemoryStream(buffer);
            var formatter = new BinaryFormatter();

            // Act
            formatter.Serialize(ms, originalException);
            var deserializedException = (DiscoveryException)formatter.Deserialize(ms2);

            // Assert
            Assert.Equal(originalException.InnerException.Message, deserializedException.InnerException.Message);
            Assert.Equal(originalException.Message, deserializedException.Message);
        }
        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            if (exception is DiscoveryException && exception != null)
            {
                //only attempt to send an email if we are handling a DiscoveryException, otherwise we will be
                //unable to find the email address

                DiscoveryException discoveryException = (DiscoveryException)exception;

                //this email handler has been related to the exception so we will email someone if we
                //can find the email address from the DB using the exception type, the policy name and the opcocode

                //check the properties collection of the DiscoveryException object

                try
                {
                    int    opCoId        = Convert.ToInt32(discoveryException.Properties["OpCoId"]);
                    string operatorEmail = discoveryException.Properties["OperatorEmail"].ToString();
                    string exceptionType = exception.GetType().ToString();

                    //use OpCoId + exceptionType + Policy (name) to look in the database and see if
                    //there is an email address to send the exception message to

                    ErrorType errorType = ErrorTypeController.GetErrorType(exceptionType, opCoId, Policy);

                    if (errorType != null)
                    {
                        //get from object
                        bool   emailOperator = errorType.EmailOperator;
                        string toAddress     = errorType.EmailRecipients;

                        if (emailOperator & !string.IsNullOrEmpty(operatorEmail))
                        {
                            if (!string.IsNullOrEmpty(toAddress))
                            {
                                //add the operator email to the recipient list if there is a list and
                                //there is a operatorEmail
                                toAddress = string.Concat(toAddress, ";", operatorEmail);
                            }
                            else
                            {
                                //set the toaddress to be the operatorEmail if there isn't a list retrived from the DB and
                                //there is a operatorEmail
                                toAddress = operatorEmail;
                            }
                        }

                        if (!string.IsNullOrEmpty(toAddress))
                        {
                            //get from config file?
                            string emailServerName = "tigger";
                            string fromAddress     = "*****@*****.**";
                            int    smtpPort        = 25;
                            string emailSubject    = "Test Subject";
                            //get from exception
                            string      emailBody  = exception.Message;
                            SmtpClient  mailClient = new SmtpClient(emailServerName, smtpPort);
                            MailMessage message    = new MailMessage(toAddress, fromAddress, emailSubject, emailBody);
                            mailClient.Send(message);
                        }
                    }
                }
                catch (Exception e)
                {
                }
            }

            return(exception);
        }