Пример #1
0
        private Exception ReadException()
        {
            switch (ReplyStatus)
            {
            case ReplyStatus.UserException:
            {
                var istr = new InputStream(_communicator, Payload, 1);
                istr.StartEncapsulation();
                RemoteException ex = istr.ReadException();
                istr.EndEncapsulation();
                return(ex);
            }

            case ReplyStatus.ObjectNotExistException:
            case ReplyStatus.FacetNotExistException:
            case ReplyStatus.OperationNotExistException:
            {
                return(ReadDispatchException());
            }

            default:
            {
                Debug.Assert(ReplyStatus == ReplyStatus.UnknownException ||
                             ReplyStatus == ReplyStatus.UnknownLocalException ||
                             ReplyStatus == ReplyStatus.UnknownUserException);
                return(ReadUnhandledException());
            }
            }
        }
Пример #2
0
        /// <summary>Writes a remote exception to the stream.</summary>
        /// <param name="v">The remote exception to write.</param>
        public void WriteException(RemoteException v)
        {
            Debug.Assert(InEncapsulation && _current == null);
            Debug.Assert(_format == FormatType.Sliced);
            Debug.Assert(!(v is ObjectNotExistException));    // temporary
            Debug.Assert(!(v is OperationNotExistException)); // temporary
            Debug.Assert(!(v is UnhandledException));         // temporary
            Push(InstanceType.Exception);

            v.Write(this);
            Pop(null);
        }
Пример #3
0
        /// <summary>Reads a remote exception from the stream.</summary>
        /// <returns>The remote exception.</returns>
        public RemoteException ReadException()
        {
            Push(InstanceType.Exception);
            Debug.Assert(_current != null);

            // Read the first slice header, and exception's type ID cannot be null.
            string typeId = ReadSliceHeaderIntoCurrent() !;

            ReadIndirectionTableIntoCurrent(); // we read the indirection table immediately

            while (true)
            {
                RemoteException?remoteEx = null;
                Type?           type     = Communicator.ResolveClass(typeId);
                if (type != null)
                {
                    try
                    {
                        remoteEx = (RemoteException?)Activator.CreateInstance(type);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidDataException(
                                  @$ "failed to create an instance of type `{type.Name
                            }' while reading a remote exception with type ID `{typeId}'", ex);
                    }
                }

                // We found the exception.
                if (remoteEx != null)
                {
                    remoteEx.ConvertToUnhandled = true;
                    remoteEx.Read(this);
                    Pop(null);
                    return(remoteEx);
                }

                // Slice off what we don't understand.
                SkipSlice();

                if ((_current.SliceFlags & EncodingDefinitions.SliceFlags.IsLastSlice) != 0)
                {
                    // Create and throw a plain RemoteException with the SlicedData.
                    Debug.Assert(SlicedData != null);
                    remoteEx = new RemoteException(SlicedData.Value);
                    remoteEx.ConvertToUnhandled = true;
                    return(remoteEx);
                }

                typeId = ReadSliceHeaderIntoCurrent() !;
                ReadIndirectionTableIntoCurrent();
            }
Пример #4
0
        /// <summary>Creates a response frame that represents "failure" and contains an exception.</summary>
        /// <param name="current">The current parameter holds decoded header data and other information about the
        /// request for which this constructor creates a response.</param>
        /// <param name="exception">The exception to store into the frame's payload.</param>
        public OutgoingResponseFrame(Current current, RemoteException exception)
            : this(current)
        {
            if (exception is RequestFailedException requestFailedException)
            {
                if (requestFailedException is DispatchException dispatchException)
                {
                    // TODO: the null checks are necessary due to the way we unmarshal the exception through reflection.

                    if (dispatchException.Id.Name == null || dispatchException.Id.Name.Length == 0)
                    {
                        dispatchException.Id = current.Id;
                    }

                    if (dispatchException.Facet == null || dispatchException.Facet.Length == 0)
                    {
                        dispatchException.Facet = current.Facet;
                    }

                    if (dispatchException.Operation == null || dispatchException.Operation.Length == 0)
                    {
                        dispatchException.Operation = current.Operation;
                    }

                    ReplyStatus replyStatus = default;

                    if (dispatchException is ObjectNotExistException)
                    {
                        replyStatus = ReplyStatus.ObjectNotExistException;
                    }
                    else if (dispatchException is OperationNotExistException)
                    {
                        replyStatus = ReplyStatus.OperationNotExistException;
                    }
                    else
                    {
                        Debug.Assert(false);
                    }

                    WriteByte((byte)replyStatus);
                    dispatchException.Id.IceWrite(this);

                    // For compatibility with the old FacetPath.
                    if (dispatchException.Facet == null || dispatchException.Facet.Length == 0)
                    {
                        WriteStringSeq(Array.Empty <string>());
                    }
                    else
                    {
                        WriteStringSeq(new string[] { dispatchException.Facet });
                    }
                    WriteString(dispatchException.Operation);
                }
                else
                {
                    WriteByte((byte)ReplyStatus.UnknownLocalException);
                    if (requestFailedException.IceMessage.Length > 0)
                    {
                        WriteString(requestFailedException.IceMessage);
                    }
                    else
                    {
                        WriteString(requestFailedException.ToString());
                    }
                }
            }
            else
            {
                WriteByte((byte)ReplyStatus.UserException);
                StartEncapsulation(current.Encoding, FormatType.SlicedFormat);
                WriteException(exception);
                EndEncapsulation();
            }
        }