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;
}
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);
}
}
};
}
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();
}
}
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();
}
}
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());
}
Aggregations