All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.convergence_platform.services.controllers.ExceptionHandlerController Maven / Gradle / Ivy

Go to download

Holds the common functionality needed by all Convergence Platform-based services written in Java.

The newest version!
//package io.convergence_platform.services.controllers;
//
//import io.convergence_platform.common.dto.FailureInfoDTO;
//import io.convergence_platform.common.exceptions.ManagedApiException;
//import io.convergence_platform.common.responses.ApiResponse;
//import io.convergence_platform.common.responses.Errors;
//import io.convergence_platform.common.responses.HttpErrors;
//import org.springframework.beans.ConversionNotSupportedException;
//import org.springframework.beans.TypeMismatchException;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatusCode;
//import org.springframework.http.ResponseEntity;
//import org.springframework.http.converter.HttpMessageNotReadableException;
//import org.springframework.http.converter.HttpMessageNotWritableException;
//import org.springframework.security.access.AccessDeniedException;
//import org.springframework.validation.BindException;
//import org.springframework.validation.FieldError;
//import org.springframework.validation.ObjectError;
//import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
//import org.springframework.web.ErrorResponseException;
//import org.springframework.web.HttpMediaTypeNotAcceptableException;
//import org.springframework.web.HttpMediaTypeNotSupportedException;
//import org.springframework.web.bind.MethodArgumentNotValidException;
//import org.springframework.web.bind.MissingPathVariableException;
//import org.springframework.web.bind.MissingServletRequestParameterException;
//import org.springframework.web.bind.ServletRequestBindingException;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.context.request.WebRequest;
//import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
//import org.springframework.web.multipart.support.MissingServletRequestPartException;
//import org.springframework.web.servlet.NoHandlerFoundException;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
//
//import java.util.Map;
//
//@ControllerAdvice
//public class ExceptionHandlerController extends ResponseEntityExceptionHandler {
//    @ExceptionHandler({
//            AccessDeniedException.class,
//            ManagedApiException.class,
//    })
//    protected ResponseEntity handleExceptionInternal(Exception ex, HttpStatusCode statusCode) {
//        FailureInfoDTO failure = new FailureInfoDTO();
//        if (ex instanceof ManagedApiException subEx) {
//            failure.setHttpStatusCode(subEx.getHttpStatusCode());
//            failure.setCode(subEx.getCode());
//            failure.setMessage(subEx.getMessage());
//        } else if (ex instanceof MethodArgumentNotValidException subEx) {
//            failure.setHttpStatusCode(HttpErrors.BAD_REQUEST);
//            failure.setCode(Errors.INVALID_DATA);
//            failure.setMessage(getArgumentValidationFriendlyMessage(subEx));
//        } else {
//            failure.setHttpStatusCode(statusCode.value());
//            failure.setCode("err_" + statusCode.toString().toLowerCase());
//            failure.setMessage(ex.getMessage());
//        }
//
//        return ResponseEntity.status(failure.getHttpStatusCode())
//                .body(ApiResponse.from(failure));
//
//    }
//
//    private String getArgumentValidationFriendlyMessage(MethodArgumentNotValidException subEx) {
//        StringBuilder result = new StringBuilder();
//        result.append("Validation of the request failed. The invalid field(s) are:");
//
//        for (ObjectError error : subEx.getBindingResult().getAllErrors()) {
//            if (error instanceof FieldError fieldError) {
//                result.append("\n    * ")
//                        .append(fieldError.getField())
//                        .append(" (").append(error.getDefaultMessage()).append(")");
//            } else {
//                result.append("\n    * ")
//                        .append("Unknown error")
//                        .append(" (").append(error.getDefaultMessage()).append(")");
//            }
//        }
//
//        return result.toString();
//    }
//
//
//
//
//
//
//    //    @ExceptionHandler(Exception.class)
////    public ResponseEntity> handleException(Exception ex) {
////        return wrap(createInternalErrorResponse(Errors.UNHANDLED_EXCEPTION, ex));
////    }
////
////    @ExceptionHandler(MethodArgumentNotValidException.class)
////    public ResponseEntity handleInvalidInputException(Exception ex) {
////        int statusCode = HttpStatus.BAD_REQUEST.value();
////
////        FailureInfoDTO info = new FailureInfoDTO();
////        info.code = Errors.INVALID_DATA;
////        info.httpStatusCode = statusCode;
////        info.message = ex.getMessage();
////
////        ApiResponse response = ApiResponse.from(info);
////        return ResponseEntity.status(statusCode).body(response);
////    }
////
////    @ExceptionHandler(AccessDeniedException.class)
////    public ResponseEntity handleAccessDeniedException(AccessDeniedException ex) {
////        int statusCode = HttpStatus.FORBIDDEN.value();
////
////        FailureInfoDTO info = new FailureInfoDTO();
////        info.code = Errors.ACCESS_DENIED;
////        info.httpStatusCode = statusCode;
////        info.message = "You don't have access to perform this operation. Please contact your administrator.";
////
////        ApiResponse response = ApiResponse.from(info);
////        return ResponseEntity.status(statusCode).body(response);
////    }
//}