Search in sources :

Example 1 with ErrorModel

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

the class RestErrorAttributes method getErrorAttributes.

@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
    Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
    ErrorModel errorModel = null;
    if (errorAttributes.containsKey("status")) {
        int status = (int) errorAttributes.get("status");
        switch(status) {
            case 401:
                {
                    errorModel = new DefaultErrorModel(CoreResultCode.LOG_IN, ImmutableMap.of("path", errorAttributes.get("path")));
                    break;
                }
            case 403:
                {
                    errorModel = new DefaultErrorModel(CoreResultCode.FORBIDDEN, ImmutableMap.of("path", errorAttributes.get("path"), "message", errorAttributes.get("message")));
                    break;
                }
            case 404:
                {
                    errorModel = new DefaultErrorModel(CoreResultCode.ENDPOINT_NOT_FOUND, ImmutableMap.of("path", errorAttributes.get("path"), "message", errorAttributes.get("message")));
                    break;
                }
            case 400:
            case 405:
                {
                    errorModel = new DefaultErrorModel(CoreResultCode.METHOD_NOT_ALLOWED, ImmutableMap.of("path", errorAttributes.get("path"), "message", errorAttributes.get("message")));
                    break;
                }
            default:
                {
                    errorModel = null;
                }
        }
    }
    if (errorModel == null) {
        log.error("Error not resolved - errorAttributes needs extension for error attrs [{}]", errorAttributes);
        return errorAttributes;
    }
    // we need timestamp etc.
    // errorAttributes.clear();
    errorAttributes.put("error", errorModel);
    log.warn(errorModel.toString());
    return errorAttributes;
}
Also used : DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)

Example 2 with ErrorModel

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

the class AsyncConfig method getAsyncUncaughtExceptionHandler.

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    // simple error logging
    return new AsyncUncaughtExceptionHandler() {

        @Override
        public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
            if (throwable instanceof ResultCodeException) {
                ResultCodeException ex = (ResultCodeException) throwable;
                LOG.error("[" + ex.getId() + "] ", ex);
            } else {
                ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.INTERNAL_SERVER_ERROR, throwable.getMessage());
                LOG.error("[" + errorModel.getId() + "] ", throwable);
            }
        }
    };
}
Also used : AsyncUncaughtExceptionHandler(org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) Method(java.lang.reflect.Method) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)

Example 3 with ErrorModel

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

the class DefaultSysSystemAttributeMappingServiceTest method transformationFromScriptFailure.

@Test
public void transformationFromScriptFailure() {
    SysSystemDto system = createSystem();
    SysSchemaObjectClassDto objClass = createObjectClass(system);
    SysSchemaAttributeDto schemaAttr = createSchemaAttribute(objClass);
    SysSystemMappingDto systemMapping = testHelper.createMappingSystem(SystemEntityType.IDENTITY, objClass);
    SysSystemAttributeMappingDto attrMapping = createAttributeMappingSystem(systemMapping, AttributeMappingStrategyType.CREATE, schemaAttr.getId());
    // script consists of just one missing symbol,
    // which is supposed to be part of error message
    String script = "xxxxx";
    attrMapping.setTransformFromResourceScript(script);
    systemMapping = mappingService.save(systemMapping);
    try {
        attributeMappingService.transformValueFromResource("testValue", attrMapping, new ArrayList<IcAttribute>());
        fail();
    } catch (ResultCodeException ex) {
        ErrorModel errModel = ex.getError().getError();
        String message = (String) errModel.getParameters().get(SysSystemAttributeMappingService.MAPPING_SCRIPT_FAIL_MESSAGE_KEY);
        String idmPath = (String) errModel.getParameters().get(SysSystemAttributeMappingService.MAPPING_SCRIPT_FAIL_IDM_PATH_KEY);
        assertEquals(errModel.getStatusEnum(), AccResultCode.GROOVY_SCRIPT_ATTR_TRANSFORMATION_FAILED.getCode());
        assertTrue(message.contains(script));
        assertTrue(idmPath.contains(system.getCode()));
        assertTrue(idmPath.contains(systemMapping.getName()));
        assertTrue(idmPath.contains(attrMapping.getName()));
    } catch (Exception e) {
        fail();
    }
}
Also used : IcAttribute(eu.bcvsolutions.idm.ic.api.IcAttribute) SysSchemaAttributeDto(eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto) SysSystemAttributeMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) SysSystemMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) SysSchemaObjectClassDto(eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with ErrorModel

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

the class DefaultSysSystemAttributeMappingServiceTest method transformationToScriptFailure.

@Test
public void transformationToScriptFailure() {
    SysSystemDto system = createSystem();
    SysSchemaObjectClassDto objClass = createObjectClass(system);
    SysSchemaAttributeDto schemaAttr = createSchemaAttribute(objClass);
    SysSystemMappingDto systemMapping = testHelper.createMappingSystem(SystemEntityType.IDENTITY, objClass);
    SysSystemAttributeMappingDto attrMapping = createAttributeMappingSystem(systemMapping, AttributeMappingStrategyType.CREATE, schemaAttr.getId());
    // script consists of just one missing symbol,
    // which is supposed to be part of error message
    String script = "xxxxx";
    attrMapping.setTransformToResourceScript(script);
    systemMapping = mappingService.save(systemMapping);
    try {
        attributeMappingService.transformValueToResource(null, "testValue", attrMapping, new IdmIdentityDto());
        fail();
    } catch (ResultCodeException ex) {
        ErrorModel errModel = ex.getError().getError();
        String message = (String) errModel.getParameters().get(SysSystemAttributeMappingService.MAPPING_SCRIPT_FAIL_MESSAGE_KEY);
        String idmPath = (String) errModel.getParameters().get(SysSystemAttributeMappingService.MAPPING_SCRIPT_FAIL_IDM_PATH_KEY);
        assertEquals(errModel.getStatusEnum(), AccResultCode.GROOVY_SCRIPT_ATTR_TRANSFORMATION_FAILED.getCode());
        assertTrue(message.contains(script));
        assertTrue(idmPath.contains(system.getCode()));
        assertTrue(idmPath.contains(systemMapping.getName()));
        assertTrue(idmPath.contains(attrMapping.getName()));
    } catch (Exception e) {
        fail();
    }
}
Also used : SysSchemaAttributeDto(eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto) SysSystemAttributeMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) SysSystemMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) SysSchemaObjectClassDto(eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 5 with ErrorModel

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

the class ExceptionControllerAdvice method handle.

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<ResultModels> handle(DataIntegrityViolationException ex) {
    ErrorModel errorModel = null;
    // 
    if (ex.getCause() != null && ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException constraintEx = (ConstraintViolationException) ex.getCause();
        // TODO: registrable constraint error codes
        if (constraintEx.getConstraintName() != null && constraintEx.getConstraintName().contains("name")) {
            errorModel = new DefaultErrorModel(CoreResultCode.NAME_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        } else if (constraintEx.getConstraintName() != null && constraintEx.getConstraintName().contains("code")) {
            errorModel = new DefaultErrorModel(CoreResultCode.CODE_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        } else if (constraintEx.getConstraintName() == null) {
            errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ImmutableMap.of("name", "..."));
        } else {
            errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ImmutableMap.of("name", StringUtils.trimToEmpty(constraintEx.getConstraintName())));
        }
    } else {
        errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ex.getMostSpecificCause().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)

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