Skip to content

Commit 1684c0e

Browse files
authored
fix: make user function exceptions log level SEVERE (GoogleCloudPlatform#113)
The FF is inconsistent in how it logs errors in the user function between HTTP functions and Event-driven functions. This change makes it so that all functions log the same error with the same severity if the user function throws an exception. Log level SEVERE is required to be consistent with the Cloud Functions & Error Reporting [docs](https://cloud.google.com/error-reporting/docs/setup/nodejs#cloud_functions).
1 parent 4a87830 commit 1684c0e

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

invoker/core/src/main/java/com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public void service(HttpServletRequest req, HttpServletResponse res) throws IOEx
333333
res.setStatus(HttpServletResponse.SC_OK);
334334
} catch (Throwable t) {
335335
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
336-
logger.log(Level.WARNING, "Failed to execute " + functionExecutor.functionName(), t);
336+
logger.log(Level.SEVERE, "Failed to execute " + functionExecutor.functionName(), t);
337337
}
338338
}
339339

invoker/core/src/main/java/com/google/cloud/functions/invoker/HttpFunctionExecutor.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ public void service(HttpServletRequest req, HttpServletResponse res) {
6666
Thread.currentThread().setContextClassLoader(function.getClass().getClassLoader());
6767
function.service(reqImpl, respImpl);
6868
} catch (Throwable t) {
69-
// TODO(b/146510646): this should be logged properly as an exception, but that currently
70-
// causes integration tests to fail.
71-
// logger.log(Level.WARNING, "Failed to execute " + function.getClass().getName(), t);
72-
logger.log(Level.WARNING, "Failed to execute {0}", function.getClass().getName());
73-
t.printStackTrace();
69+
logger.log(Level.SEVERE, "Failed to execute " + function.getClass().getName(), t);
7470
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
7571
} finally {
7672
Thread.currentThread().setContextClassLoader(oldContextLoader);

invoker/core/src/test/java/com/google/cloud/functions/invoker/IntegrationTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,48 @@ public void helloWorld() throws Exception {
248248
ROBOTS_TXT_TEST_CASE));
249249
}
250250

251+
@Test
252+
public void exceptionHttp() throws Exception {
253+
String exceptionExpectedOutput =
254+
"\"severity\": \"ERROR\", \"logging.googleapis.com/sourceLocation\": {\"file\":"
255+
+ " \"com/google/cloud/functions/invoker/HttpFunctionExecutor.java\", \"method\":"
256+
+ " \"service\"}, \"message\": \"Failed to execute"
257+
+ " com.google.cloud.functions.invoker.testfunctions.ExceptionHttp\\n"
258+
+ "java.lang.RuntimeException: exception thrown for test";
259+
testHttpFunction(
260+
fullTarget("ExceptionHttp"),
261+
ImmutableList.of(
262+
TestCase.builder()
263+
.setExpectedResponseCode(500)
264+
.setExpectedOutput(exceptionExpectedOutput)
265+
.build()));
266+
}
267+
268+
269+
@Test
270+
public void exceptionBackground() throws Exception {
271+
String exceptionExpectedOutput =
272+
"\"severity\": \"ERROR\", \"logging.googleapis.com/sourceLocation\": {\"file\":"
273+
+ " \"com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java\", \"method\":"
274+
+ " \"service\"}, \"message\": \"Failed to execute"
275+
+ " com.google.cloud.functions.invoker.testfunctions.ExceptionBackground\\n"
276+
+ "java.lang.RuntimeException: exception thrown for test";
277+
278+
File snoopFile = snoopFile();
279+
String gcfRequestText = sampleLegacyEvent(snoopFile);
280+
281+
testFunction(
282+
SignatureType.BACKGROUND,
283+
fullTarget("ExceptionBackground"),
284+
ImmutableList.of(),
285+
ImmutableList.of(
286+
TestCase.builder()
287+
.setRequestText(gcfRequestText)
288+
.setExpectedResponseCode(500)
289+
.setExpectedOutput(exceptionExpectedOutput)
290+
.build()));
291+
}
292+
251293
@Test
252294
public void echo() throws Exception {
253295
String testText = "hello\nworld\n";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.google.cloud.functions.invoker.testfunctions;
2+
3+
import com.google.cloud.functions.Context;
4+
import com.google.cloud.functions.RawBackgroundFunction;
5+
6+
public class ExceptionBackground implements RawBackgroundFunction {
7+
@Override
8+
public void accept(String json, Context context) {
9+
throw new RuntimeException("exception thrown for test");
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.google.cloud.functions.invoker.testfunctions;
2+
3+
import com.google.cloud.functions.HttpFunction;
4+
import com.google.cloud.functions.HttpRequest;
5+
import com.google.cloud.functions.HttpResponse;
6+
7+
public class ExceptionHttp implements HttpFunction {
8+
@Override
9+
public void service(HttpRequest request, HttpResponse response) throws Exception {
10+
throw new RuntimeException("exception thrown for test");
11+
}
12+
}

0 commit comments

Comments
 (0)