Skip to content

Commit ab74442

Browse files
authored
Improve error logging for FIRAuthInternalErrorCodeUnexpectedErrorResponse errors (#8704)
* Add underlyingError to FIRAuthInternalErrorCodeUnexpectedErrorResponse errors. * Revert some formatting changes that make it hard to see the actual diffs. * Fix unit tests. * Apparently the formatting is required. * Update changelog. * Re-ran the formatting script after updating clang-format.
1 parent 4262ffe commit ab74442

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

FirebaseAuth/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
# Unreleased
2+
- [changed] Improved error logging. (#8704)
3+
14
# 8.8.0
25
- [fixed] Fall back to reCAPTCHA for phone auth app verification if the push notification is not received before the timeout. (#8653)
36

47
# 8.6.0
5-
- [fixed] Annotated platform-level availability using `API_UNAVAILABLE` instead of conditionally compiling certain methods with `#if` directives (#8451).
8+
- [fixed] Annotated platform-level availability using `API_UNAVAILABLE` instead of conditionally compiling certain methods with `#if` directives. (#8451)
69

710
# 8.5.0
811
- [fixed] Fixed an analyze issue introduced in Xcode 12.5. (#8411)

FirebaseAuth/Sources/Backend/FIRAuthBackend.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,8 @@ - (void)postWithRequest:(id<FIRAuthRPCRequest>)request
10411041
if (![dictionary isKindOfClass:[NSDictionary class]]) {
10421042
if (error) {
10431043
callback([FIRAuthErrorUtils
1044-
unexpectedErrorResponseWithDeserializedResponse:dictionary]);
1044+
unexpectedErrorResponseWithDeserializedResponse:dictionary
1045+
underlyingError:error]);
10451046
} else {
10461047
callback([FIRAuthErrorUtils
10471048
unexpectedResponseWithDeserializedResponse:dictionary]);
@@ -1074,15 +1075,16 @@ - (void)postWithRequest:(id<FIRAuthRPCRequest>)request
10741075
// Not a message we know, return the message directly.
10751076
if (errorMessage) {
10761077
NSError *unexpecterErrorResponse = [FIRAuthErrorUtils
1077-
unexpectedErrorResponseWithDeserializedResponse:
1078-
errorDictionary];
1078+
unexpectedErrorResponseWithDeserializedResponse:errorDictionary
1079+
underlyingError:error];
10791080
callback(unexpecterErrorResponse);
10801081
return;
10811082
}
10821083
}
10831084
// No error message at all, return the decoded response.
10841085
callback([FIRAuthErrorUtils
1085-
unexpectedErrorResponseWithDeserializedResponse:dictionary]);
1086+
unexpectedErrorResponseWithDeserializedResponse:dictionary
1087+
underlyingError:error]);
10861088
return;
10871089
}
10881090

FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ NS_ASSUME_NONNULL_BEGIN
9090
*/
9191
+ (NSError *)unexpectedErrorResponseWithDeserializedResponse:(id)deserializedResponse;
9292

93+
/** @fn unexpectedErrorResponseWithDeserializedResponse:underlyingError:
94+
@brief Constructs an @c NSError with the @c FIRAuthInternalErrorCodeUnexpectedErrorResponse
95+
code, and populated @c FIRAuthErrorUserInfoDeserializedResponseKey and
96+
@c NSUnderlyingErrorKey keys in the @c NSError.userInfo dictionary.
97+
@param deserializedResponse The value of the @c FIRAuthErrorUserInfoDeserializedResponseKey key.
98+
@param underlyingError The value of the @c NSUnderlyingErrorKey key.
99+
@remarks This error is used when a network request results in an error, and the body data was
100+
deserializable as JSON, but couldn't be decoded as an error.
101+
*/
102+
+ (NSError *)unexpectedErrorResponseWithDeserializedResponse:(id)deserializedResponse
103+
underlyingError:(NSError *)underlyingError;
104+
93105
/** @fn malformedJWTErrorWithToken:underlyingError:
94106
@brief Constructs an @c NSError with the code set to @c FIRAuthErrorCodeMalformedJWT and
95107
populates the userInfo dictionary with an error message, the bad token, and an underlying

FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,19 @@ + (NSError *)unexpectedErrorResponseWithDeserializedResponse:(id)deserializedRes
10131013
return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedErrorResponse userInfo:userInfo];
10141014
}
10151015

1016+
+ (NSError *)unexpectedErrorResponseWithDeserializedResponse:(id)deserializedResponse
1017+
underlyingError:(NSError *)underlyingError {
1018+
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
1019+
if (deserializedResponse) {
1020+
userInfo[FIRAuthErrorUserInfoDeserializedResponseKey] = deserializedResponse;
1021+
}
1022+
if (underlyingError) {
1023+
userInfo[NSUnderlyingErrorKey] = underlyingError;
1024+
}
1025+
return [self errorWithCode:FIRAuthInternalErrorCodeUnexpectedErrorResponse
1026+
userInfo:[userInfo copy]];
1027+
}
1028+
10161029
+ (NSError *)malformedJWTErrorWithToken:(NSString *)token
10171030
underlyingError:(NSError *_Nullable)underlyingError {
10181031
NSMutableDictionary *userInfo =

FirebaseAuth/Tests/Unit/FIRAuthBackendRPCImplementationTests.m

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,10 @@ - (void)testUnparsableSuccessResponse {
575575
/** @fn testNonDictionaryErrorResponse
576576
@brief This test checks the behaviour of @c postWithRequest:response:callback: when the
577577
response deserialized by @c NSJSONSerialization is not a dictionary, and an error was
578-
expected. We are expecting to receive an @c NSError with the code
579-
@c FIRAuthErrorCodeUnexpectedErrorServerResponse with the decoded response in the
580-
@c NSError.userInfo dictionary associated with the key
581-
@c FIRAuthErrorUserInfoDecodedResponseKey.
578+
expected. We are expecting to receive the original network error wrapped in an @c NSError
579+
with the code @c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded response
580+
in the @c NSError.userInfo dictionary associated with the key
581+
@c FIRAuthErrorUserInfoDeserializedResponseKey.
582582
*/
583583
- (void)testNonDictionaryErrorResponse {
584584
FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
@@ -613,7 +613,9 @@ - (void)testNonDictionaryErrorResponse {
613613
XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
614614

615615
NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
616-
XCTAssertNil(underlyingUnderlyingError);
616+
XCTAssertNotNil(underlyingUnderlyingError);
617+
XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
618+
XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
617619

618620
id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
619621
XCTAssertNotNil(deserializedResponse);
@@ -677,9 +679,9 @@ - (void)testNonDictionarySuccessResponse {
677679
@brief This test checks the behaviour of @c postWithRequest:response:callback: when the
678680
we get an error message indicating captcha is required. The backend should not be returning
679681
this error to mobile clients. If it does, we should wrap it in an @c NSError with the code
680-
@c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
682+
@c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded error message in the
681683
@c NSError.userInfo dictionary associated with the key
682-
@c FIRAuthErrorUserInfoDecodedErrorResponseKey.
684+
@c FIRAuthErrorUserInfoDeserializedResponseKey.
683685
*/
684686
- (void)testCaptchaRequiredResponse {
685687
FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
@@ -709,7 +711,9 @@ - (void)testCaptchaRequiredResponse {
709711
XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
710712

711713
NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
712-
XCTAssertNil(underlyingUnderlyingError);
714+
XCTAssertNotNil(underlyingUnderlyingError);
715+
XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
716+
XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
713717

714718
id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
715719
XCTAssertNotNil(deserializedResponse);
@@ -756,9 +760,9 @@ - (void)testCaptchaCheckFailedResponse {
756760
we get an error message indicating captcha is required and an invalid password was entered.
757761
The backend should not be returning this error to mobile clients. If it does, we should wrap
758762
it in an @c NSError with the code
759-
@c FIRAuthErrorCodeUnexpectedServerResponse with the decoded error message in the
763+
@c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded error message in the
760764
@c NSError.userInfo dictionary associated with the key
761-
@c FIRAuthErrorUserInfoDecodedErrorResponseKey.
765+
@c FIRAuthErrorUserInfoDeserializedResponseKey.
762766
*/
763767
- (void)testCaptchaRequiredInvalidPasswordResponse {
764768
FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
@@ -789,7 +793,9 @@ - (void)testCaptchaRequiredInvalidPasswordResponse {
789793
XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
790794

791795
NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
792-
XCTAssertNil(underlyingUnderlyingError);
796+
XCTAssertNotNil(underlyingUnderlyingError);
797+
XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
798+
XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
793799

794800
id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
795801
XCTAssertNotNil(deserializedResponse);
@@ -804,9 +810,10 @@ - (void)testCaptchaRequiredInvalidPasswordResponse {
804810
@brief This test checks the behaviour of @c postWithRequest:response:callback: when the
805811
response deserialized by @c NSJSONSerialization represents a valid error response (and an
806812
error was indicated) but we didn't receive an error message we know about. We are expecting
807-
an @c NSError with the code @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
813+
to receive the original network error wrapped in an @c NSError with the code
814+
@c FIRAuthInternalErrorCodeUnexpectedErrorResponse with the decoded
808815
error message in the @c NSError.userInfo dictionary associated with the key
809-
@c FIRAuthErrorUserInfoDecodedErrorResponseKey.
816+
@c FIRAuthErrorUserInfoDeserializedResponseKey.
810817
*/
811818
- (void)testDecodableErrorResponseWithUnknownMessage {
812819
FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
@@ -838,7 +845,9 @@ - (void)testDecodableErrorResponseWithUnknownMessage {
838845
XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
839846

840847
NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
841-
XCTAssertNil(underlyingUnderlyingError);
848+
XCTAssertNotNil(underlyingUnderlyingError);
849+
XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
850+
XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
842851

843852
id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
844853
XCTAssertNotNil(deserializedResponse);
@@ -852,10 +861,11 @@ - (void)testDecodableErrorResponseWithUnknownMessage {
852861
/** @fn testErrorResponseWithNoErrorMessage
853862
@brief This test checks the behaviour of @c postWithRequest:response:callback: when the
854863
response deserialized by @c NSJSONSerialization is a dictionary, and an error was indicated,
855-
but no error information was present in the decoded response. We are expecting an @c NSError
856-
with the code @c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
864+
but no error information was present in the decoded response. We are expecting to receive
865+
the original network error wrapped in an @c NSError with the code
866+
@c FIRAuthErrorCodeUnexpectedServerResponse with the decoded
857867
response message in the @c NSError.userInfo dictionary associated with the key
858-
@c FIRAuthErrorUserInfoDecodedResponseKey.
868+
@c FIRAuthErrorUserInfoDeserializedResponseKey.
859869
*/
860870
- (void)testErrorResponseWithNoErrorMessage {
861871
FIRFakeRequest *request = [FIRFakeRequest fakeRequest];
@@ -885,7 +895,9 @@ - (void)testErrorResponseWithNoErrorMessage {
885895
XCTAssertEqual(underlyingError.code, FIRAuthInternalErrorCodeUnexpectedErrorResponse);
886896

887897
NSError *underlyingUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
888-
XCTAssertNil(underlyingUnderlyingError);
898+
XCTAssertNotNil(underlyingUnderlyingError);
899+
XCTAssertEqualObjects(underlyingUnderlyingError.domain, kFakeErrorDomain);
900+
XCTAssertEqual(underlyingUnderlyingError.code, kFakeErrorCode);
889901

890902
id deserializedResponse = underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey];
891903
XCTAssertNotNil(deserializedResponse);

0 commit comments

Comments
 (0)