Piroca doce

This commit is contained in:
2026-05-12 20:30:14 -03:00
commit 0e39524e00
57 changed files with 3774 additions and 0 deletions
@@ -0,0 +1,53 @@
package com.agendaestudantil.excecao;
import com.agendaestudantil.dto.RespostaApi;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class ManipuladorExcecaoGlobal {
@ExceptionHandler(ExcecaoRecursoNaoEncontrado.class)
public ResponseEntity<RespostaApi<Void>> handleResourceNotFound(ExcecaoRecursoNaoEncontrado ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new RespostaApi<>(null, ex.getMessage(), LocalDateTime.now()));
}
@ExceptionHandler(ExcecaoNegocio.class)
public ResponseEntity<RespostaApi<Void>> handleExcecaoNegocio(ExcecaoNegocio ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new RespostaApi<>(null, ex.getMessage(), LocalDateTime.now()));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<RespostaApi<Map<String, String>>> handleValidationException(
MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
for (FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new RespostaApi<>(errors, "Falha na validação", LocalDateTime.now()));
}
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<RespostaApi<Void>> handleResponseStatusException(ResponseStatusException ex) {
return ResponseEntity.status(ex.getStatusCode())
.body(new RespostaApi<>(null, ex.getReason(), LocalDateTime.now()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<RespostaApi<Void>> handleGenericException(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new RespostaApi<>(null, "Erro interno no servidor: " + ex.getMessage(), LocalDateTime.now()));
}
}