// Path: spat-services/src/main/java/com/pcalouche/spat/exception/ApiErrorResponse.java
// @Getter
// @EqualsAndHashCode
// public class ApiErrorResponse {
//     private static final ObjectMapper objectMapper = new ObjectMapper();
//     private static final String VALIDATION_MESSAGES = "Validation failed. See validation messages.";
//     private final String timestamp;
//     private final String path;
//     private final String message;
//     private final String exception;
//     @JsonIgnore
//     private final HttpStatus httpStatus;
//     @JsonInclude(JsonInclude.Include.NON_NULL)
//     private final Map<String, String> validationMessages;
// 
//     public ApiErrorResponse(Exception e, HttpServletRequest request) {
//         timestamp = ZonedDateTime.now().toOffsetDateTime().toString();
//         path = StringUtils.isNotBlank(request.getQueryString()) ?
//                 request.getRequestURI() + "?" + request.getQueryString()
//                 :
//                 request.getRequestURI();
//         exception = e.getClass().getName();
// 
//         if (e instanceof AuthenticationException ||
//                 e instanceof JwtException) {
//             httpStatus = HttpStatus.UNAUTHORIZED;
//         } else if (e instanceof AccessDeniedException) {
//             httpStatus = HttpStatus.FORBIDDEN;
//         } else if (e instanceof HttpMessageConversionException ||
//                 e instanceof MethodArgumentNotValidException ||
//                 e instanceof MethodArgumentTypeMismatchException ||
//                 e instanceof BindException) {
//             httpStatus = HttpStatus.UNPROCESSABLE_ENTITY;
//         } else if (e instanceof ApiException) {
//             httpStatus = ((ApiException) e).getStatus();
//         } else {
//             httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
//         }
// 
//         if (e instanceof MethodArgumentNotValidException || e instanceof BindException) {
//             message = VALIDATION_MESSAGES;
//             validationMessages = getValidationMessages(e);
//         } else {
//             message = e.getMessage();
//             validationMessages = null;
//         }
//     }
// 
//     public static void writeExceptionToResponse(Exception e, HttpServletRequest request, HttpServletResponse response)
//             throws IOException {
//         ApiErrorResponse apiErrorResponse = new ApiErrorResponse(e, request);
//         response.setStatus(apiErrorResponse.getStatus());
//         response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//         objectMapper.writeValue(response.getWriter(), apiErrorResponse);
//     }
// 
//     private static Map<String, String> getValidationMessages(Exception e) {
//         Map<String, String> validateMessages = new HashMap<>();
//         List<FieldError> fieldErrors;
//         if (e instanceof MethodArgumentNotValidException) {
//             MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
//             fieldErrors = methodArgumentNotValidException.getBindingResult().getFieldErrors();
//         } else {
//             BindException bindException = (BindException) e;
//             fieldErrors = bindException.getBindingResult().getFieldErrors();
//         }
//         fieldErrors.forEach(fieldError -> validateMessages.put(fieldError.getField(), fieldError.getDefaultMessage()));
//         return validateMessages;
//     }
// 
//     public int getStatus() {
//         return httpStatus.value();
//     }
// 
//     public String getReasonPhrase() {
//         return httpStatus.getReasonPhrase();
//     }
// }


import com.pcalouche.spat.exception.ApiErrorResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
package com.pcalouche.spat.api;



@ControllerAdvice
public class ControllerExceptionAdvice {
    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionAdvice.class);

    @ExceptionHandler({Exception.class})
// Path: spat-services/src/main/java/com/pcalouche/spat/exception/ApiErrorResponse.java
// @Getter
// @EqualsAndHashCode
// public class ApiErrorResponse {
//     private static final ObjectMapper objectMapper = new ObjectMapper();
//     private static final String VALIDATION_MESSAGES = "Validation failed. See validation messages.";
//     private final String timestamp;
//     private final String path;
//     private final String message;
//     private final String exception;
//     @JsonIgnore
//     private final HttpStatus httpStatus;
//     @JsonInclude(JsonInclude.Include.NON_NULL)
//     private final Map<String, String> validationMessages;
// 
//     public ApiErrorResponse(Exception e, HttpServletRequest request) {
//         timestamp = ZonedDateTime.now().toOffsetDateTime().toString();
//         path = StringUtils.isNotBlank(request.getQueryString()) ?
//                 request.getRequestURI() + "?" + request.getQueryString()
//                 :
//                 request.getRequestURI();
//         exception = e.getClass().getName();
// 
//         if (e instanceof AuthenticationException ||
//                 e instanceof JwtException) {
//             httpStatus = HttpStatus.UNAUTHORIZED;
//         } else if (e instanceof AccessDeniedException) {
//             httpStatus = HttpStatus.FORBIDDEN;
//         } else if (e instanceof HttpMessageConversionException ||
//                 e instanceof MethodArgumentNotValidException ||
//                 e instanceof MethodArgumentTypeMismatchException ||
//                 e instanceof BindException) {
//             httpStatus = HttpStatus.UNPROCESSABLE_ENTITY;
//         } else if (e instanceof ApiException) {
//             httpStatus = ((ApiException) e).getStatus();
//         } else {
//             httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
//         }
// 
//         if (e instanceof MethodArgumentNotValidException || e instanceof BindException) {
//             message = VALIDATION_MESSAGES;
//             validationMessages = getValidationMessages(e);
//         } else {
//             message = e.getMessage();
//             validationMessages = null;
//         }
//     }
// 
//     public static void writeExceptionToResponse(Exception e, HttpServletRequest request, HttpServletResponse response)
//             throws IOException {
//         ApiErrorResponse apiErrorResponse = new ApiErrorResponse(e, request);
//         response.setStatus(apiErrorResponse.getStatus());
//         response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//         objectMapper.writeValue(response.getWriter(), apiErrorResponse);
//     }
// 
//     private static Map<String, String> getValidationMessages(Exception e) {
//         Map<String, String> validateMessages = new HashMap<>();
//         List<FieldError> fieldErrors;
//         if (e instanceof MethodArgumentNotValidException) {
//             MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
//             fieldErrors = methodArgumentNotValidException.getBindingResult().getFieldErrors();
//         } else {
//             BindException bindException = (BindException) e;
//             fieldErrors = bindException.getBindingResult().getFieldErrors();
//         }
//         fieldErrors.forEach(fieldError -> validateMessages.put(fieldError.getField(), fieldError.getDefaultMessage()));
//         return validateMessages;
//     }
// 
//     public int getStatus() {
//         return httpStatus.value();
//     }
// 
//     public String getReasonPhrase() {
//         return httpStatus.getReasonPhrase();
//     }
// }


// Path: spat-services/src/main/java/com/pcalouche/spat/api/ControllerExceptionAdvice.java
import com.pcalouche.spat.exception.ApiErrorResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;

package com.pcalouche.spat.api;



@ControllerAdvice
public class ControllerExceptionAdvice {
    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionAdvice.class);

    @ExceptionHandler({Exception.class})
    public ResponseEntity<ApiErrorResponse> handleException(Exception e, HttpServletRequest request) {