Search in sources :

Example 11 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class ArticleServiceImplTest method testCreateArticleContent_testDataTruncationError.

@Test
public void testCreateArticleContent_testDataTruncationError() throws Exception {
    Article a = buildTestDto();
    a.setArticleKey(generateLongString(Article.FN_KEY_SIZE, "Z"));
    a.setLang("ZZ");
    a.setTitle(generateLongString(Article.FN_TITLE_SIZE, "Ф"));
    a.setAnnotation(generateLongString(Article.FN_ANNOTATION_SIZE, "Ф"));
    a.setContent(generateLongString(Article.FN_ANNOTATION_SIZE, "Ф"));
    a.setArticleGroup(generateLongString(Article.FN_GROUP_SIZE, "Z"));
    articleService.create(a);
    // Update data with wrong length text
    try {
        a = buildTestDto();
        a.setArticleKey(generateLongString(Article.FN_KEY_SIZE + 1, "ф"));
        a.setLang("ZZZ");
        a.setTitle(generateLongString(Article.FN_TITLE_SIZE + 1, "Ф"));
        a.setAnnotation(generateLongString(Article.FN_ANNOTATION_SIZE + 1, "ф"));
        a.setContent(generateLongString(Article.FN_ANNOTATION_SIZE + 1, "ф"));
        a.setArticleGroup(generateLongString(Article.FN_GROUP_SIZE + 1, "Z"));
        articleService.create(a);
        fail("FVE expected");
    } catch (FieldValidationException fve) {
        assertEquals(5, fve.getErrors().size());
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_KEY));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_LANG));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_TITLE));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_ANNOTATION));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_GROUP));
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) Article(org.summerb.microservices.articles.api.dto.Article) Test(org.junit.Test)

Example 12 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class EasyCrudValidationStrategyAbstract method validateForUpdate.

@Override
public void validateForUpdate(TDto existingVersion, TDto newVersion) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    doValidateForUpdate(existingVersion, newVersion, ctx);
    if (ctx.getHasErrors()) {
        throw new FieldValidationException(ctx.getErrors());
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 13 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class EasyCrudValidationStrategyAbstract method validateForCreate.

@Override
public void validateForCreate(TDto dto) throws FieldValidationException {
    ValidationContext ctx = new ValidationContext();
    doValidateForCreate(dto, ctx);
    if (ctx.getHasErrors()) {
        throw new FieldValidationException(ctx.getErrors());
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) ValidationContext(org.summerb.approaches.validation.ValidationContext)

Example 14 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class EasyCrudDaoMySqlImpl method processDuplicateEntryException.

private void processDuplicateEntryException(Throwable t) throws FieldValidationException {
    DuplicateKeyException dke = ExceptionUtils.findExceptionOfType(t, DuplicateKeyException.class);
    if (dke == null) {
        return;
    }
    String constraint = DaoExceptionUtils.findViolatedConstraintName(dke);
    // Handle case when uuid is duplicated
    if (DaoExceptionUtils.MYSQL_CONSTRAINT_PRIMARY.equals(constraint)) {
        throw new IllegalArgumentException("Row with same primary key already exists", dke);
    }
    if (!constraint.contains(DaoExceptionUtils.MYSQL_CONSTRAINT_UNIQUE)) {
        throw new IllegalArgumentException("Constraint violation " + constraint, dke);
    }
    String fieldName = constraint.substring(0, constraint.indexOf(DaoExceptionUtils.MYSQL_CONSTRAINT_UNIQUE));
    if (fieldName.contains("_")) {
        fieldName = JdbcUtils.convertUnderscoreNameToPropertyName(fieldName);
    }
    throw new FieldValidationException(new DuplicateRecordValidationError(fieldName));
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DuplicateRecordValidationError(org.summerb.approaches.validation.errors.DuplicateRecordValidationError)

Example 15 with FieldValidationException

use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.

the class DaoExceptionUtils method propagateIfTruncationError.

/**
 * @deprecated This feature will not actually work at least with mysql.
 *             MySql doesn't send the numbers - it sends only field name
 *             which was truncated.
 */
@Deprecated
public static void propagateIfTruncationError(Throwable t) throws FieldValidationException {
    DataTruncation exc = ExceptionUtils.findExceptionOfType(t, DataTruncation.class);
    if (exc == null) {
        return;
    }
    String fieldName = findTruncatedFieldNameIfAny(t);
    if (fieldName == null) {
        return;
    }
    DataTooLongValidationError dataTooLongValidationError = new DataTooLongValidationError(exc.getDataSize(), exc.getTransferSize(), fieldName);
    throw new FieldValidationException(dataTooLongValidationError);
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) DataTooLongValidationError(org.summerb.approaches.validation.errors.DataTooLongValidationError) DataTruncation(java.sql.DataTruncation)

Aggregations

FieldValidationException (org.summerb.approaches.validation.FieldValidationException)29 User (org.summerb.microservices.users.api.dto.User)11 UserServiceUnexpectedException (org.summerb.microservices.users.api.exceptions.UserServiceUnexpectedException)11 Transactional (org.springframework.transaction.annotation.Transactional)9 UserNotFoundException (org.summerb.microservices.users.api.exceptions.UserNotFoundException)8 ValidationContext (org.summerb.approaches.validation.ValidationContext)5 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ValidationErrorsVm (org.summerb.approaches.springmvc.model.ValidationErrorsVm)3 ValidationError (org.summerb.approaches.validation.ValidationError)3 AuthToken (org.summerb.microservices.users.api.dto.AuthToken)3 InvalidPasswordException (org.summerb.microservices.users.api.exceptions.InvalidPasswordException)3 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 NotAuthorizedResult (org.summerb.approaches.security.api.dto.NotAuthorizedResult)2 NotAuthorizedException (org.summerb.approaches.security.api.exceptions.NotAuthorizedException)2 UserDetailsImpl (org.summerb.approaches.springmvc.security.dto.UserDetailsImpl)2 UserStatus (org.summerb.approaches.springmvc.security.dto.UserStatus)2 DuplicateUserValidationError (org.summerb.microservices.users.api.validation.DuplicateUserValidationError)2 GenericException (org.summerb.utils.exceptions.GenericException)2 ExceptionInfo (org.summerb.utils.exceptions.dto.ExceptionInfo)2