Skip to content
Snippets Groups Projects
Commit 230f9afc authored by Diana Gulčíková's avatar Diana Gulčíková
Browse files

checkstyle fix

parent db1c4a6a
No related branches found
No related tags found
2 merge requests!60Docker,!30Persistent layer for Race module
......@@ -4,6 +4,9 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import lombok.Getter;
/**
* Response when exception is thrown in API.
*/
public class ExError {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
......
......@@ -13,17 +13,31 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
/**
* Exception handler for all controllers.
*/
@RestControllerAdvice
public class RestExceptionHandler {
/**
* Handlers for 404 status code.
*
* @param ex thrown exception.
* @return Message with datetime.
*/
@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ExError> handleEntityNotFoundException(EntityNotFoundException ex) {
System.out.println();
return new ResponseEntity<>(
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.NOT_FOUND);
}
/**
* Handlers for 404 when resource is not found in the database.
*
* @param ex thrown exception.
* @return Message with datetime.
*/
@ExceptionHandler(DatabaseException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ExError> handleDatabaseException(DatabaseException ex) {
......@@ -31,13 +45,25 @@ public class RestExceptionHandler {
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.NOT_FOUND);
}
/**
* Handlers for 404 when there is no endpoint mapped to the requested path.
*
* @param ex thrown exception.
* @return Message with datetime.
*/
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ExError> handleDatabaseException(NoHandlerFoundException ex) {
public ResponseEntity<ExError> handleNoHandlerFoundException(NoHandlerFoundException ex) {
return new ResponseEntity<>(
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.NOT_FOUND);
}
/**
* Handler for 400 status code when json is not valid.
*
* @param ex thrown exception.
* @return Message with datetime.
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ExError> handleNotReadableException(HttpMessageNotReadableException ex) {
......@@ -45,26 +71,33 @@ public class RestExceptionHandler {
new ExError(ex.getMessage()), new HttpHeaders(), HttpStatus.BAD_REQUEST);
}
/**
* Handler for 400 status code when there are validation exceptions.
*
* @param ex thrown exception.
* @return Message with datetime.
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ExError> handleNotReadableException(MethodArgumentNotValidException ex) {
public ResponseEntity<ExError> handleValidationException(MethodArgumentNotValidException ex) {
String message = ex.getMessage();
try {
StringBuilder validtionMessage = new StringBuilder("Validation Errors: [");
var validationErrors = ex.getBindingResult().getAllErrors();
for (ObjectError error: validationErrors) {
for (ObjectError error : validationErrors) {
var errorField = (FieldError) error;
var errorMessage = errorField.getDefaultMessage();
var fieldName = errorField.getField();
validtionMessage.append(" field ").append(fieldName).append(": ").append(errorMessage).append(",");
validtionMessage.append(" field ").append(fieldName).append(": ")
.append(errorMessage).append(",");
}
validtionMessage.deleteCharAt(validtionMessage.length() - 1);
validtionMessage.append(" ]");
message = validtionMessage.toString();
} catch (Exception ignored) {
} catch (Exception e) {
System.out.println("When creating validation error message: " + e.getMessage());
}
return new ResponseEntity<>(
new ExError(message), new HttpHeaders(), HttpStatus.BAD_REQUEST);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment