Commit 0554f0d
authored
fix(functions): use notifyError() instead of throwing (#6773)
Throwing inside the `PublisherStream` causes a Runtime exception that
can't be caught in the call site.
Instead, we should use `notifyError()` so that the error can be caught
in the `Subscriber#onError()` function.
I tried to catch the exception using both of the code snippets below,
but none of them worked. (they both work with the changes in this PR):
```kotlin
functions.getHttpsCallable("nonExistentFunction")
.stream().asFlow()
.catch {
// Handle error for a 404 function
}
.collect {
// ...
}
```
```kotlin
try {
functions.getHttpsCallable("nonExistentFunction")
.stream().asFlow()
.collect {
// ...
}
} catch(e: Exception) {
// Handle error for a 404 function
}
```
Runtime exception thrown:
```
FATAL EXCEPTION: OkHttp Dispatcher
Process: com.google.samples.quickstart.functions, PID: 13321
com.google.firebase.functions.FirebaseFunctionsException: Value <html><head> of type java.lang.String cannot be converted to JSONObject Unexpected Response:
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 Page not found</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Page not found</h1>
<h2>The requested URL was not found on this server.</h2>
<h2></h2>
</body></html>
at com.google.firebase.functions.PublisherStream.validateResponse(PublisherStream.kt:316)
at com.google.firebase.functions.PublisherStream.access$validateResponse(PublisherStream.kt:41)
at com.google.firebase.functions.PublisherStream$startStreaming$1$4.onResponse(PublisherStream.kt:161)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1156)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:651)
at java.lang.Thread.run(Thread.java:1119)
```1 parent 1aca899 commit 0554f0d
File tree
2 files changed
+35
-12
lines changed- firebase-functions/src
- androidTest/java/com/google/firebase/functions
- main/java/com/google/firebase/functions
2 files changed
+35
-12
lines changedLines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
146 | 166 | | |
147 | 167 | | |
148 | 168 | | |
| |||
Lines changed: 15 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
307 | 309 | | |
308 | 310 | | |
309 | 311 | | |
| |||
313 | 315 | | |
314 | 316 | | |
315 | 317 | | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
320 | 324 | | |
| 325 | + | |
321 | 326 | | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
| 327 | + | |
| 328 | + | |
326 | 329 | | |
327 | 330 | | |
328 | 331 | | |
0 commit comments