UnityWebRequestException.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT)
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine.Networking;
  5. namespace Cysharp.Threading.Tasks
  6. {
  7. public class UnityWebRequestException : Exception
  8. {
  9. public UnityWebRequest UnityWebRequest { get; }
  10. #if UNITY_2020_2_OR_NEWER
  11. public UnityWebRequest.Result Result { get; }
  12. #else
  13. public bool IsNetworkError { get; }
  14. public bool IsHttpError { get; }
  15. #endif
  16. public string Error { get; }
  17. public string Text { get; }
  18. public long ResponseCode { get; }
  19. public Dictionary<string, string> ResponseHeaders { get; }
  20. string msg;
  21. public UnityWebRequestException(UnityWebRequest unityWebRequest)
  22. {
  23. this.UnityWebRequest = unityWebRequest;
  24. #if UNITY_2020_2_OR_NEWER
  25. this.Result = unityWebRequest.result;
  26. #else
  27. this.IsNetworkError = unityWebRequest.isNetworkError;
  28. this.IsHttpError = unityWebRequest.isHttpError;
  29. #endif
  30. this.Error = unityWebRequest.error;
  31. this.ResponseCode = unityWebRequest.responseCode;
  32. if (UnityWebRequest.downloadHandler != null)
  33. {
  34. if (unityWebRequest.downloadHandler is DownloadHandlerBuffer dhb)
  35. {
  36. this.Text = dhb.text;
  37. }
  38. }
  39. this.ResponseHeaders = unityWebRequest.GetResponseHeaders();
  40. }
  41. public override string Message
  42. {
  43. get
  44. {
  45. if (msg == null)
  46. {
  47. if(!string.IsNullOrWhiteSpace(Text))
  48. {
  49. msg = Error + Environment.NewLine + Text;
  50. }
  51. else
  52. {
  53. msg = Error;
  54. }
  55. }
  56. return msg;
  57. }
  58. }
  59. }
  60. }
  61. #endif