Skip to content

Commit

Permalink
feat: change response error format
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-miazina committed May 2, 2024
1 parent 92a893a commit 99a874d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,60 +1,69 @@
package mate.academy.bookstore.handler;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.List;
import mate.academy.bookstore.exception.EntityNotFoundException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.mapping.PropertyReferenceException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class BookControllerExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
@ResponseBody
public ResponseEntity<Object> handleEntityNotFoundException(EntityNotFoundException ex) {
return getResponseEntity(HttpStatus.NOT_FOUND, ex.getMessage());
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseData handleEntityNotFoundException(EntityNotFoundException ex) {
return new ResponseData(
HttpStatus.NOT_FOUND,
null,
List.of(new ErrorMessage("", ex.getMessage()))
);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseEntity<Object> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach(error -> {
Map.Entry<String, String> errorMessage = getErrorMessage(error);
errors.put(errorMessage.getKey(), errorMessage.getValue());
});
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseData handleValidationExceptions(MethodArgumentNotValidException ex) {
List<ErrorMessage> errors = ex.getBindingResult().getAllErrors().stream()
.map(this::getErrorMessage)
.toList();
return new ResponseData(HttpStatus.BAD_REQUEST, null, errors);
}

return getResponseEntity(HttpStatus.BAD_REQUEST, errors);
@ExceptionHandler(PropertyReferenceException.class)
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseData handlePropertyReferenceException(PropertyReferenceException ex) {
return new ResponseData(
HttpStatus.BAD_REQUEST,
null,
List.of(new ErrorMessage("", ex.getMessage()))
);
}

@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseBody
public ResponseEntity<Object> handleDataIntegrityViolationException(
@ResponseStatus(HttpStatus.CONFLICT)
public ResponseData handleDataIntegrityViolationException(
DataIntegrityViolationException ex) {
return getResponseEntity(HttpStatus.CONFLICT, ex.getMessage());
return new ResponseData(
HttpStatus.CONFLICT,
null,
List.of(new ErrorMessage("", ex.getMessage()))
);
}

private Map.Entry<String, String> getErrorMessage(ObjectError e) {
private ErrorMessage getErrorMessage(ObjectError e) {
String field = "";
if (e instanceof FieldError fieldError) {
String field = fieldError.getField();
String message = e.getDefaultMessage();
return Map.entry(field, message);
field = fieldError.getField();
}
return Map.entry("", e.getDefaultMessage());
}

private ResponseEntity<Object> getResponseEntity(HttpStatus status, Object errors) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("success", status.is2xxSuccessful());
body.put("errors", errors);
return new ResponseEntity<>(body, status);
return new ErrorMessage(field, e.getDefaultMessage());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package mate.academy.bookstore.handler;

public record ErrorMessage(String field, String message) {
}
22 changes: 22 additions & 0 deletions src/main/java/mate/academy/bookstore/handler/ResponseData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mate.academy.bookstore.handler;

import java.util.List;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class ResponseData {
private boolean success;

private int status;
private Object data;
private List<ErrorMessage> errors;

public ResponseData(HttpStatus status, Object data, List<ErrorMessage> errors) {
this.data = data;
this.errors = errors;
this.status = status.value();
this.success = status.is2xxSuccessful();
}
}

0 comments on commit 99a874d

Please sign in to comment.