protected void ErrorHandle(ServiceResponse response)
        {
            // Treats NotModified(Http status code) specially.
            if (response.StatusCode == HttpStatusCode.NotModified)
            {
                throw ExceptionFactory.CreateException(HttpStatusCode.NotModified.ToString(),
                                                       response.Failure.Message, null, null);
            }

            ErrorResult errorResult = null;

            try
            {
                var deserializer = DeserializerFactory.GetFactory().CreateErrorResultDeserializer();
                if (deserializer == null)
                {
                    // Re-throw the web exception if the response cannot be parsed.
                    response.EnsureSuccessful();
                }
                else
                {
                    errorResult = deserializer.Deserialize(response);
                }
            }
            catch (XmlException)
            {
                // Re-throw the web exception if the response cannot be parsed.
                response.EnsureSuccessful();
            }
            catch (InvalidOperationException)
            {
                // Re-throw the web exception if the response cannot be parsed.
                response.EnsureSuccessful();
            }

            // This throw must be out of the try block because otherwise
            // the exception would be caught be the following catch.
            Debug.Assert(errorResult != null);
            throw ExceptionFactory.CreateException(errorResult.Code,
                                                   errorResult.Message,
                                                   errorResult.RequestId,
                                                   errorResult.HostId);
        }
示例#2
0
        /// <summary>
        /// Writes the specified message as error message and then throws the specified exception.
        /// <para />
        /// The specified exception must have a constructor that accepts a single string as message.
        /// </summary>
        /// <typeparam name="TException">The type of the exception.</typeparam>
        /// <param name="log">The log.</param>
        /// <param name="innerException">The inner exception.</param>
        /// <param name="messageFormat">The message format.</param>
        /// <param name="args">The args.</param>
        /// <returns>Exception.</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        /// <exception cref="ArgumentNullException">The <paramref name="log" /> is <c>null</c>.</exception>
        /// <exception cref="NotSupportedException">The <typeparamref name="TException" /> does not have a constructor accepting a string.</exception>
        /// <example>
        ///   <code>
        /// This example logs an error and immediately throws the exception:<para /><![CDATA[
        /// throw Log.ErrorAndCreateException<NotSupportedException>("This action is not supported");
        /// ]]></code>
        /// </example>
        public static Exception ErrorAndCreateException <TException>(this ILog log, Exception innerException, string messageFormat, params object[] args)
            where TException : Exception
        {
            return(ErrorAndCreateException <TException>(log, innerException, msg =>
            {
                var exception = ExceptionFactory.CreateException <TException>(msg, innerException);
                if (exception == null)
                {
                    var error = string.Format("Exception type '{0}' does not have a constructor accepting a string", typeof(TException).Name);

                    if (log != null)
                    {
                        log.Error(error);
                    }

                    throw new NotSupportedException(error);
                }

                return exception;
            }, messageFormat, args));
        }
        public void ExceptionFactoryTest()
        {
            var exception  = ExceptionFactory.CreateException("code", "msg", "id", "host");
            var exception1 = ExceptionFactory.CreateException("code1", "msg1", "id1", "host1", exception);

            Assert.AreEqual(exception1.InnerException, exception);

            try
            {
                ExceptionFactory.CreateInvalidResponseException(exception);
                Assert.IsTrue(false);
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(e.InnerException, exception);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
示例#4
0
        /// <summary>
        /// Writes the bytes data to the UART slave device.
        /// </summary>
        /// <param name="buffer">The byte array that contains the data to write to the device.</param>
        /// <param name="offset">The offset in buffer at which to begin copying bytes.</param>
        /// <param name="count">The number of bytes to write</param>
        public int Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (count > buffer.Length - offset)
            {
                throw new Exception("Can not write more bytes than the buffer holds.");
            }
            byte[] tmpBuffer = new byte[count];
            Array.Copy(buffer, offset, tmpBuffer, 0, count);
            var length = Convert.ToUInt32(count);
            var ret    = NativeUart.Write(_handle, tmpBuffer, length);

            if (ret < Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
            return((int)ret);
        }
示例#5
0
文件: Spi.cs 项目: yunmiha/TizenFX
        /// <summary>
        /// Exchanges the bytes data to the SPI slave device.
        /// writeBuffer.Length and readBuffer.Length must be equal.
        /// </summary>
        /// <param name="writeBuffer">Array containing data to write to the device.</param>
        /// <param name="readBuffer">Array containing data read from the dievice.</param>
        public void TransferSequential(byte[] writeBuffer, byte[] readBuffer)
        {
            if (readBuffer == null)
            {
                throw new ArgumentNullException(nameof(readBuffer));
            }
            if (writeBuffer == null)
            {
                throw new ArgumentNullException(nameof(writeBuffer));
            }
            if (writeBuffer.Length != readBuffer.Length)
            {
                throw new Exception("writeBuffer.Length is not equal to readBuffer.Length");
            }

            var buffersLength = Convert.ToUInt32(writeBuffer.Length);

            var ret = NativeSpi.Transfer(_handle, writeBuffer, readBuffer, buffersLength);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
示例#6
0
            public void ReturnsNullWhenExceptionCannotBeConstructed()
            {
                var exception = ExceptionFactory.CreateException <TestExceptionWithInnerExceptionSupport>(null);

                Assert.IsNull(exception);
            }