Skip to content

Commit 8352adc

Browse files
committed
refactor(functions): notifyError() instead of throwing
1 parent 17c302e commit 8352adc

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ class StreamTests {
143143
assertThat(subscriber.throwable).isInstanceOf(FirebaseFunctionsException::class.java)
144144
}
145145

146+
@Test
147+
fun nonExistentFunction_receivesError() = runBlocking {
148+
val function = functions.getHttpsCallable("nonexistentFunction")
149+
.withTimeout(2000, TimeUnit.MILLISECONDS)
150+
val subscriber = StreamSubscriber()
151+
152+
function.stream().subscribe(subscriber)
153+
154+
withTimeout(2000) {
155+
while (subscriber.throwable == null) {
156+
delay(100)
157+
}
158+
}
159+
160+
assertThat(subscriber.throwable).isNotNull()
161+
assertThat(subscriber.throwable).isInstanceOf(FirebaseFunctionsException::class.java)
162+
assertThat((subscriber.throwable as FirebaseFunctionsException).code)
163+
.isEqualTo(FirebaseFunctionsException.Code.NOT_FOUND)
164+
}
165+
146166
@Test
147167
fun genStreamWeather_receivesWeatherForecasts() = runBlocking {
148168
val inputData = listOf(mapOf("name" to "Toronto"), mapOf("name" to "London"))

firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,12 @@ internal class PublisherStream(
300300
val errorMessage: String
301301
if (response.code() == 404 && response.header("Content-Type") == htmlContentType) {
302302
errorMessage = """URL not found. Raw response: ${response.body()?.string()}""".trimMargin()
303-
throw FirebaseFunctionsException(
304-
errorMessage,
305-
FirebaseFunctionsException.Code.fromHttpStatus(response.code()),
306-
null
303+
notifyError(
304+
FirebaseFunctionsException(
305+
errorMessage,
306+
FirebaseFunctionsException.Code.fromHttpStatus(response.code()),
307+
null
308+
)
307309
)
308310
}
309311

@@ -313,16 +315,21 @@ internal class PublisherStream(
313315
val json = JSONObject(text)
314316
error = serializer.decode(json.opt("error"))
315317
} catch (e: Throwable) {
316-
throw FirebaseFunctionsException(
317-
"${e.message} Unexpected Response:\n$text ",
318-
FirebaseFunctionsException.Code.INTERNAL,
319-
e
318+
notifyError(
319+
FirebaseFunctionsException(
320+
"${e.message} Unexpected Response:\n$text ",
321+
FirebaseFunctionsException.Code.INTERNAL,
322+
e
323+
)
320324
)
325+
return
321326
}
322-
throw FirebaseFunctionsException(
323-
error.toString(),
324-
FirebaseFunctionsException.Code.INTERNAL,
325-
error
327+
notifyError(
328+
FirebaseFunctionsException(
329+
error.toString(),
330+
FirebaseFunctionsException.Code.INTERNAL,
331+
error
332+
)
326333
)
327334
}
328335
}

0 commit comments

Comments
 (0)