private static void VerifyErrorElementNotFound(ref DuplicateErrorElementPropertyBitMask elementsFoundBitField, DuplicateErrorElementPropertyBitMask elementFoundBitMask, string elementName)
 {
     if ((elementsFoundBitField & elementFoundBitMask) == elementFoundBitMask)
     {
         throw new ODataException(Strings.ODataAtomErrorDeserializer_MultipleErrorElementsWithSameName(elementName));
     }
     elementsFoundBitField |= elementFoundBitMask;
 }
        internal static ODataError ReadErrorElement(BufferingXmlReader xmlReader, int maxInnerErrorDepth)
        {
            ODataError error = new ODataError();
            DuplicateErrorElementPropertyBitMask none = DuplicateErrorElementPropertyBitMask.None;

            if (xmlReader.IsEmptyElement)
            {
                return(error);
            }
            xmlReader.Read();
Label_001A:
            switch (xmlReader.NodeType)
            {
            case XmlNodeType.Element:
                string str;
                if (xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace) && ((str = xmlReader.LocalName) != null))
                {
                    if (!(str == "code"))
                    {
                        if (str == "message")
                        {
                            VerifyErrorElementNotFound(ref none, DuplicateErrorElementPropertyBitMask.Message, "message");
                            error.MessageLanguage = xmlReader.GetAttribute(xmlReader.XmlLangAttributeName, xmlReader.XmlNamespace);
                            error.Message         = xmlReader.ReadElementValue();
                            goto Label_00EA;
                        }
                        if (str == "innererror")
                        {
                            VerifyErrorElementNotFound(ref none, DuplicateErrorElementPropertyBitMask.InnerError, "innererror");
                            error.InnerError = ReadInnerErrorElement(xmlReader, 0, maxInnerErrorDepth);
                            goto Label_00EA;
                        }
                    }
                    else
                    {
                        VerifyErrorElementNotFound(ref none, DuplicateErrorElementPropertyBitMask.Code, "code");
                        error.ErrorCode = xmlReader.ReadElementValue();
                        goto Label_00EA;
                    }
                }
                break;

            case XmlNodeType.EndElement:
                goto Label_00EA;
            }
            xmlReader.Skip();
Label_00EA:
            if (xmlReader.NodeType != XmlNodeType.EndElement)
            {
                goto Label_001A;
            }
            return(error);
        }
        /// <summary>
        /// Verifies that the specified element was not yet found in a top-level error element.
        /// </summary>
        /// <param name="elementsFoundBitField">
        /// The bit field which stores which elements of an error were found so far.
        /// </param>
        /// <param name="elementFoundBitMask">The bit mask for the element to check.</param>
        /// <param name="elementName">The name of the element to check (used for error reporting).</param>
        private static void VerifyErrorElementNotFound(
            ref DuplicateErrorElementPropertyBitMask elementsFoundBitField,
            DuplicateErrorElementPropertyBitMask elementFoundBitMask,
            string elementName)
        {
            Debug.Assert(((int)elementFoundBitMask & (((int)elementFoundBitMask) - 1)) == 0, "elementFoundBitMask is not a power of 2.");
            Debug.Assert(!string.IsNullOrEmpty(elementName), "!string.IsNullOrEmpty(elementName)");

            if ((elementsFoundBitField & elementFoundBitMask) == elementFoundBitMask)
            {
                throw new ODataException(Strings.ODataAtomErrorDeserializer_MultipleErrorElementsWithSameName(elementName));
            }

            elementsFoundBitField |= elementFoundBitMask;
        }
        /// <summary>
        /// Reads the content of an error element.
        /// </summary>
        /// <param name="xmlReader">The Xml reader to read the error payload from.</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of recursive internalexception elements to allow.</param>
        /// <returns>The <see cref="ODataError"/> representing the error.</returns>
        /// <remarks>
        /// This method is used to read top-level errors as well as in-stream errors (from inside the buffering Xml reader).
        /// Pre-Condition:  XmlNodeType.Element   - The m:error start element.
        /// Post-Condition: XmlNodeType.EndElement - The m:error end-element.
        ///                 XmlNodeType.Element    - The empty m:error start element.
        /// </remarks>
        internal static ODataError ReadErrorElement(BufferingXmlReader xmlReader, int maxInnerErrorDepth)
        {
            Debug.Assert(xmlReader != null, "this.XmlReader != null");
            Debug.Assert(xmlReader.NodeType == XmlNodeType.Element, "xmlReader.NodeType == XmlNodeType.Element");
            Debug.Assert(xmlReader.LocalName == AtomConstants.ODataErrorElementName, "Expected reader to be positioned on <m:error> element.");
            Debug.Assert(xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace), "this.XmlReader.NamespaceEquals(atomizedMetadataNamespace)");

            ODataError error = new ODataError();
            DuplicateErrorElementPropertyBitMask elementsReadBitmask = DuplicateErrorElementPropertyBitMask.None;

            if (!xmlReader.IsEmptyElement)
            {
                // Move to the first child node of the element.
                xmlReader.Read();

                do
                {
                    switch (xmlReader.NodeType)
                    {
                    case XmlNodeType.EndElement:
                        // end of the <m:error> element
                        continue;

                    case XmlNodeType.Element:
                        if (xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace))
                        {
                            switch (xmlReader.LocalName)
                            {
                            // <m:code>
                            case AtomConstants.ODataErrorCodeElementName:
                                VerifyErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateErrorElementPropertyBitMask.Code,
                                    AtomConstants.ODataErrorCodeElementName);
                                error.ErrorCode = xmlReader.ReadElementValue();
                                continue;

                            // <m:message >
                            case AtomConstants.ODataErrorMessageElementName:
                                VerifyErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateErrorElementPropertyBitMask.Message,
                                    AtomConstants.ODataErrorMessageElementName);
                                error.Message = xmlReader.ReadElementValue();
                                continue;

                            // <m:innererror>
                            case AtomConstants.ODataInnerErrorElementName:
                                VerifyErrorElementNotFound(
                                    ref elementsReadBitmask,
                                    DuplicateErrorElementPropertyBitMask.InnerError,
                                    AtomConstants.ODataInnerErrorElementName);
                                error.InnerError = ReadInnerErrorElement(xmlReader, 0 /* recursionDepth */, maxInnerErrorDepth);
                                continue;

                            default:
                                break;
                            }
                        }

                        break;

                    default:
                        break;
                    }

                    xmlReader.Skip();
                }while (xmlReader.NodeType != XmlNodeType.EndElement);
            }

            return(error);
        }
        /// <summary>
        /// Verifies that the specified element was not yet found in a top-level error element.
        /// </summary>
        /// <param name="elementsFoundBitField">
        /// The bit field which stores which elements of an error were found so far.
        /// </param>
        /// <param name="elementFoundBitMask">The bit mask for the element to check.</param>
        /// <param name="elementName">The name of the element to check (used for error reporting).</param>
        private static void VerifyErrorElementNotFound(
            ref DuplicateErrorElementPropertyBitMask elementsFoundBitField,
            DuplicateErrorElementPropertyBitMask elementFoundBitMask,
            string elementName)
        {
            Debug.Assert(((int)elementFoundBitMask & (((int)elementFoundBitMask) - 1)) == 0, "elementFoundBitMask is not a power of 2.");
            Debug.Assert(!string.IsNullOrEmpty(elementName), "!string.IsNullOrEmpty(elementName)");

            if ((elementsFoundBitField & elementFoundBitMask) == elementFoundBitMask)
            {
                throw new ODataException(Strings.ODataAtomErrorDeserializer_MultipleErrorElementsWithSameName(elementName));
            }

            elementsFoundBitField |= elementFoundBitMask;
        }
 private static void VerifyErrorElementNotFound(ref DuplicateErrorElementPropertyBitMask elementsFoundBitField, DuplicateErrorElementPropertyBitMask elementFoundBitMask, string elementName)
 {
     if ((elementsFoundBitField & elementFoundBitMask) == elementFoundBitMask)
     {
         throw new ODataException(Strings.ODataAtomErrorDeserializer_MultipleErrorElementsWithSameName(elementName));
     }
     elementsFoundBitField |= elementFoundBitMask;
 }