Search in sources :

Example 16 with DefaultErrorModel

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

the class ExceptionUtilsUnitTest method testLogModelLevelDebug.

@Test
public void testLogModelLevelDebug() {
    ExceptionUtils.log(LOG, new DefaultErrorModel(CoreResultCode.ACCEPTED), new CoreException("mock"));
    // error is logged without model is specified
    verify(LOG).debug(any(String.class), any(Exception.class));
}
Also used : CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Example 17 with DefaultErrorModel

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

the class ExceptionUtilsUnitTest method testLogModelWithoutException.

@Test
public void testLogModelWithoutException() {
    ExceptionUtils.log(LOG, new DefaultErrorModel(CoreResultCode.INTERNAL_SERVER_ERROR), null);
    // error is logged without model is specified
    verify(LOG).error(any(String.class), (Throwable) ArgumentMatchers.isNull());
}
Also used : DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Example 18 with DefaultErrorModel

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

the class ExceptionUtilsUnitTest method testLogExceptionLevelWarn.

@Test
public void testLogExceptionLevelWarn() {
    ExceptionUtils.log(LOG, new ResultCodeException(new DefaultErrorModel(CoreResultCode.FORBIDDEN)));
    // error is logged without model is specified
    verify(LOG).warn(any(String.class), any(Exception.class));
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AbstractVerifiableUnitTest(eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest) Test(org.junit.Test)

Example 19 with DefaultErrorModel

use of eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel 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 20 with DefaultErrorModel

use of eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel 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)

Aggregations

DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)24 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)14 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)14 HttpHeaders (org.springframework.http.HttpHeaders)11 ResponseEntity (org.springframework.http.ResponseEntity)11 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)10 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)10 AbstractVerifiableUnitTest (eu.bcvsolutions.idm.test.api.AbstractVerifiableUnitTest)7 Test (org.junit.Test)7 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)6 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)3 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)1 Recoverable (eu.bcvsolutions.idm.core.api.service.Recoverable)1 Method (java.lang.reflect.Method)1 AsyncUncaughtExceptionHandler (org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler)1 HttpStatus (org.springframework.http.HttpStatus)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1