Skip to content

Commit

Permalink
Only decorate in Dev Mode
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Jul 18, 2024
1 parent 11480d2 commit b043849
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ public class HttpConfiguration {
@ConfigItem
public Map<String, FilterConfig> filter;

/**
* This will decorate the stacktrace to show the line in the code that cause the exception
* By default, this is enabled in Dev Mode and disabled in all other modes. Making this true will enable it in all mode
* and making this false will disable this in all modes.
*/
@ConfigItem
public Optional<Boolean> decorateStacktraces;

public ProxyConfig proxy;

public int determinePort(LaunchMode launchMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public class QuarkusErrorHandler implements Handler<RoutingContext> {
private static final AtomicLong ERROR_COUNT = new AtomicLong();

private final boolean showStack;
private final boolean decorateStack;
private final Optional<HttpConfiguration.PayloadHint> contentTypeDefault;

public QuarkusErrorHandler(boolean showStack, Optional<HttpConfiguration.PayloadHint> contentTypeDefault) {
public QuarkusErrorHandler(boolean showStack, boolean decorateStack,
Optional<HttpConfiguration.PayloadHint> contentTypeDefault) {
this.showStack = showStack;
this.decorateStack = decorateStack;
this.contentTypeDefault = contentTypeDefault;
}

Expand Down Expand Up @@ -129,7 +132,9 @@ public void accept(Throwable throwable) {
exception.addSuppressed(e);
}
if (showStack && exception != null) {
exception = new DecoratedAssertionError(exception);
if (decorateStack) {
exception = new DecoratedAssertionError(exception);
}
details = generateHeaderMessage(exception, uuid);
stack = generateStackTrace(exception);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ public void finalizeRouter(BeanContainer container, Consumer<Route> defaultRoute

applyCompression(httpBuildTimeConfig.enableCompression, httpRouteRouter);
httpRouteRouter.route().last().failureHandler(
new QuarkusErrorHandler(launchMode.isDevOrTest(), httpConfiguration.unhandledErrorContentTypeDefault));
new QuarkusErrorHandler(launchMode.isDevOrTest(), decorateStacktrace(launchMode, httpConfiguration),
httpConfiguration.unhandledErrorContentTypeDefault));

for (BooleanSupplier requireBodyHandlerCondition : requireBodyHandlerConditions) {
if (requireBodyHandlerCondition.getAsBoolean()) {
Expand Down Expand Up @@ -533,7 +534,8 @@ public void handle(RoutingContext event) {
addHotReplacementHandlerIfNeeded(mr);

mr.route().last().failureHandler(
new QuarkusErrorHandler(launchMode.isDevOrTest(), httpConfiguration.unhandledErrorContentTypeDefault));
new QuarkusErrorHandler(launchMode.isDevOrTest(), decorateStacktrace(launchMode, httpConfiguration),
httpConfiguration.unhandledErrorContentTypeDefault));

mr.route().order(RouteConstants.ROUTE_ORDER_BODY_HANDLER_MANAGEMENT)
.handler(createBodyHandlerForManagementInterface());
Expand Down Expand Up @@ -574,6 +576,12 @@ public void handle(HttpServerRequest event) {
}
}

private boolean decorateStacktrace(LaunchMode launchMode, HttpConfiguration httpConfiguration) {
return httpConfiguration.decorateStacktraces.orElseGet(() -> {
return launchMode.equals(LaunchMode.DEVELOPMENT);
});
}

private void addHotReplacementHandlerIfNeeded(Router router) {
if (hotReplacementHandler != null) {
//recorders are always executed in the current CL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void string_with_quotes_should_be_correctly_escaped() {

@Test
public void json_content_type_hint_should_be_respected_if_not_accepted() {
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, Optional.of(HttpConfiguration.PayloadHint.JSON));
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, false,
Optional.of(HttpConfiguration.PayloadHint.JSON));
Mockito.when(routingContext.failure()).thenReturn(testError);
Mockito.when(routingContext.parsedHeaders().findBestUserAcceptedIn(any(), any()))
.thenReturn(new ParsableMIMEValue("application/foo+json").forceParse());
Expand All @@ -62,7 +63,8 @@ public void json_content_type_hint_should_be_respected_if_not_accepted() {

@Test
public void json_content_type_hint_should_be_ignored_if_accepted() {
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, Optional.of(HttpConfiguration.PayloadHint.JSON));
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, false,
Optional.of(HttpConfiguration.PayloadHint.JSON));
Mockito.when(routingContext.failure()).thenReturn(testError);
Mockito.when(routingContext.parsedHeaders().findBestUserAcceptedIn(any(), any()))
.thenReturn(new ParsableMIMEValue("text/html").forceParse());
Expand All @@ -73,7 +75,7 @@ public void json_content_type_hint_should_be_ignored_if_accepted() {

@Test
public void content_type_should_default_to_json_if_accepted() {
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, Optional.empty());
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, false, Optional.empty());
Mockito.when(routingContext.failure()).thenReturn(testError);
Mockito.when(routingContext.parsedHeaders().findBestUserAcceptedIn(any(), any()))
.thenReturn(new ParsableMIMEValue("application/json").forceParse());
Expand All @@ -84,7 +86,8 @@ public void content_type_should_default_to_json_if_accepted() {

@Test
public void html_content_type_hint_should_be_respected_if_not_accepted() {
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, Optional.of(HttpConfiguration.PayloadHint.HTML));
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, false,
Optional.of(HttpConfiguration.PayloadHint.HTML));
Mockito.when(routingContext.failure()).thenReturn(testError);
Mockito.when(routingContext.parsedHeaders().findBestUserAcceptedIn(any(), any()))
.thenReturn(new ParsableMIMEValue("application/foo+json").forceParse());
Expand All @@ -94,7 +97,8 @@ public void html_content_type_hint_should_be_respected_if_not_accepted() {

@Test
public void html_content_type_hint_should_be_ignored_if_accepted() {
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, Optional.of(HttpConfiguration.PayloadHint.HTML));
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, false,
Optional.of(HttpConfiguration.PayloadHint.HTML));
Mockito.when(routingContext.failure()).thenReturn(testError);
Mockito.when(routingContext.parsedHeaders().findBestUserAcceptedIn(any(), any()))
.thenReturn(new ParsableMIMEValue("application/json").forceParse());
Expand All @@ -105,7 +109,7 @@ public void html_content_type_hint_should_be_ignored_if_accepted() {

@Test
public void content_type_should_default_to_html_if_accepted() {
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, Optional.empty());
QuarkusErrorHandler errorHandler = new QuarkusErrorHandler(false, false, Optional.empty());
Mockito.when(routingContext.failure()).thenReturn(testError);
Mockito.when(routingContext.parsedHeaders().findBestUserAcceptedIn(any(), any()))
.thenReturn(new ParsableMIMEValue("text/html").forceParse());
Expand Down

0 comments on commit b043849

Please sign in to comment.