1 package org.petify.shelter.exception; 2 3 import org.springframework.http.HttpHeaders; 4 import org.springframework.http.HttpStatus; 5 import org.springframework.http.HttpStatusCode; 6 import org.springframework.http.ProblemDetail; 7 import org.springframework.http.ResponseEntity; 8 import org.springframework.security.access.AccessDeniedException; 9 import org.springframework.web.bind.MethodArgumentNotValidException; 10 import org.springframework.web.bind.annotation.ExceptionHandler; 11 import org.springframework.web.bind.annotation.RestControllerAdvice; 12 import org.springframework.web.context.request.WebRequest; 13 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 14 15 import java.time.Instant; 16 import java.util.HashMap; 17 import java.util.Map; 18 19 @RestControllerAdvice 20 public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { 21 22 @ExceptionHandler(AccessDeniedException.class) 23 ProblemDetail handleAccessDeniedException(WebRequest request) { 24 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.FORBIDDEN); 25 problemDetail.setTitle("Access Denied"); 26 problemDetail.setDetail(request.getDescription(false)); 27 problemDetail.setProperty("timestamp", Instant.now()); 28 return problemDetail; 29 } 30 31 @Override 32 protected ResponseEntity<Object> handleMethodArgumentNotValid( 33 MethodArgumentNotValidException ex, 34 HttpHeaders headers, 35 HttpStatusCode status, 36 WebRequest request) { 37 38 ProblemDetail problemDetail = ProblemDetail.forStatus(status); 39 problemDetail.setTitle("Validation Failed"); 40 41 Map<String, String> errors = new HashMap<>(); 42 ex.getBindingResult().getFieldErrors().forEach(error -> 43 errors.put(error.getField(), error.getDefaultMessage()) 44 ); 45 46 problemDetail.setProperty("errors", errors); 47 48 return new ResponseEntity<>(problemDetail, headers, status); 49 } 50 51 @ExceptionHandler(ShelterAlreadyExistsException.class) 52 ProblemDetail handleShelterAlreadyExists(ShelterAlreadyExistsException ex) { 53 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.CONFLICT); 54 problemDetail.setTitle("Shelter Already Exists"); 55 problemDetail.setDetail(ex.getMessage()); 56 problemDetail.setProperty("timestamp", Instant.now()); 57 return problemDetail; 58 } 59 60 @ExceptionHandler(ShelterNotFoundException.class) 61 ProblemDetail handleShelterNotFound(ShelterNotFoundException ex) { 62 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND); 63 problemDetail.setTitle("Shelter Not Found"); 64 problemDetail.setDetail(ex.getMessage()); 65 problemDetail.setProperty("timestamp", Instant.now()); 66 return problemDetail; 67 } 68 69 @ExceptionHandler(ShelterByOwnerNotFoundException.class) 70 ProblemDetail handleShelterByOwnerNotFound(ShelterByOwnerNotFoundException ex) { 71 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND); 72 problemDetail.setTitle("Shelter Not Found"); 73 problemDetail.setDetail(ex.getMessage()); 74 problemDetail.setProperty("timestamp", Instant.now()); 75 return problemDetail; 76 } 77 78 @ExceptionHandler(ShelterIsNotActiveException.class) 79 ProblemDetail handleShelterIsNotActive(ShelterIsNotActiveException ex) { 80 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_ACCEPTABLE); 81 problemDetail.setTitle("Shelter is not active yet."); 82 problemDetail.setDetail(ex.getMessage()); 83 problemDetail.setProperty("timestamp", Instant.now()); 84 return problemDetail; 85 } 86 87 @ExceptionHandler(PetIsArchivedException.class) 88 ProblemDetail handlePetIsArchived(PetIsArchivedException ex) { 89 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.CONFLICT); 90 problemDetail.setTitle("Pet is archived and not available for action."); 91 problemDetail.setDetail(ex.getMessage()); 92 problemDetail.setProperty("timestamp", Instant.now()); 93 return problemDetail; 94 } 95 96 @ExceptionHandler(PetMaxImagesReachedException.class) 97 ProblemDetail handlePetMaxImagesReached(PetMaxImagesReachedException ex) { 98 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST); 99 problemDetail.setTitle("Pet max images reached"); 100 problemDetail.setDetail(ex.getMessage()); 101 problemDetail.setProperty("timestamp", Instant.now()); 102 return problemDetail; 103 } 104 105 @ExceptionHandler(PetNotFoundException.class) 106 ProblemDetail handlePetNotFound(PetNotFoundException ex) { 107 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND); 108 problemDetail.setTitle("Pet not found"); 109 problemDetail.setDetail(ex.getMessage()); 110 problemDetail.setProperty("timestamp", Instant.now()); 111 return problemDetail; 112 } 113 114 @ExceptionHandler(AdoptionAlreadyExistsException.class) 115 ProblemDetail handleAdoptionAlreadyExists(AdoptionAlreadyExistsException ex) { 116 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.CONFLICT); 117 problemDetail.setTitle("Adoption Already Exists"); 118 problemDetail.setDetail(ex.getMessage()); 119 problemDetail.setProperty("timestamp", Instant.now()); 120 return problemDetail; 121 } 122 123 @ExceptionHandler(AdoptionFormNotFoundException.class) 124 ProblemDetail handleAdoptionFormNotFound(AdoptionFormNotFoundException ex) { 125 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST); 126 problemDetail.setTitle("Adoption Form Not Found"); 127 problemDetail.setDetail(ex.getMessage()); 128 problemDetail.setProperty("timestamp", Instant.now()); 129 return problemDetail; 130 } 131 132 @ExceptionHandler(PetImageNotFoundException.class) 133 ProblemDetail handlePetImageNotFound(PetImageNotFoundException ex) { 134 ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND); 135 problemDetail.setTitle("Pet Image Not Found"); 136 problemDetail.setDetail(ex.getMessage()); 137 problemDetail.setProperty("timestamp", Instant.now()); 138 return problemDetail; 139 } 140 }