Skip to content
Open
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 @@ -12,6 +12,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
Expand All @@ -33,11 +34,13 @@
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;


@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler {

@Value("${misc.excludeStackTracesInWebResponses:false}")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@germanosin not sure on the name, any better ideas?

private boolean excludeStackTraces;

public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes,
ApplicationContext applicationContext,
ServerCodecConfigurer codecConfigurer) {
Expand Down Expand Up @@ -77,7 +80,7 @@ private Mono<ServerResponse> renderDefault(Throwable throwable, ServerRequest re
.message(coalesce(throwable.getMessage(), "Unexpected internal error"))
.requestId(requestId(request))
.timestamp(currentTimestamp())
.stackTrace(Throwables.getStackTraceAsString(throwable));
.stackTrace(formatStacktrace(throwable));
return ServerResponse
.status(ErrorCode.UNEXPECTED.httpStatus())
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -92,7 +95,7 @@ private Mono<ServerResponse> render(CustomBaseException baseException, ServerReq
.message(coalesce(baseException.getMessage(), "Internal error"))
.requestId(requestId(request))
.timestamp(currentTimestamp())
.stackTrace(Throwables.getStackTraceAsString(baseException));
.stackTrace(formatStacktrace(baseException));
return ServerResponse
.status(errorCode.httpStatus())
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -122,7 +125,7 @@ private Mono<ServerResponse> render(WebExchangeBindException exception, ServerRe
.requestId(requestId(request))
.timestamp(currentTimestamp())
.fieldsErrors(fieldsErrors)
.stackTrace(Throwables.getStackTraceAsString(exception));
.stackTrace(formatStacktrace(exception));
return ServerResponse
.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -137,7 +140,7 @@ private Mono<ServerResponse> render(ResponseStatusException exception, ServerReq
.message(msg)
.requestId(requestId(request))
.timestamp(currentTimestamp())
.stackTrace(Throwables.getStackTraceAsString(exception));
.stackTrace(formatStacktrace(exception));
return ServerResponse
.status(exception.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -166,4 +169,11 @@ private <T> T coalesce(T... items) {
return Stream.of(items).filter(Objects::nonNull).findFirst().orElse(null);
}

private String formatStacktrace(Throwable e) {
if (excludeStackTraces) {
return "REDACTED FOR SECURITY REASONS";
}
return Throwables.getStackTraceAsString(e);
}

}
Loading