[R9] Refactor entity not found error handler to display messages

This commit is contained in:
Mirna Gama 2024-01-13 17:32:24 -03:00 committed by Mirna Gama
parent 8dbc0f4f01
commit ce519740ef
3 changed files with 9 additions and 12 deletions

View File

@ -67,7 +67,7 @@ public class DoctorServiceImpl implements DoctorService {
public Doctor findDoctorById(Long id) throws EntityNotFoundException {
Doctor doctor = findDoctorById.execute(id);
if (doctor == null) throw new EntityNotFoundException();
if (doctor == null) throw new EntityNotFoundException("No existing doctor with this id");
return doctor;
}
@ -97,7 +97,7 @@ public class DoctorServiceImpl implements DoctorService {
Doctor doctor = findDoctorById.execute(doctorUpdatedDataDTO.id());
if (doctor == null) {
throw new EntityNotFoundException();
throw new EntityNotFoundException("No existing doctor with this id");
}
if (doctorUpdatedDataDTO.name() != null) {
@ -163,7 +163,7 @@ public class DoctorServiceImpl implements DoctorService {
Doctor doctor = findDoctorById.execute(id);
if (doctor == null) {
throw new EntityNotFoundException();
throw new EntityNotFoundException("No existing doctor with this id");
}
doctor.setActive(false);

View File

@ -15,7 +15,6 @@ import com.mirna.hospitalmanagementapi.domain.dtos.patient.PatientUpdatedDataDTO
import com.mirna.hospitalmanagementapi.domain.entities.Address;
import com.mirna.hospitalmanagementapi.domain.entities.Patient;
import com.mirna.hospitalmanagementapi.domain.services.PatientService;
import com.mirna.hospitalmanagementapi.infra.handlers.EntityNotFoundErrorHandler;
import jakarta.persistence.EntityNotFoundException;
@ -65,7 +64,7 @@ public class PatientServiceImpl implements PatientService {
public Patient findPatientById(Long id) throws EntityNotFoundException {
Patient patient = this.findPatientById.execute(id);
if (patient == null) throw new EntityNotFoundException();
if (patient == null) throw new EntityNotFoundException("No existing patient with this id");
return patient;
}
@ -93,7 +92,7 @@ public class PatientServiceImpl implements PatientService {
Patient patient = findPatientById.execute(patientUpdatedDataDTO.id());
if (patient == null) throw new EntityNotFoundException();
if (patient == null) throw new EntityNotFoundException("No existing patient with this id");
if (patientUpdatedDataDTO.name() != null) patient.setName(patientUpdatedDataDTO.name());
@ -154,7 +153,7 @@ public class PatientServiceImpl implements PatientService {
Patient patient = findPatientById.execute(id);
if (patient == null) {
throw new EntityNotFoundException();
throw new EntityNotFoundException("No existing patient with this id");
}
patient.setActive(false);

View File

@ -4,6 +4,7 @@ import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@ -18,12 +19,9 @@ import jakarta.persistence.EntityNotFoundException;
*/
@RestControllerAdvice
public class EntityNotFoundErrorHandler {
@Autowired
private MessageSource messageSource;
@ExceptionHandler({EntityNotFoundException.class})
public ResponseEntity<Object> handle(EntityNotFoundException expection) {
return ResponseEntity.notFound().build();
public ResponseEntity<Object> handle(EntityNotFoundException exception) {
return new ResponseEntity<>(exception.getMessage(), HttpStatus.NOT_FOUND);
}
}