From 8352adc45183ab8e10bf4ae522e74e071c80b53c Mon Sep 17 00:00:00 2001 From: rosariopf Date: Fri, 14 Mar 2025 12:45:05 +0000 Subject: [PATCH 1/2] refactor(functions): notifyError() instead of throwing --- .../google/firebase/functions/StreamTests.kt | 20 ++++++++++++ .../firebase/functions/PublisherStream.kt | 31 ++++++++++++------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt b/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt index e0de5cc2262..21ea4636a54 100644 --- a/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt +++ b/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt @@ -143,6 +143,26 @@ class StreamTests { assertThat(subscriber.throwable).isInstanceOf(FirebaseFunctionsException::class.java) } + @Test + fun nonExistentFunction_receivesError() = runBlocking { + val function = functions.getHttpsCallable("nonexistentFunction") + .withTimeout(2000, TimeUnit.MILLISECONDS) + val subscriber = StreamSubscriber() + + function.stream().subscribe(subscriber) + + withTimeout(2000) { + while (subscriber.throwable == null) { + delay(100) + } + } + + assertThat(subscriber.throwable).isNotNull() + assertThat(subscriber.throwable).isInstanceOf(FirebaseFunctionsException::class.java) + assertThat((subscriber.throwable as FirebaseFunctionsException).code) + .isEqualTo(FirebaseFunctionsException.Code.NOT_FOUND) + } + @Test fun genStreamWeather_receivesWeatherForecasts() = runBlocking { val inputData = listOf(mapOf("name" to "Toronto"), mapOf("name" to "London")) diff --git a/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt b/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt index 6fc6a9d657c..0bd0173f073 100644 --- a/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt +++ b/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt @@ -300,10 +300,12 @@ internal class PublisherStream( val errorMessage: String if (response.code() == 404 && response.header("Content-Type") == htmlContentType) { errorMessage = """URL not found. Raw response: ${response.body()?.string()}""".trimMargin() - throw FirebaseFunctionsException( - errorMessage, - FirebaseFunctionsException.Code.fromHttpStatus(response.code()), - null + notifyError( + FirebaseFunctionsException( + errorMessage, + FirebaseFunctionsException.Code.fromHttpStatus(response.code()), + null + ) ) } @@ -313,16 +315,21 @@ internal class PublisherStream( val json = JSONObject(text) error = serializer.decode(json.opt("error")) } catch (e: Throwable) { - throw FirebaseFunctionsException( - "${e.message} Unexpected Response:\n$text ", - FirebaseFunctionsException.Code.INTERNAL, - e + notifyError( + FirebaseFunctionsException( + "${e.message} Unexpected Response:\n$text ", + FirebaseFunctionsException.Code.INTERNAL, + e + ) ) + return } - throw FirebaseFunctionsException( - error.toString(), - FirebaseFunctionsException.Code.INTERNAL, - error + notifyError( + FirebaseFunctionsException( + error.toString(), + FirebaseFunctionsException.Code.INTERNAL, + error + ) ) } } From 6b6241300bee13f0b4d86326d33d22402c31104a Mon Sep 17 00:00:00 2001 From: rosariopf Date: Fri, 14 Mar 2025 13:59:37 +0000 Subject: [PATCH 2/2] style: spotless apply --- .../java/com/google/firebase/functions/StreamTests.kt | 4 ++-- .../java/com/google/firebase/functions/PublisherStream.kt | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt b/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt index 21ea4636a54..300385f6a13 100644 --- a/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt +++ b/firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt @@ -145,8 +145,8 @@ class StreamTests { @Test fun nonExistentFunction_receivesError() = runBlocking { - val function = functions.getHttpsCallable("nonexistentFunction") - .withTimeout(2000, TimeUnit.MILLISECONDS) + val function = + functions.getHttpsCallable("nonexistentFunction").withTimeout(2000, TimeUnit.MILLISECONDS) val subscriber = StreamSubscriber() function.stream().subscribe(subscriber) diff --git a/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt b/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt index 0bd0173f073..a8ef77c9442 100644 --- a/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt +++ b/firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt @@ -325,11 +325,7 @@ internal class PublisherStream( return } notifyError( - FirebaseFunctionsException( - error.toString(), - FirebaseFunctionsException.Code.INTERNAL, - error - ) + FirebaseFunctionsException(error.toString(), FirebaseFunctionsException.Code.INTERNAL, error) ) } }