Skip to content
Snippets Groups Projects
Commit b191755c authored by Jan Pokorný's avatar Jan Pokorný :lifter_tone2:
Browse files

Adding extended error handling

parent fb23854f
No related branches found
No related tags found
1 merge request!35M2 fix language school
package org.fuseri.modulelanguageschool.exceptions;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.ToString;
import org.springframework.http.HttpStatus;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.List;
@Getter
@ToString
class ApiError {
private HttpStatus status;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
private LocalDateTime timestamp;
private String message;
private List<ApiSubError> subErrors;
private String path;
private ApiError() {
timestamp = LocalDateTime.now(Clock.systemUTC());
}
ApiError(HttpStatus status, Throwable ex, String path) {
this();
this.status = status;
this.path = path;
this.message = ex.getLocalizedMessage();
}
ApiError(HttpStatus status, List<ApiSubError> subErrors, Throwable ex, String path) {
this();
this.status = status;
this.subErrors = subErrors;
this.path = path;
this.message = ex.getLocalizedMessage();
}
}
package org.fuseri.modulelanguageschool.exceptions;
public interface ApiSubError {
}
package org.fuseri.modulelanguageschool.exceptions;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@Data
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@Getter
@ToString
class ApiValidationError implements ApiSubError {
private String object;
private String field;
private Object rejectedValue;
private String message;
}
package org.fuseri.modulelanguageschool.exceptions;
import jakarta.persistence.EntityNotFoundException;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.util.UrlPathHelper;
import java.util.List;
@ControllerAdvice
public class RestResponseEntityExceptionHandler {
private static final UrlPathHelper URL_PATH_HELPER = new UrlPathHelper();
/**
* Handle ResourceNotFoundException exceptions
*
* @param ex exception
* @param request request
* @return response entity
*/
@ExceptionHandler(value = {EntityNotFoundException.class})
public ResponseEntity<ApiError> handleNotFoundError(EntityNotFoundException ex, HttpServletRequest request) {
ApiError error = new ApiError(
HttpStatus.NOT_FOUND,
ex,
URL_PATH_HELPER.getRequestUri(request));
return buildResponseEntity(error);
}
/**
* Handle Validation exceptions
*
* @param ex exception
* @param request request
* @return response entity
*/
@ExceptionHandler(value = {MethodArgumentNotValidException.class, HttpMessageNotReadableException.class})
public ResponseEntity<ApiError> handleValidationErrors(MethodArgumentNotValidException ex, HttpServletRequest request) {
List<ApiSubError> subErrors = ex.getBindingResult().getFieldErrors()
.stream()
.map(e -> (ApiSubError) new ApiValidationError(e.getObjectName(), e.getField(), e.getRejectedValue(), e.getDefaultMessage()))
.toList();
ApiError error = new ApiError(
HttpStatus.BAD_REQUEST,
subErrors,
ex,
URL_PATH_HELPER.getRequestUri(request));
return buildResponseEntity(error);
}
/**
* Handle exceptions not matched by above handler methods
*
* @param ex exception
* @param request request
* @return response entity
*/
@ExceptionHandler({Exception.class})
public ResponseEntity<ApiError> handleAll(final Exception ex, HttpServletRequest request) {
final ApiError error = new ApiError(
HttpStatus.INTERNAL_SERVER_ERROR,
ExceptionUtils.getRootCause(ex),
URL_PATH_HELPER.getRequestUri(request));
return buildResponseEntity(error);
}
private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
return new ResponseEntity<>(apiError, apiError.getStatus());
}
}
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