Reputation: 83
public class DefaultCountValidationInterceptor implements ValidateInterceptor
{
@Override
public void onValidate(final Object object, final InterceptorContext interceptorContext) throws InterceptorException
{
if (object instanceof BaseStoreModel)
{
final BaseStoreModel baseStoreModel = (BaseStoreModel) object;
if (baseStoreModel.getCount() < 0 || baseStoreModel.getCount() > 100)
{
throw new InterceptorException("Count should be between 0 and 100");
}
}
}
}
Interceptor Configuration:
<bean id="defaultCountValidationInterceptor"
class="se.istone.hybris.maersk.core.interceptors.DefaultCountValidationInterceptor " />
<bean id="defaultCountValidationInterceptorMapping"
class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor"
ref="defaultCountValidationInterceptor" />
<property name="typeCode" value="BaseStore" />
</bean>
Validation error message is displaying correctly in Hybris5.4 HMC, but
its not workinig in Hybris 6.7(1905) Backoffice
Upvotes: 3
Views: 3799
Reputation: 1
You can resolve this issue by including your interceptor in the supportedInterceptors list of the RuleEngineExceptionTranslationHandler.
<bean depends-on="exceptionTranslationForInterceptors" parent="listMergeDirective">
<property name="add" value="DefaultCountValidationInterceptor"/>
</bean>
Upvotes: 0
Reputation: 1
I tried, it works. Beside you overrided or create new handlers, you also need to add configuration like "remove" original handlers
Upvotes: 0
Reputation: 472
It could be an error copying your code and configuration, but your InterceptorMapping there are errors with the bean ref, it is on capital letter
"DefaultCountValidationInterceptor "
instead of
"defaultCountValidationInterceptor "
Apart from that, you should remove the blank spaces at the end of the bean ids and class attributes of beans declared.
I've been trying to reproduce your error on 1905 Hybris OOTB code, but I cannot reproduce it, I tried it with the AddressValidator (which implement ValidatorInteceptor as well), getting the message from the exception shown on the error alert on backoffice:
Link to Backoffice address validation error alert image
The problem could be if an exception different from InterceptorException is thrown, like a nullPointerException, are you sure of having the count attribute filled an being not null?
It could be obvius, but if any of the attributes on the baseStore is changed, and then saved, the validation interceptor will be runned. My advice is checking nulls before accessing the count attribute for not getting an error, like:
if (baseStoreModel.getCount() !=null && (baseStoreModel.getCount() < 0 || baseStoreModel.getCount() > 100))
Doing the condition on this way will avoid the null problem, because if you're having the count attribute set to null, the condition will exit as soon as the first condition is evaluated as false (baseStoreModel.getCount() != null).
Another way to avoid the null error is having a defaultValue on your *-items.xml definition, adding a:
<defaultvalue>Integer.valueOf(0)</defaultvalue>
@JagadeeshKumar, as @Zaheer Attar has said on his answer https://stackoverflow.com/a/62415830/3346298 (really good point), you could have your own ModelExceptionTranslation Handler. But I would check, before customising anything, what is the error, I mean, what is the exception recieved on the
de.hybris.platform.platformbackoffice.services.handlers.ModelSavingExceptionTranslationHandler
Use a debug point there to check the content of the exception, the cause and even finding out if your message from your validator is there.
Once you know the exception you could know the real reason of why the OOTB TranslationHandler is not working as expected.
Developing a new Handler could not solve the root reason of the error which could create adjacent problems in the next future.
Upvotes: 0
Reputation: 1672
You are always getting default message due OOTB code in ModelSavingExceptionTranslationHandler.toString()
.
public class ModelSavingExceptionTranslationHandler extends ModelExceptionTranslationHandler {
public static final String I18N_UNEXPECTED_UPDATE_ERROR = "unexpected.update.error";
public boolean canHandle(Throwable exception) {
return exception instanceof ModelSavingException
|| exception instanceof ObjectSavingException && exception.getCause() instanceof ModelSavingException;
}
public String toString(Throwable exception) {
return this.getLabelByKey("unexpected.update.error");
}
}
When you are throwing InterceptorException
Hybris internally throws ModelSavingException
with your InterceptorException
as Cause.
Backoffice exception are handled by ExceptionTranslationService
, which contain list of Handlers to handle different kinds of Exceptions. For ModelSavingException
, ModelSavingExceptionTranslationHandler
is used.
Since OOTB Handler is straight up displaying default message, you can either override this class or you can create your own Exception Translation Handler and add it in the handlers list.
Documentation -> https://help.sap.com/viewer/5c9ea0c629214e42b727bf08800d8dfa/1905/en-US/8bc9570b86691014a901c290d2c5f107.html
Upvotes: 2