Skip to content

Commit 591d625

Browse files
authored
Fix NPE when serializing an Exception with a null message (#45)
1 parent a51de71 commit 591d625

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ecs-logging-core/src/main/java/co/elastic/logging/EcsJsonSerializer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,13 @@ public static void serializeException(StringBuilder builder, Throwable thrown, b
159159
builder.append("\"error.type\":\"");
160160
JsonUtils.quoteAsString(thrown.getClass().getName(), builder);
161161
builder.append("\",");
162-
builder.append("\"error.message\":\"");
163-
JsonUtils.quoteAsString(thrown.getMessage(), builder);
164-
builder.append("\",");
162+
163+
String message = thrown.getMessage();
164+
if (message != null) {
165+
builder.append("\"error.message\":\"");
166+
JsonUtils.quoteAsString(message, builder);
167+
builder.append("\",");
168+
}
165169
if (stackTraceAsArray) {
166170
builder.append("\"error.stack_trace\":[").append(NEW_LINE);
167171
formatThrowableAsArray(builder, thrown);

ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ void testLogException() throws Exception {
103103
assertThat(stackTrace).contains("at co.elastic.logging.AbstractEcsLoggingTest.testLogException");
104104
}
105105

106+
@Test
107+
void testLogExceptionNullMessage() throws Exception {
108+
error("test", new RuntimeException());
109+
assertThat(getLastLogLine().get("error.message")).isNull();
110+
assertThat(getLastLogLine().get("error.type").textValue()).isEqualTo(RuntimeException.class.getName());
111+
}
112+
106113
@Test
107114
void testLogOrigin() throws Exception {
108115
debug("test");

0 commit comments

Comments
 (0)