Skip to content
Snippets Groups Projects
Commit 5e95e282 authored by Hugues.Cazeaux's avatar Hugues.Cazeaux
Browse files

feat(SolidifyControllerAdvice): add detailed information for exception error

parent 3d06314e
Branches
Tags
1 merge request!424feat(SolidifyControllerAdvice): add detailed information for exception error
......@@ -28,6 +28,9 @@ import static ch.unige.solidify.controller.SolidifyControllerAdvice.StackTraceDi
import java.util.NoSuchElementException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
......@@ -58,8 +61,6 @@ import org.springframework.web.servlet.resource.NoResourceFoundException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import ch.unige.solidify.config.SolidifyProperties;
import ch.unige.solidify.exception.SolidifyBulkActionException;
......@@ -72,6 +73,7 @@ import ch.unige.solidify.exception.SolidifyUndeletableException;
import ch.unige.solidify.exception.SolidifyUnmodifiableException;
import ch.unige.solidify.exception.SolidifyValidationException;
import ch.unige.solidify.service.MessageService;
import ch.unige.solidify.util.StringTool;
import ch.unige.solidify.validation.ValidationError;
@ControllerAdvice
......@@ -147,7 +149,7 @@ public class SolidifyControllerAdvice {
@ExceptionHandler(Exception.class)
public ResponseEntity<SolidifyError> handleException(Exception exception, HttpServletRequest request) {
this.logException(exception, request, WITH_STACK_TRACE);
this.logException(exception, null, request, WITH_STACK_TRACE);
final SolidifyError error = new SolidifyError(this.errorAttributes, request);
error.setMessage(exception.getMessage());
return ResponseEntity.status(error.getStatus()).headers(this.headers).body(error);
......@@ -163,7 +165,7 @@ public class SolidifyControllerAdvice {
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<SolidifyError> handleException(HttpMessageNotReadableException exception, HttpServletRequest request) {
this.logException(exception, request, WITH_STACK_TRACE);
this.logException(exception, null, request, WITH_STACK_TRACE);
String message = this.messageService.get("error.request.notreadable");
......@@ -242,7 +244,7 @@ public class SolidifyControllerAdvice {
@ExceptionHandler(SolidifyRestException.class)
public ResponseEntity<SolidifyError> handleHttpRestClientException(SolidifyRestException exception, HttpServletRequest request) {
this.logException(exception, request, WITH_STACK_TRACE);
this.logException(exception, null, request, WITH_STACK_TRACE);
SolidifyError error = exception.getSourceError();
if (error == null) {
error = new SolidifyError(this.errorAttributes, request);
......@@ -297,7 +299,7 @@ public class SolidifyControllerAdvice {
}
protected ResponseEntity<SolidifyError> buildBadRequestResponse(Exception exception, HttpServletRequest request, SolidifyError solidifyError) {
this.logException(exception, request, this.badRequestStacktrace);
this.logException(exception, null, request, this.badRequestStacktrace);
this.logErrorAsJson(solidifyError);
return ResponseEntity.badRequest().headers(this.headers).body(solidifyError);
}
......@@ -315,7 +317,7 @@ public class SolidifyControllerAdvice {
protected ResponseEntity<SolidifyError> buildResponse(Exception exception, HttpServletRequest request, String technicalMessage,
HttpStatusCode httpStatusCode, StackTraceDisplay withStackTrace) {
this.logException(exception, request, withStackTrace);
this.logException(exception, technicalMessage, request, withStackTrace);
final SolidifyError error = new SolidifyError(this.errorAttributes, request);
error.setMessage(exception.getMessage()); // Message for user (i18n)
......@@ -334,14 +336,16 @@ public class SolidifyControllerAdvice {
}
}
protected void logException(Exception exception, HttpServletRequest request, StackTraceDisplay stackTraceDisplay) {
protected void logException(Exception exception, String technicalMessage, HttpServletRequest request, StackTraceDisplay stackTraceDisplay) {
final String headerXFF = request.getHeader("X-FORWARDED-FOR");
String message = "Request url {} {} from remote address {} with X-FORWARDED-FOR {} has generated an exception caught by controller advice:";
if (stackTraceDisplay == WITH_STACK_TRACE) {
log.error(message, request.getMethod(), request.getRequestURL(), request.getRemoteAddr(), headerXFF, exception);
} else if (stackTraceDisplay == WITHOUT_STACK_TRACE) {
if (stackTraceDisplay == WITHOUT_STACK_TRACE) {
message += " {}";
log.info(message, request.getMethod(), request.getRequestURL(), request.getRemoteAddr(), headerXFF, exception);
}
log.error(message, request.getMethod(), request.getRequestURL(), request.getRemoteAddr(), headerXFF, exception);
// Log technical information
if (!StringTool.isNullOrEmpty(technicalMessage)) {
log.error("Exception error details: {}", technicalMessage);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment