Skip to content
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

Jsf: simplify asserting exception #11736

Merged
merged 1 commit into from
Jul 4, 2024
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 @@ -17,7 +17,6 @@
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.semconv.ClientAttributes;
import io.opentelemetry.semconv.ExceptionAttributes;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
Expand All @@ -30,7 +29,6 @@
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import io.opentelemetry.testing.internal.armeria.common.QueryParams;
import io.opentelemetry.testing.internal.armeria.common.RequestHeaders;
import jakarta.servlet.ServletException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -257,7 +255,7 @@ void testException() {
AggregatedHttpResponse response2 = client.execute(request2).aggregate().join();
assertThat(response2.status().code()).isEqualTo(500);

ServletException ex = new ServletException("submit exception");
IllegalStateException expectedException = new IllegalStateException("submit exception");

testing.waitAndAssertTraces(
trace ->
Expand All @@ -267,21 +265,8 @@ void testException() {
.hasKind(SpanKind.SERVER)
.hasNoParent()
.hasStatus(StatusData.error())
.hasEventsSatisfyingExactly(
event ->
event
.hasName("exception")
.hasAttributesSatisfyingExactly(
equalTo(
ExceptionAttributes.EXCEPTION_TYPE,
ex.getClass().getName()),
satisfies(
ExceptionAttributes.EXCEPTION_STACKTRACE,
stacktrace -> stacktrace.contains("submit exception")),
satisfies(
ExceptionAttributes.EXCEPTION_MESSAGE,
message -> message.endsWith(ex.getMessage())))),
span -> handlerSpan(trace, 0, "#{greetingForm.submit()}", ex)));
.hasException(expectedException),
span -> handlerSpan(trace, 0, "#{greetingForm.submit()}", expectedException)));
}

List<Consumer<SpanDataAssert>> handlerSpan(
Expand All @@ -295,22 +280,7 @@ List<Consumer<SpanDataAssert>> handlerSpan(
.hasParent(trace.getSpan(parentIndex))));

if (expectedException != null) {
assertions.add(
span ->
span.hasStatus(StatusData.error())
.hasEventsSatisfyingExactly(
event ->
event
.hasName("exception")
.hasAttributesSatisfyingExactly(
equalTo(
ExceptionAttributes.EXCEPTION_TYPE,
expectedException.getClass().getName()),
satisfies(
ExceptionAttributes.EXCEPTION_MESSAGE,
message ->
message.startsWithIgnoringCase(
expectedException.getMessage())))));
assertions.add(span -> span.hasStatus(StatusData.error()).hasException(expectedException));
}
return assertions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import java.io.IOException;

public class ExceptionFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException {
throws ServletException, IOException {
try {
chain.doFilter(request, response);
} catch (Exception exception) {
throw new ServletException(exception);
} catch (ServletException exception) {
// to ease testing unwrap our exception to root cause
Throwable tmp = exception;
while (tmp.getCause() != null) {
tmp = tmp.getCause();
}
if (tmp.getMessage() != null && tmp.getMessage().contains("submit exception")) {
throw (IllegalStateException) tmp;
}
throw exception;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public String getMessage() {
return message;
}

public void submit() throws Exception {
public void submit() {
message = "Hello " + name;
if (name.equals("exception")) {
throw new Exception("submit exception");
throw new IllegalStateException("submit exception");
}
}
}
Loading