Skip to content

Commit be689ab

Browse files
authored
More accurate error reporting when an Auth error occurs. (#437)
* More accurate error reporting when an Auth error occurs. * Address code review comments. * Fix variable name usage. * Improve the comment formatting.
1 parent 6911397 commit be689ab

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

firestore/src/ios/credentials_provider_desktop.cc

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Firestore/core/src/util/status.h"
77
#include "app/src/function_registry.h"
88
#include "app/src/reference_counted_future_impl.h"
9+
#include "firebase/auth/types.h"
910
#include "firebase/firestore/firestore_errors.h"
1011
#include "firestore/src/common/futures.h"
1112
#include "firestore/src/common/hard_assert_common.h"
@@ -18,9 +19,39 @@ using auth::CredentialChangeListener;
1819
using auth::Token;
1920
using auth::TokenListener;
2021
using auth::User;
22+
using firebase::auth::AuthError;
2123
using util::Status;
2224
using util::StatusOr;
2325

26+
/**
27+
* Takes an integer that represents an `AuthError` enum value, and returns a
28+
* `firestore::Error` that best describes the given `AuthError`.
29+
*/
30+
Error FirestoreErrorFromAuthError(int error) {
31+
switch (error) {
32+
case AuthError::kAuthErrorNone:
33+
return kErrorNone;
34+
case AuthError::kAuthErrorUnimplemented:
35+
return kErrorUnimplemented;
36+
case AuthError::kAuthErrorFailure:
37+
return kErrorInternal;
38+
case AuthError::kAuthErrorNetworkRequestFailed:
39+
return kErrorUnavailable;
40+
case AuthError::kAuthErrorCancelled:
41+
return kErrorCancelled;
42+
case AuthError::kAuthErrorInvalidCustomToken:
43+
case AuthError::kAuthErrorInvalidCredential:
44+
case AuthError::kAuthErrorUserDisabled:
45+
case AuthError::kAuthErrorUserNotFound:
46+
case AuthError::kAuthErrorInvalidUserToken:
47+
case AuthError::kAuthErrorUserTokenExpired:
48+
case AuthError::kAuthErrorNoSignedInUser:
49+
return kErrorUnauthenticated;
50+
default:
51+
return kErrorUnknown;
52+
}
53+
}
54+
2455
/**
2556
* Returns a Future that, when completed, will contain the token for the
2657
* current user or an error. An empty token means that the current user is
@@ -58,12 +89,9 @@ User GetCurrentUser(App& app) {
5889
StatusOr<Token> ConvertToken(const Future<std::string>& future, App& app) {
5990
if (future.error() != Error::kErrorOk) {
6091
// `AuthError` is a different error domain from go/canonical-codes that
61-
// `Status` uses, so it can't be converted directly. Instead, use
62-
// `kErrorUnknown` in the `Status` because the error code from the future
63-
// is "from a different error domain".
64-
// TODO(b/174485290) Map `AuthError` values to Firestore `Error` values more
65-
// intelligently so as to enable retries when appropriate.
66-
return Status(Error::kErrorUnknown,
92+
// `Status` uses. We map `AuthError` values to Firestore `Error` values in
93+
// order to be able to perform retries when appropriate.
94+
return Status(FirestoreErrorFromAuthError(future.error()),
6795
std::string(future.error_message()) + " (AuthError " +
6896
std::to_string(future.error()) + ")");
6997
}
@@ -76,8 +104,10 @@ StatusOr<Token> ConvertToken(const Future<std::string>& future, App& app) {
76104
// the `listener` with the error. If the current token generation is higher
77105
// than `expected_generation`, will invoke the `listener` with "aborted"
78106
// error. `future_token` must be a completed future.
79-
void OnToken(const Future<std::string>& future_token, App& app,
80-
int token_generation, const TokenListener& listener,
107+
void OnToken(const Future<std::string>& future_token,
108+
App& app,
109+
int token_generation,
110+
const TokenListener& listener,
81111
int expected_generation) {
82112
SIMPLE_HARD_ASSERT(
83113
future_token.status() == FutureStatus::kFutureStatusComplete,
@@ -98,7 +128,8 @@ void OnToken(const Future<std::string>& future_token, App& app,
98128
} // namespace
99129

100130
FirebaseCppCredentialsProvider::FirebaseCppCredentialsProvider(App& app)
101-
: contents_(std::make_shared<Contents>(app)) {}
131+
: contents_(std::make_shared<Contents>(app)) {
132+
}
102133

103134
FirebaseCppCredentialsProvider::~FirebaseCppCredentialsProvider() {
104135
RemoveAuthStateListener();

0 commit comments

Comments
 (0)