feat(ExceptionController): implementados ExceptionHandlers para las excepciones personalizadas

parent 4d798a1d
package com.example.apprecetas.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.time.LocalDateTime;
@RestControllerAdvice
public class ExceptionController {
//MethodArgumentNotValidException
@ExceptionHandler(UnvalidInputEntityException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorMessage> handle(UnvalidInputEntityException ex) {
return new ResponseEntity<>(new ErrorMessage(LocalDateTime.now(), HttpStatus.BAD_REQUEST.value(),
ex.getMessage()), HttpStatusCode.valueOf(HttpStatus.BAD_REQUEST.value()));
}
@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ErrorMessage> handle(EntityNotFoundException ex) {
return new ResponseEntity<>(new ErrorMessage(LocalDateTime.now(), HttpStatus.NOT_FOUND.value(),
ex.getMessage()), HttpStatusCode.valueOf(HttpStatus.NOT_FOUND.value()));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment