ControlErrorHandler.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.IO;
  5. using System.Text;
  6. using System.Xml;
  7. using Emby.Dlna.Didl;
  8. namespace Emby.Dlna.Service
  9. {
  10. public static class ControlErrorHandler
  11. {
  12. private const string NS_SOAPENV = "http://schemas.xmlsoap.org/soap/envelope/";
  13. public static ControlResponse GetResponse(Exception ex)
  14. {
  15. var settings = new XmlWriterSettings
  16. {
  17. Encoding = Encoding.UTF8,
  18. CloseOutput = false
  19. };
  20. StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8);
  21. using (var writer = XmlWriter.Create(builder, settings))
  22. {
  23. writer.WriteStartDocument(true);
  24. writer.WriteStartElement("SOAP-ENV", "Envelope", NS_SOAPENV);
  25. writer.WriteAttributeString(string.Empty, "encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/");
  26. writer.WriteStartElement("SOAP-ENV", "Body", NS_SOAPENV);
  27. writer.WriteStartElement("SOAP-ENV", "Fault", NS_SOAPENV);
  28. writer.WriteElementString("faultcode", "500");
  29. writer.WriteElementString("faultstring", ex.Message);
  30. writer.WriteStartElement("detail");
  31. writer.WriteRaw("<UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>401</errorCode><errorDescription>Invalid Action</errorDescription></UPnPError>");
  32. writer.WriteFullEndElement();
  33. writer.WriteFullEndElement();
  34. writer.WriteFullEndElement();
  35. writer.WriteFullEndElement();
  36. writer.WriteEndDocument();
  37. }
  38. return new ControlResponse
  39. {
  40. Xml = builder.ToString(),
  41. IsSuccessful = false
  42. };
  43. }
  44. }
  45. }