Skip to content

Commit

Permalink
fix null pointer exception
Browse files Browse the repository at this point in the history
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@zalando.de>
  • Loading branch information
zeitlinger committed Apr 23, 2021
1 parent 314c659 commit 54de9cd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
@@ -1,19 +1,19 @@
package org.zalando.opentracing.spring.webflux.extension;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

import io.opentracing.Span;
import io.opentracing.log.Fields;
import io.opentracing.tag.Tags;
import lombok.AllArgsConstructor;
import org.apiguardian.api.API;
import static org.apiguardian.api.API.Status.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

import static org.apiguardian.api.API.Status.EXPERIMENTAL;

@API(status = EXPERIMENTAL)
@AllArgsConstructor
public final class ErrorSpanDecorator
Expand All @@ -30,7 +30,8 @@ public void onResponse(
final ServerWebExchange exchange,
final Span span) {

if (predicate.test(exchange.getResponse().getStatusCode())) {
HttpStatus statusCode = exchange.getResponse().getStatusCode();
if (statusCode == null || predicate.test(statusCode)) {
span.setTag(Tags.ERROR, true);
}
}
Expand Down
@@ -0,0 +1,23 @@
package org.zalando.opentracing.spring.webflux.extension;

import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;

class ErrorSpanDecoratorTest {

@Test
void onTagNullStatusCode() {
ServerWebExchange exchange = mock(ServerWebExchange.class);
when(exchange.getResponse()).thenReturn(mock(ServerHttpResponse.class));

MockSpan span = new MockTracer().buildSpan("span").start();
new ErrorSpanDecorator().onResponse(exchange, span);

assertThat(span.tags().get("error")).isEqualTo(true);
}
}

0 comments on commit 54de9cd

Please sign in to comment.