Skip to content

Commit 1658c40

Browse files
authored
Fix 9.0.0 Functions Error Code Regressions (#9862)
1 parent 9b4d0b6 commit 1658c40

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

FirebaseFunctions/Backend/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ exports.httpErrorTest = functions.https.onRequest((request, response) => {
108108
response.status(400).send();
109109
});
110110

111+
// Regression test for https://github.com/firebase/firebase-ios-sdk/issues/9855
112+
exports.throwTest = functions.https.onCall((data) => {
113+
throw new functions.https.HttpsError('invalid-argument', 'Invalid test requested.');
114+
});
115+
111116
exports.timeoutTest = functions.https.onRequest((request, response) => {
112117
// Wait for longer than 500ms.
113118
setTimeout(() => response.send({data: true}), 500);

FirebaseFunctions/Backend/start.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ FUNCTIONS_BIN="./node_modules/.bin/functions"
5353
"${FUNCTIONS_BIN}" deploy unknownErrorTest --trigger-http
5454
"${FUNCTIONS_BIN}" deploy explicitErrorTest --trigger-http
5555
"${FUNCTIONS_BIN}" deploy httpErrorTest --trigger-http
56+
"${FUNCTIONS_BIN}" deploy throwTest --trigger-http
5657
"${FUNCTIONS_BIN}" deploy timeoutTest --trigger-http
5758

5859
if [ "$1" != "synchronous" ]; then

FirebaseFunctions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 9.2.0
2+
- [fixed] Fixed regressions in error code processing introduced in 9.0.0. (#9855)
3+
14
# 9.0.0
25
- [changed] The FirebaseFunctionsSwift library has been removed. All of its APIs are now included
36
in the FirebaseFunctions library. Please remove references to FirebaseFunctionsSwift from Podfiles

FirebaseFunctions/Sources/FunctionsError.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ extension FunctionsErrorCode {
143143
static func errorCode(forName name: String) -> FunctionsErrorCode {
144144
switch name {
145145
case "OK": return .OK
146-
case "CANELLED": return .cancelled
147-
case "UNKOWN": return .unknown
148-
case "INVLID_ARGUMENT": return .invalidArgument
149-
case "DEALINE_EXCEEDED": return .deadlineExceeded
150-
case "NOTFOUND": return .notFound
146+
case "CANCELLED": return .cancelled
147+
case "UNKNOWN": return .unknown
148+
case "INVALID_ARGUMENT": return .invalidArgument
149+
case "DEADLINE_EXCEEDED": return .deadlineExceeded
150+
case "NOT_FOUND": return .notFound
151151
case "ALREADY_EXISTS": return .notFound
152152
case "PERMISSION_DENIED": return .permissionDenied
153153
case "RESOURCE_EXHAUSTED": return .resourceExhausted
@@ -158,9 +158,7 @@ extension FunctionsErrorCode {
158158
case "INTERNAL": return .internal
159159
case "UNAVAILABLE": return .unavailable
160160
case "DATA_LOSS": return .dataLoss
161-
case "UNATHENTICATED": return .unauthenticated
162-
// TODO(review): The docs originally say that unknown should be returned if it doesn't match,
163-
// but the implementation doesn't do that, it's optional. Let's make this internal.
161+
case "UNAUTHENTICATED": return .unauthenticated
164162
default: return .internal
165163
}
166164
}

FirebaseFunctions/Tests/Integration/IntegrationTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,62 @@ class IntegrationTests: XCTestCase {
647647
}
648648
#endif
649649

650+
func testThrowError() {
651+
let byName = functions.httpsCallable(
652+
"throwTest",
653+
requestAs: [Int].self,
654+
responseAs: Int.self
655+
)
656+
let byURL = functions.httpsCallable(
657+
emulatorURL("throwTest"),
658+
requestAs: [Int].self,
659+
responseAs: Int.self
660+
)
661+
for function in [byName, byURL] {
662+
let expectation = expectation(description: #function)
663+
XCTAssertNotNil(function)
664+
function.call([]) { result in
665+
do {
666+
_ = try result.get()
667+
} catch {
668+
let error = error as NSError
669+
XCTAssertEqual(FunctionsErrorCode.invalidArgument.rawValue, error.code)
670+
XCTAssertEqual(error.localizedDescription, "Invalid test requested.")
671+
expectation.fulfill()
672+
return
673+
}
674+
XCTFail("Failed to throw error for missing result")
675+
}
676+
waitForExpectations(timeout: 5)
677+
}
678+
}
679+
680+
#if compiler(>=5.5.2) && canImport(_Concurrency)
681+
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
682+
func testThrowErrorAsync() async {
683+
let byName = functions.httpsCallable(
684+
"throwTest",
685+
requestAs: [Int].self,
686+
responseAs: Int.self
687+
)
688+
let byURL = functions.httpsCallable(
689+
emulatorURL("throwTest"),
690+
requestAs: [Int].self,
691+
responseAs: Int.self
692+
)
693+
for function in [byName, byURL] {
694+
do {
695+
_ = try await function.call([])
696+
XCTAssertFalse(true, "Failed to throw error for missing result")
697+
} catch {
698+
let error = error as NSError
699+
XCTAssertEqual(FunctionsErrorCode.invalidArgument.rawValue, error.code)
700+
XCTAssertEqual(error.localizedDescription, "Invalid test requested.")
701+
}
702+
}
703+
}
704+
#endif
705+
650706
func testTimeout() {
651707
let byName = functions.httpsCallable(
652708
"timeoutTest",

FirebaseFunctions/Tests/ObjCIntegration/FIRIntegrationTests.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ - (void)testHttpError {
261261
[self waitForExpectations:@[ expectation ] timeout:10];
262262
}
263263

264+
// Regression test for https://github.com/firebase/firebase-ios-sdk/issues/9855
265+
- (void)testThrowTest {
266+
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
267+
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"throwTest"];
268+
[function callWithObject:@{}
269+
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
270+
XCTAssertNotNil(error);
271+
XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
272+
XCTAssertEqualObjects(error.localizedDescription, @"Invalid test requested.");
273+
[expectation fulfill];
274+
}];
275+
[self waitForExpectations:@[ expectation ] timeout:10];
276+
}
277+
264278
- (void)testTimeout {
265279
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
266280
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"timeoutTest"];

0 commit comments

Comments
 (0)