Skip to content

Commit

Permalink
Merge pull request #115 from perplexhub/feature/throw_conversion_exce…
Browse files Browse the repository at this point in the history
…ption

Throw conversion exception #107
  • Loading branch information
perplexhub authored Aug 10, 2023
2 parents b8be22a + 8eead01 commit fd920d4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.perplexhub.rsql;

public class ConversionException extends RSQLException {
public ConversionException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ protected Object convert(String source, Class targetType) {
}

return object;
} catch (DateTimeParseException | IllegalArgumentException e) {
log.debug("Parsing [{}] with [{}] causing [{}], skip", source, targetType.getName(), e.getMessage());
} catch (Exception e) {
log.error("Parsing [{}] with [{}] causing [{}], add your parser via RSQLSupport.addConverter(Type.class, Type::valueOf)", source, targetType.getName(), e.getMessage(), e);
throw new ConversionException(String.format("Failed to convert %s to %s type", source, targetType.getName()), e);
}
return null;
}

protected void accessControl(Class type, String name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.perplexhub.rsql;

import io.github.perplexhub.rsql.model.User;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.convert.support.ConfigurableConversionService;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class RSQLVisitorBaseTest {

@Spy
RSQLVisitorBase unit;
@Mock
ConfigurableConversionService defaultConversionService;

@BeforeEach
void init() {
RSQLVisitorBase.setDefaultConversionService(defaultConversionService);
}

@Test
void testConversionException() {
assertThatExceptionOfType(ConversionException.class)
.isThrownBy(() -> unit.convert("abc", Integer.class))
.satisfies(e -> assertEquals("Failed to convert abc to java.lang.Integer type", e.getMessage()));

}
}

0 comments on commit fd920d4

Please sign in to comment.