Skip to content

fix(functions): use notifyError() instead of throwing #6773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}

Expand All @@ -313,16 +315,17 @@ 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)
)
}
}
Loading