FirebaseError.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Net;
  3. namespace SimpleFirebaseUnity
  4. {
  5. public class FirebaseError : Exception
  6. {
  7. const string MESSAGE_ERROR_400 = "Firebase request has invalid child names or invalid/missing/too large data";
  8. const string MESSAGE_ERROR_401 = "Firebase request's authorization has failed";
  9. const string MESSAGE_ERROR_403 = "Firebase request violates Firebase Realtime Database Rules";
  10. const string MESSAGE_ERROR_404 = "Firebase request made over HTTP instead of HTTPS";
  11. const string MESSAGE_ERROR_417 = "Firebase request doesn't specify a Firebase database name";
  12. const string MESSAGE_ERROR_UNDOCUMENTED = "Firebase request's error is not yet documented on Firebase";
  13. protected HttpStatusCode m_Status;
  14. public FirebaseError(HttpStatusCode status, string message) : base(message)
  15. {
  16. m_Status = status;
  17. }
  18. public FirebaseError(HttpStatusCode status, string message, Exception inner) : base(message, inner)
  19. {
  20. m_Status = status;
  21. }
  22. public FirebaseError(string message) : base(message)
  23. {
  24. }
  25. public FirebaseError(string message, Exception inner) : base(message, inner)
  26. {
  27. }
  28. /// <summary>
  29. /// Create the FirebaseError initialized based on the given WebException.
  30. /// </summary>
  31. /// <param name="webEx">Web exception.</param>
  32. public static FirebaseError Create(WebException webEx)
  33. {
  34. string message;
  35. HttpStatusCode status = 0;
  36. bool isStatusAvailable = false;
  37. if (webEx.Status == WebExceptionStatus.ProtocolError)
  38. {
  39. HttpWebResponse response = webEx.Response as HttpWebResponse;
  40. if (response != null)
  41. {
  42. status = response.StatusCode;
  43. isStatusAvailable = true;
  44. }
  45. }
  46. if (!isStatusAvailable)
  47. return new FirebaseError(webEx.Message, webEx);
  48. switch (status)
  49. {
  50. case HttpStatusCode.Unauthorized:
  51. message = MESSAGE_ERROR_401;
  52. break;
  53. case HttpStatusCode.BadRequest:
  54. message = MESSAGE_ERROR_400;
  55. break;
  56. case HttpStatusCode.NotFound:
  57. message = MESSAGE_ERROR_404;
  58. break;
  59. case HttpStatusCode.ExpectationFailed:
  60. message = MESSAGE_ERROR_417;
  61. break;
  62. case HttpStatusCode.Forbidden:
  63. message = MESSAGE_ERROR_403;
  64. break;
  65. default:
  66. message = webEx.Message;
  67. break;
  68. }
  69. return new FirebaseError(status, message, webEx);
  70. }
  71. /// <summary>
  72. /// Create the FirebaseError initialized based on the given http status code.
  73. /// </summary>
  74. /// <param name="status">Http status code.</param>
  75. public static FirebaseError Create(HttpStatusCode status)
  76. {
  77. string message;
  78. switch (status)
  79. {
  80. case HttpStatusCode.Unauthorized:
  81. message = MESSAGE_ERROR_401;
  82. break;
  83. case HttpStatusCode.BadRequest:
  84. message = MESSAGE_ERROR_400;
  85. break;
  86. case HttpStatusCode.NotFound:
  87. message = MESSAGE_ERROR_404;
  88. break;
  89. case HttpStatusCode.ExpectationFailed:
  90. message = MESSAGE_ERROR_417;
  91. break;
  92. case HttpStatusCode.Forbidden:
  93. message = MESSAGE_ERROR_403;
  94. break;
  95. default:
  96. message = MESSAGE_ERROR_UNDOCUMENTED;
  97. break;
  98. }
  99. return new FirebaseError (status, message);
  100. }
  101. /// <summary>
  102. /// Gets the status code.
  103. /// Tips: Typecast to integer to get the code. You could also typecast to string to print as it is.
  104. /// </summary>
  105. /// <value>The status.</value>
  106. public HttpStatusCode Status
  107. {
  108. get{
  109. return m_Status;
  110. }
  111. }
  112. }
  113. }