Skip to content

Commit

Permalink
EA-3236 add the stacktrace param and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SrishtiSingh-eu committed Aug 28, 2023
1 parent 840a335 commit 2c70a32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import jakarta.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -23,6 +25,9 @@ class GlobalExceptionHandler {

private static final Logger LOG = LogManager.getLogger(GlobalExceptionHandler.class);

@Value("${server.error.include-stacktrace:ON_PARAM}")
private ErrorProperties.IncludeStacktrace includeStacktraceConfig;

// with Spring boot 3 (and Spring Framework 6) require a baseline of Jakarte EE 10
// You cannot use it with Java EE or Jakarte EE versions below that.
// You have to remove the explicit dependency on jakarta.servlet-api from your pom.xml.
Expand All @@ -41,11 +46,15 @@ protected void logException(RecordApiException e) {

}

protected boolean stackTraceEnabled() {
return this.includeStacktraceConfig != ErrorProperties.IncludeStacktrace.NEVER;
}


@ExceptionHandler
public ResponseEntity<ApiErrorResponse> handleBaseException(RecordApiException e, HttpServletRequest httpRequest) {
this.logException(e);
ApiErrorResponse response = (new ApiErrorResponse.Builder(httpRequest, e, false)).setStatus(e.getResponseStatus().value()).setError
ApiErrorResponse response = (new ApiErrorResponse.Builder(httpRequest, e, this.stackTraceEnabled())).setStatus(e.getResponseStatus().value()).setError
(e.getResponseStatus().getReasonPhrase()).setMessage(e.doExposeMessage() ? e.getMessage() : null).setCode(e.getErrorCode()).build();
return ResponseEntity.status(e.getResponseStatus()).headers(this.createHttpHeaders()).body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import eu.europeana.api.commons.error.ResponseUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

Expand All @@ -12,21 +13,25 @@
@JsonPropertyOrder({"success", "status", "error", "message", "timestamp", "path"})
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ApiErrorResponse {

private final boolean success = false;
private final int status;
private final String error;
private final String trace;
private final String message;

@JsonFormat(
pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"
)
private final OffsetDateTime timestamp = OffsetDateTime.now();
private final String path;
private final String code;

private ApiErrorResponse(int status, String error, String message, String path, String code) {
private ApiErrorResponse(int status, String error, String message, String trace, String path, String code) {
this.status = status;
this.error = error;
this.message = message;
this.trace = trace;
this.path = path;
this.code = code;
}
Expand Down Expand Up @@ -55,6 +60,10 @@ public String getPath() {
return this.path;
}

public String getTrace() {
return this.trace;
}

public String getCode() {
return this.code;
}
Expand All @@ -63,6 +72,7 @@ public static class Builder {
private int status;
private String message;
private String error;
private String trace;
private final String path;
private String code;

Expand All @@ -74,6 +84,10 @@ public Builder(HttpServletRequest httpRequest, Exception e, boolean stacktraceEn
includeErrorStack = List.of(profileParamString.split(",")).contains("debug");
}

if (stacktraceEnabled && includeErrorStack) {
this.trace = ResponseUtils.getExceptionStackTrace(e);
}

}

public ApiErrorResponse.Builder setStatus(int status) {
Expand All @@ -97,7 +111,7 @@ public ApiErrorResponse.Builder setCode(String code) {
}

public ApiErrorResponse build() {
return new ApiErrorResponse(this.status, this.error, this.message, this.path, this.code);
return new ApiErrorResponse(this.status, this.error, this.message, this.trace, this.path, this.code);
}
}

Expand Down

0 comments on commit 2c70a32

Please sign in to comment.