接口 ExceptionTranslator<E extends Exception>
- 类型参数:
E- The translated exception type.Note, this generic type must be specified by any class implements this interface, otherwise, exception will be raised
SQLException;
or a relatively advanced Jimmer exception,
such as @SaveException.NotUnique
Example:
new ExceptionTranslator<SaveException.NotUnique>() {
@Override
public @Nullable Exception translate(
@NotNull SaveException.NotUnique exception,
@NotNull Args args
) {
if (exception.isMatched(BookProps.NAME, BookProps.EDITION)) {
return new IllegalArgumentException(
"The book whose name is \"" +
exception.getValue(BookProps.NAME) +
"\" and edition is \"" +
exception.getValue(BookProps.EDITION) +
"\" already exists"
);
}
return null;
}
}
After being translated, exceptions can continue to be translated until there is no matching translator. To avoid potential infinite recursion problems, the same translator will not be used twice.
If the final translated exception is
RuntimeException, it will be thrown directly;
otherwise, it will be wrapped and thrown as
ExecutionException
There are 3 ways to setup exception translators.
- Add it to save command, like this
sqlClient.getEntities() .saveCommand(...entity...) .addExceptionTranslator( new ExceptionTranslator<SaveException.NotUnique>() { ... } )This configuration has a higher priority. If the same exception type is configured twice by this method and the global configuration, the configuration of this method takes precedence.
- Global configuration without spring-starter, like this
JSqlClient sqlClient = JSqlClient .newBuilder() .addExceptionTranslator( new ExceptionTranslator<SaveException.NotUnique>() { ... } ) - Global configuration by jimmer-spring-starter,
by
@Componentof spring framework, such as@Component // Let spring know your translator public NotUniqueExceptionTranslator implements ExceptionTranslator<SaveException.NotUnique> { ... }
-
嵌套类概要
嵌套类 -
方法概要
修饰符和类型方法说明static ExceptionTranslator<Exception>of(Collection<ExceptionTranslator<?>> translators) default @Nullable Exceptiontranslate(E exception, @NotNull ExceptionTranslator.Args args) Translate the exception.
-
方法详细资料
-
translate
@Nullable default @Nullable Exception translate(@NotNull E exception, @NotNull @NotNull ExceptionTranslator.Args args) Translate the exception.If the exception is not known how to be translated, return null or the original argument.
-
of
-