Search in sources :

Example 11 with ErrorModel

use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handle.

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ResultModels> handle(AccessDeniedException ex) {
    ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.FORBIDDEN, ex.getMessage());
    LOG.warn("[" + errorModel.getId() + "] ", ex);
    return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 12 with ErrorModel

use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handle.

@ExceptionHandler(IdmAuthenticationException.class)
public ResponseEntity<ResultModels> handle(IdmAuthenticationException ex) {
    ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.AUTH_FAILED);
    // source exception message is shown only in log
    LOG.warn("[" + errorModel.getId() + "] ", ex);
    return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 13 with ErrorModel

use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handle.

@ExceptionHandler(PersistenceException.class)
public ResponseEntity<ResultModels> handle(PersistenceException ex) {
    ErrorModel errorModel = null;
    // 
    if (ex.getCause() != null && ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException constraintEx = (ConstraintViolationException) ex.getCause();
        // TODO: registrable contstrain error codes
        if (constraintEx.getConstraintName().contains("name")) {
            errorModel = new DefaultErrorModel(CoreResultCode.NAME_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        } else if (constraintEx.getConstraintName().contains("code")) {
            errorModel = new DefaultErrorModel(CoreResultCode.CODE_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        } else {
            errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        }
    } else {
        errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ex.getMessage());
    }
    LOG.error("[" + errorModel.getId() + "] ", ex);
    return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 14 with ErrorModel

use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handleError.

/**
 * Handle exceptions from http (~ authentication) filters.
 *
 * @see RestErrorAttributes
 * @param request
 * @param response
 * @return error models
 * @since 10.7.0
 */
@RequestMapping(path = { "/error" })
public ResponseEntity<?> handleError(HttpServletRequest request, HttpServletResponse response) {
    Object exception = request.getAttribute(javax.servlet.RequestDispatcher.ERROR_EXCEPTION);
    // 
    if (exception == null || !(exception instanceof Throwable)) {
        // if source exception is not set (e.g. from controller security ~ forbidden), return base error attributes.
        RestErrorAttributes attributes = new RestErrorAttributes();
        attributes.resolveException(request, response, null, null);
        Map<String, Object> errorAttributes = attributes.getErrorAttributes(new ServletWebRequest(request, response), false);
        // 
        // fill http status
        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
        if (errorAttributes.containsKey(RestErrorAttributes.ATTRIBUTE_STATUS)) {
            int statusCode = (int) errorAttributes.get(RestErrorAttributes.ATTRIBUTE_STATUS);
            httpStatus = HttpStatus.valueOf(statusCode);
        }
        // move error as IdM errors
        if (errorAttributes.containsKey(RestErrorAttributes.ATTRIBUTE_ERROR)) {
            errorAttributes.put(ResultModels.ATTRIBUTE_ERRORS, Lists.newArrayList(errorAttributes.get(RestErrorAttributes.ATTRIBUTE_ERROR)));
            errorAttributes.remove(RestErrorAttributes.ATTRIBUTE_ERROR);
        }
        // Return original error attributes => to cover all cases, when even "unknown" exception can be thrown (without our error).
        return new ResponseEntity<>(errorAttributes, createHttpHeaders(request, response), httpStatus);
    }
    // 
    Throwable cause = Throwables.getRootCause((Throwable) exception);
    HttpHeaders httpHeaders = createHttpHeaders(request, response);
    // 
    if (cause instanceof ResultCodeException) {
        ResultCodeException resultCodeException = (ResultCodeException) cause;
        ExceptionUtils.log(LOG, resultCodeException);
        // 
        return new ResponseEntity<>(resultCodeException.getError(), httpHeaders, resultCodeException.getStatus());
    }
    // 
    ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.INTERNAL_SERVER_ERROR, cause.getMessage());
    ExceptionUtils.log(LOG, errorModel, cause);
    // 
    return new ResponseEntity<>(new ResultModels(errorModel), httpHeaders, errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with ErrorModel

use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handle.

/**
 * Upload file size exceeded.
 *
 * @param ex handled  exception
 * @return error model
 * @since 10.2.0
 */
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<ResultModels> handle(MaxUploadSizeExceededException ex) {
    long maxUploadSize = ex.getMaxUploadSize();
    // 
    String effectiveMaxUploadSize;
    if (maxUploadSize <= 0) {
        effectiveMaxUploadSize = configurationService.getValue("spring.servlet.multipart.max-file-size", // -1 as default
        String.valueOf(maxUploadSize));
    } else {
        effectiveMaxUploadSize = FileUtils.byteCountToDisplaySize(maxUploadSize);
    }
    // 
    ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.ATTACHMENT_SIZE_LIMIT_EXCEEDED, ex.getMessage(), ImmutableMap.of("actualSize", effectiveMaxUploadSize));
    LOG.warn("[" + errorModel.getId() + "] ", ex);
    return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)17 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)14 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)11 HttpHeaders (org.springframework.http.HttpHeaders)11 ResponseEntity (org.springframework.http.ResponseEntity)11 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)6 SysSchemaAttributeDto (eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto)2 SysSchemaObjectClassDto (eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto)2 SysSystemAttributeMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto)2 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)2 SysSystemMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 Test (org.junit.Test)2 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 IcAttribute (eu.bcvsolutions.idm.ic.api.IcAttribute)1 Method (java.lang.reflect.Method)1 AsyncUncaughtExceptionHandler (org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler)1