[R5] Error handler for entity not found error

This commit is contained in:
Mirna Gama 2024-01-06 18:09:39 -03:00 committed by Mirna Gama
parent be03545783
commit 8266f5758d

View File

@ -0,0 +1,29 @@
package com.mirna.hospitalmanagementapi.infra.handlers;
import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import jakarta.persistence.EntityNotFoundException;
/**
* This @RestControllerAdvice is used to handle entity not found error and return appropriate response message
*
* @author Mirna Gama
* @version 1.0
*/
@RestControllerAdvice
public class EntityNotFoundErrorHandler {
@Autowired
private MessageSource messageSource;
@ExceptionHandler({EntityNotFoundException.class})
public ResponseEntity<Object> handle(EntityNotFoundException expection) {
return ResponseEntity.notFound().build();
}
}