Skip to content

Commit 0b3bf61

Browse files
authored
added handling of error returned from a blocking cloud function (#10628)
1 parent 1095aaf commit 0b3bf61

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

FirebaseAuth/Sources/Backend/FIRAuthBackend.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@
468468
*/
469469
static NSString *const kUnsupportedFirstFactorErrorMessage = @"UNSUPPORTED_FIRST_FACTOR";
470470

471+
/** @var kBlockingCloudFunctionErrorResponse
472+
@brief This is the error message blocking Cloud Functions.
473+
*/
474+
static NSString *const kBlockingCloudFunctionErrorResponse = @"BLOCKING_FUNCTION_ERROR_RESPONSE";
475+
471476
/** @var kEmailChangeNeedsVerificationErrorMessage
472477
@brief This is the error message the server will respond with if changing an unverified email.
473478
*/
@@ -1447,6 +1452,11 @@ + (nullable NSError *)clientErrorWithServerErrorMessage:(NSString *)serverErrorM
14471452
return [FIRAuthErrorUtils unsupportedTenantOperationError];
14481453
}
14491454

1455+
if ([shortErrorMessage isEqualToString:kBlockingCloudFunctionErrorResponse]) {
1456+
return
1457+
[FIRAuthErrorUtils blockingCloudFunctionServerResponseWithMessage:serverDetailErrorMessage];
1458+
}
1459+
14501460
// In this case we handle an error that might be specified in the underlying errors dictionary,
14511461
// the error message in determined based on the @c reason key in the dictionary.
14521462
if (errorDictionary[kErrorsKey]) {

FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ typedef NS_ERROR_ENUM(FIRAuthErrorDomain, FIRAuthErrorCode){
413413
*/
414414
FIRAuthErrorCodeMissingOrInvalidNonce = 17094,
415415

416+
/** Raised when an Cloud Function returns a blocking error. Will include a message returned from
417+
* the function.
418+
*/
419+
FIRAuthErrorCodeBlockingCloudFunctionError = 17105,
420+
416421
/** Indicates an error for when the client identifier is missing.
417422
*/
418423
FIRAuthErrorCodeMissingClientIdentifier = 17993,

FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ NS_ASSUME_NONNULL_BEGIN
605605
*/
606606
+ (NSError *)unsupportedTenantOperationError;
607607

608+
+ (NSError *)blockingCloudFunctionServerResponseWithMessage:(nullable NSString *)message;
609+
608610
@end
609611

610612
NS_ASSUME_NONNULL_END

FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@
591591
@"This operation is not"
592592
"supported in a multi-tenant context.";
593593

594+
/** @var kFIRAuthErrorMessageBlockingCloudFunctionReturnedError
595+
@brief Message for @c FIRAuthErrorCodeBlockingCloudFunctionError error code.
596+
*/
597+
static NSString *const kFIRAuthErrorMessageBlockingCloudFunctionReturnedError =
598+
@"Blocking cloud function returned an error.";
599+
594600
/** @var FIRAuthErrorDescription
595601
@brief The error descrioption, based on the error code.
596602
@remarks No default case so that we get a compiler warning if a new value was added to the enum.
@@ -755,6 +761,8 @@
755761
return kFIRAuthErrorMessageTenantIDMismatch;
756762
case FIRAuthErrorCodeUnsupportedTenantOperation:
757763
return kFIRAuthErrorMessageUnsupportedTenantOperation;
764+
case FIRAuthErrorCodeBlockingCloudFunctionError:
765+
return kFIRAuthErrorMessageBlockingCloudFunctionReturnedError;
758766
}
759767
}
760768

@@ -922,6 +930,8 @@
922930
return @"ERROR_TENANT_ID_MISMATCH";
923931
case FIRAuthErrorCodeUnsupportedTenantOperation:
924932
return @"ERROR_UNSUPPORTED_TENANT_OPERATION";
933+
case FIRAuthErrorCodeBlockingCloudFunctionError:
934+
return @"ERROR_BLOCKING_CLOUD_FUNCTION_RETURNED_ERROR";
925935
}
926936
}
927937

@@ -1405,6 +1415,32 @@ + (NSError *)unsupportedTenantOperationError {
14051415
return [self errorWithCode:FIRAuthInternalErrorCodeUnsupportedTenantOperation];
14061416
}
14071417

1418+
+ (NSError *)blockingCloudFunctionServerResponseWithMessage:(nullable NSString *)message {
1419+
if (message == nil) {
1420+
return [self errorWithCode:FIRAuthInternalErrorBlockingCloudFunctionError message:message];
1421+
}
1422+
1423+
NSString *jsonString =
1424+
[message stringByReplacingOccurrencesOfString:@"HTTP Cloud Function returned an error:"
1425+
withString:@""];
1426+
jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
1427+
1428+
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
1429+
NSError *jsonError;
1430+
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
1431+
options:0
1432+
error:&jsonError];
1433+
1434+
if (jsonError) {
1435+
return [self JSONSerializationErrorWithUnderlyingError:jsonError];
1436+
}
1437+
1438+
NSDictionary *errorDict = jsonDict[@"error"];
1439+
NSString *errorMessage = errorDict[@"message"];
1440+
1441+
return [self errorWithCode:FIRAuthInternalErrorBlockingCloudFunctionError message:errorMessage];
1442+
}
1443+
14081444
@end
14091445

14101446
NS_ASSUME_NONNULL_END

FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ typedef NS_ENUM(NSInteger, FIRAuthInternalErrorCode) {
477477

478478
FIRAuthInternalErrorCodeMalformedJWT = FIRAuthPublicErrorCodeFlag | FIRAuthErrorCodeMalformedJWT,
479479

480+
/** Indicates that an authentication blocking cloud function returned an error.
481+
*/
482+
FIRAuthInternalErrorBlockingCloudFunctionError = FIRAuthPublicErrorCodeFlag |
483+
FIRAuthErrorCodeBlockingCloudFunctionError,
484+
480485
/** @var FIRAuthInternalErrorCodeRPCRequestEncodingError
481486
@brief Indicates an error encoding the RPC request.
482487
@remarks This is typically due to some sort of unexpected input value.

0 commit comments

Comments
 (0)