// Path: src/main/java/com/edwise/completespring/exceptions/InvalidRequestException.java
// public class InvalidRequestException extends RuntimeException {
// 
//     @Getter
//     private final ErrorInfo errors;
// 
//     public InvalidRequestException(BindingResult bindingResultErrors) {
//         super(bindingResultErrors.toString());
//         this.errors = ErrorInfo.generateErrorInfoFromBindingResult(bindingResultErrors);
//     }
// 
// }
// 
// Path: src/main/java/com/edwise/completespring/exceptions/NotFoundException.java
// public class NotFoundException extends RuntimeException {
// 
//     public NotFoundException(String msg) {
//         super(msg);
//     }
// }
// 
// Path: src/main/java/com/edwise/completespring/exceptions/helpers/ErrorInfo.java
// @Accessors(chain = true)
// @EqualsAndHashCode(doNotUseGetters = true)
// @ToString(doNotUseGetters = true)
// public class ErrorInfo implements Serializable {
// 
//     private static final long serialVersionUID = 673514291953827696L;
// 
//     @Getter
//     @Setter
//     private String url;
// 
//     @Getter
//     private List<ErrorItem> errors = new ArrayList<>();
// 
//     public ErrorInfo addError(String field, String message) {
//         errors.add(new ErrorItem().setField(field).setMessage(message));
//         return this;
//     }
// 
//     public static ErrorInfo generateErrorInfoFromBindingResult(BindingResult errors) {
//         ErrorInfo errorInfo = new ErrorInfo();
//         errors.getFieldErrors()
//                 .forEach(fieldError -> errorInfo.addError(fieldError.getField(), fieldError.getDefaultMessage()));
// 
//         return errorInfo;
//     }
// }


import com.edwise.completespring.exceptions.InvalidRequestException;
import com.edwise.completespring.exceptions.NotFoundException;
import com.edwise.completespring.exceptions.helpers.ErrorInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
package com.edwise.completespring.controllers;



@ControllerAdvice
@Slf4j
public class RestExceptionProcessor {
    private static final String FIELD_ID = "id";

    @ExceptionHandler(NotFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ResponseBody
    public ErrorInfo entityNotFound(HttpServletRequest req, NotFoundException ex) {
        String errorURL = req.getRequestURL().toString();

        log.warn("Entity not found: {}", errorURL);
        return new ErrorInfo()
                .setUrl(errorURL)
                .addError(FIELD_ID, ex.getMessage());
    }


    @ExceptionHandler(InvalidRequestException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
// Path: src/main/java/com/edwise/completespring/exceptions/InvalidRequestException.java
// public class InvalidRequestException extends RuntimeException {
// 
//     @Getter
//     private final ErrorInfo errors;
// 
//     public InvalidRequestException(BindingResult bindingResultErrors) {
//         super(bindingResultErrors.toString());
//         this.errors = ErrorInfo.generateErrorInfoFromBindingResult(bindingResultErrors);
//     }
// 
// }
// 
// Path: src/main/java/com/edwise/completespring/exceptions/NotFoundException.java
// public class NotFoundException extends RuntimeException {
// 
//     public NotFoundException(String msg) {
//         super(msg);
//     }
// }
// 
// Path: src/main/java/com/edwise/completespring/exceptions/helpers/ErrorInfo.java
// @Accessors(chain = true)
// @EqualsAndHashCode(doNotUseGetters = true)
// @ToString(doNotUseGetters = true)
// public class ErrorInfo implements Serializable {
// 
//     private static final long serialVersionUID = 673514291953827696L;
// 
//     @Getter
//     @Setter
//     private String url;
// 
//     @Getter
//     private List<ErrorItem> errors = new ArrayList<>();
// 
//     public ErrorInfo addError(String field, String message) {
//         errors.add(new ErrorItem().setField(field).setMessage(message));
//         return this;
//     }
// 
//     public static ErrorInfo generateErrorInfoFromBindingResult(BindingResult errors) {
//         ErrorInfo errorInfo = new ErrorInfo();
//         errors.getFieldErrors()
//                 .forEach(fieldError -> errorInfo.addError(fieldError.getField(), fieldError.getDefaultMessage()));
// 
//         return errorInfo;
//     }
// }


// Path: src/main/java/com/edwise/completespring/controllers/RestExceptionProcessor.java
import com.edwise.completespring.exceptions.InvalidRequestException;
import com.edwise.completespring.exceptions.NotFoundException;
import com.edwise.completespring.exceptions.helpers.ErrorInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;

package com.edwise.completespring.controllers;



@ControllerAdvice
@Slf4j
public class RestExceptionProcessor {
    private static final String FIELD_ID = "id";

    @ExceptionHandler(NotFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ResponseBody
    public ErrorInfo entityNotFound(HttpServletRequest req, NotFoundException ex) {
        String errorURL = req.getRequestURL().toString();

        log.warn("Entity not found: {}", errorURL);
        return new ErrorInfo()
                .setUrl(errorURL)
                .addError(FIELD_ID, ex.getMessage());
    }


    @ExceptionHandler(InvalidRequestException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorInfo invalidPostData(HttpServletRequest req, InvalidRequestException ex) {