google_service_auth_error.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "google_apis/gaia/google_service_auth_error.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/check_op.h"
  9. #include "base/notreached.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "net/base/net_errors.h"
  12. namespace {
  13. const char* InvalidCredentialsReasonToString(
  14. GoogleServiceAuthError::InvalidGaiaCredentialsReason reason) {
  15. using InvalidGaiaCredentialsReason =
  16. GoogleServiceAuthError::InvalidGaiaCredentialsReason;
  17. switch (reason) {
  18. case InvalidGaiaCredentialsReason::UNKNOWN:
  19. return "unknown";
  20. case InvalidGaiaCredentialsReason::CREDENTIALS_REJECTED_BY_SERVER:
  21. return "credentials rejected by server";
  22. case InvalidGaiaCredentialsReason::CREDENTIALS_REJECTED_BY_CLIENT:
  23. return "credentials rejected by client";
  24. case InvalidGaiaCredentialsReason::CREDENTIALS_MISSING:
  25. return "credentials missing";
  26. case InvalidGaiaCredentialsReason::NUM_REASONS:
  27. NOTREACHED();
  28. return "";
  29. }
  30. }
  31. } // namespace
  32. bool GoogleServiceAuthError::operator==(
  33. const GoogleServiceAuthError& b) const {
  34. return (state_ == b.state_) && (network_error_ == b.network_error_) &&
  35. (error_message_ == b.error_message_) &&
  36. (invalid_gaia_credentials_reason_ ==
  37. b.invalid_gaia_credentials_reason_);
  38. }
  39. bool GoogleServiceAuthError::operator!=(
  40. const GoogleServiceAuthError& b) const {
  41. return !(*this == b);
  42. }
  43. GoogleServiceAuthError::GoogleServiceAuthError()
  44. : GoogleServiceAuthError(NONE) {}
  45. GoogleServiceAuthError::GoogleServiceAuthError(State s)
  46. : GoogleServiceAuthError(s, std::string()) {}
  47. GoogleServiceAuthError::GoogleServiceAuthError(State state,
  48. const std::string& error_message)
  49. : GoogleServiceAuthError(
  50. state,
  51. (state == CONNECTION_FAILED) ? net::ERR_FAILED : 0) {
  52. error_message_ = error_message;
  53. }
  54. GoogleServiceAuthError::GoogleServiceAuthError(
  55. const GoogleServiceAuthError& other) = default;
  56. GoogleServiceAuthError& GoogleServiceAuthError::operator=(
  57. const GoogleServiceAuthError& other) = default;
  58. // static
  59. GoogleServiceAuthError
  60. GoogleServiceAuthError::FromConnectionError(int error) {
  61. return GoogleServiceAuthError(CONNECTION_FAILED, error);
  62. }
  63. // static
  64. GoogleServiceAuthError GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
  65. InvalidGaiaCredentialsReason reason) {
  66. GoogleServiceAuthError error(INVALID_GAIA_CREDENTIALS);
  67. error.invalid_gaia_credentials_reason_ = reason;
  68. return error;
  69. }
  70. // static
  71. GoogleServiceAuthError GoogleServiceAuthError::FromServiceUnavailable(
  72. const std::string& error_message) {
  73. return GoogleServiceAuthError(SERVICE_UNAVAILABLE, error_message);
  74. }
  75. // static
  76. GoogleServiceAuthError
  77. GoogleServiceAuthError::FromScopeLimitedUnrecoverableError(
  78. const std::string& error_message) {
  79. return GoogleServiceAuthError(SCOPE_LIMITED_UNRECOVERABLE_ERROR,
  80. error_message);
  81. }
  82. // static
  83. GoogleServiceAuthError GoogleServiceAuthError::FromServiceError(
  84. const std::string& error_message) {
  85. return GoogleServiceAuthError(SERVICE_ERROR, error_message);
  86. }
  87. // static
  88. GoogleServiceAuthError GoogleServiceAuthError::FromUnexpectedServiceResponse(
  89. const std::string& error_message) {
  90. return GoogleServiceAuthError(UNEXPECTED_SERVICE_RESPONSE, error_message);
  91. }
  92. // static
  93. GoogleServiceAuthError GoogleServiceAuthError::AuthErrorNone() {
  94. return GoogleServiceAuthError(NONE);
  95. }
  96. // static
  97. bool GoogleServiceAuthError::IsValid(State state) {
  98. switch (state) {
  99. case NONE:
  100. case INVALID_GAIA_CREDENTIALS:
  101. case USER_NOT_SIGNED_UP:
  102. case CONNECTION_FAILED:
  103. case SERVICE_UNAVAILABLE:
  104. case REQUEST_CANCELED:
  105. case UNEXPECTED_SERVICE_RESPONSE:
  106. case SERVICE_ERROR:
  107. case SCOPE_LIMITED_UNRECOVERABLE_ERROR:
  108. return true;
  109. case NUM_STATES:
  110. return false;
  111. }
  112. return false;
  113. }
  114. GoogleServiceAuthError::State GoogleServiceAuthError::state() const {
  115. return state_;
  116. }
  117. int GoogleServiceAuthError::network_error() const {
  118. return network_error_;
  119. }
  120. const std::string& GoogleServiceAuthError::error_message() const {
  121. return error_message_;
  122. }
  123. GoogleServiceAuthError::InvalidGaiaCredentialsReason
  124. GoogleServiceAuthError::GetInvalidGaiaCredentialsReason() const {
  125. DCHECK_EQ(INVALID_GAIA_CREDENTIALS, state());
  126. return invalid_gaia_credentials_reason_;
  127. }
  128. std::string GoogleServiceAuthError::ToString() const {
  129. switch (state_) {
  130. case NONE:
  131. return std::string();
  132. case INVALID_GAIA_CREDENTIALS:
  133. return base::StringPrintf(
  134. "Invalid credentials (%s).",
  135. InvalidCredentialsReasonToString(invalid_gaia_credentials_reason_));
  136. case USER_NOT_SIGNED_UP:
  137. return "Not authorized.";
  138. case CONNECTION_FAILED:
  139. return base::StringPrintf("Connection failed (%d).", network_error_);
  140. case SERVICE_UNAVAILABLE:
  141. return "Service unavailable; try again later.";
  142. case REQUEST_CANCELED:
  143. return "Request canceled.";
  144. case UNEXPECTED_SERVICE_RESPONSE:
  145. return base::StringPrintf("Unexpected service response (%s)",
  146. error_message_.c_str());
  147. case SERVICE_ERROR:
  148. return base::StringPrintf("Service responded with error: '%s'",
  149. error_message_.c_str());
  150. case SCOPE_LIMITED_UNRECOVERABLE_ERROR:
  151. return base::StringPrintf("Service responded with error: '%s'",
  152. error_message_.c_str());
  153. case NUM_STATES:
  154. NOTREACHED();
  155. return std::string();
  156. }
  157. }
  158. bool GoogleServiceAuthError::IsPersistentError() const {
  159. if (state_ == GoogleServiceAuthError::NONE) return false;
  160. return !IsTransientError();
  161. }
  162. bool GoogleServiceAuthError::IsScopePersistentError() const {
  163. return state_ == GoogleServiceAuthError::SCOPE_LIMITED_UNRECOVERABLE_ERROR;
  164. }
  165. bool GoogleServiceAuthError::IsTransientError() const {
  166. switch (state_) {
  167. // These are failures that are likely to succeed if tried again.
  168. case GoogleServiceAuthError::CONNECTION_FAILED:
  169. case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
  170. case GoogleServiceAuthError::REQUEST_CANCELED:
  171. return true;
  172. // Everything else will have the same result.
  173. default:
  174. return false;
  175. }
  176. }
  177. GoogleServiceAuthError::GoogleServiceAuthError(State s, int error)
  178. : state_(s),
  179. network_error_(error),
  180. invalid_gaia_credentials_reason_(InvalidGaiaCredentialsReason::UNKNOWN) {}