ErrorResponseHandler.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Diagnostics;
  9. using System.Net;
  10. using System.Xml;
  11. using Aliyun.OSS.Common.Communication;
  12. using Aliyun.OSS.Common.Handlers;
  13. using Aliyun.OSS.Model;
  14. using Aliyun.OSS.Transform;
  15. namespace Aliyun.OSS.Util
  16. {
  17. internal class ErrorResponseHandler : ResponseHandler
  18. {
  19. public override void Handle(ServiceResponse response)
  20. {
  21. base.Handle(response);
  22. if (response.IsSuccessful())
  23. return;
  24. // Treats NotModified(Http status code) specially.
  25. if (response.StatusCode == HttpStatusCode.NotModified)
  26. {
  27. throw ExceptionFactory.CreateException(HttpStatusCode.NotModified.ToString(),
  28. response.Failure.Message, null, null);
  29. }
  30. ErrorResult errorResult = null;
  31. try
  32. {
  33. var deserializer = DeserializerFactory.GetFactory().CreateErrorResultDeserializer();
  34. if (deserializer == null)
  35. {
  36. // Re-throw the web exception if the response cannot be parsed.
  37. response.EnsureSuccessful();
  38. }
  39. else
  40. {
  41. errorResult = deserializer.Deserialize(response);
  42. }
  43. }
  44. catch (XmlException)
  45. {
  46. // Re-throw the web exception if the response cannot be parsed.
  47. response.EnsureSuccessful();
  48. }
  49. catch (InvalidOperationException)
  50. {
  51. // Re-throw the web exception if the response cannot be parsed.
  52. response.EnsureSuccessful();
  53. }
  54. // This throw must be out of the try block because otherwise
  55. // the exception would be caught be the following catch.
  56. Debug.Assert(errorResult != null);
  57. throw ExceptionFactory.CreateException(errorResult.Code,
  58. errorResult.Message,
  59. errorResult.RequestId,
  60. errorResult.HostId);
  61. }
  62. }
  63. }