ReadSoapFault method

Future<SoapFaultDetails?> ReadSoapFault(
  1. EwsServiceXmlReader reader
)
Reads the SOAP fault. The reader.

Implementation

Future<SoapFaultDetails?> ReadSoapFault(EwsServiceXmlReader reader) async {
  SoapFaultDetails? soapFaultDetails = null;

  try {
    await this._ReadXmlDeclaration(reader);

    await reader.Read();
    if (!reader.IsStartElement() ||
        (reader.LocalName != XmlElementNames.SOAPEnvelopeElementName)) {
      return soapFaultDetails;
    }

    // namespace URI from the envelope element and use it for the rest of the parsing.
    // If it's not 1.1 or 1.2, we can't continue.
    XmlNamespace soapNamespace =
        EwsUtilities.GetNamespaceFromUri(reader.NamespaceUri);
    if (soapNamespace == XmlNamespace.NotSpecified) {
      return soapFaultDetails;
    }

    await reader.Read();

    // EWS doesn't always return a SOAP header. If this response contains a header element,
    // read the server version information contained in the header.
    if (reader.IsStartElementWithNamespace(
        soapNamespace, XmlElementNames.SOAPHeaderElementName)) {
      do {
        await reader.Read();

        if (reader.IsStartElementWithNamespace(
            XmlNamespace.Types, XmlElementNames.ServerVersionInfo)) {
          this.Service.ServerInfo = ExchangeServerInfo.Parse(reader);
        }
      } while (!reader.IsEndElementWithNamespace(
          soapNamespace, XmlElementNames.SOAPHeaderElementName));

      // Queue up the next read
      await reader.Read();
    }

    // Parse the fault element contained within the SOAP body.
    if (reader.IsStartElementWithNamespace(
        soapNamespace, XmlElementNames.SOAPBodyElementName)) {
      do {
        await reader.Read();

        // Parse Fault element
        if (reader.IsStartElementWithNamespace(
            soapNamespace, XmlElementNames.SOAPFaultElementName)) {
          soapFaultDetails =
              await SoapFaultDetails.Parse(reader, soapNamespace);
        }
      } while (!reader.IsEndElementWithNamespace(
          soapNamespace, XmlElementNames.SOAPBodyElementName));
    }

    await reader.ReadEndElementWithNamespace(
        soapNamespace, XmlElementNames.SOAPEnvelopeElementName);
  } on XmlException catch (e) {
    // If response doesn't contain a valid SOAP fault, just ignore exception and
    // return null for SOAP fault details.
  }

  return soapFaultDetails;
}