on ‎2020 Mar 13 4:15 PM
Hi, I have created a Validate Interceptor on an my product
public class MyProductValidatorInterceptor implements ValidateInterceptor
{
@Override
public void onValidate(final Object model, final InterceptorContext ctx) throws InterceptorExceptio
{
if (model instanceof MyProductModel
&& (ctx.isNew(model) || ctx.isModified(model, ChemicalProductModel.APPROVALSTATUS)))
{
final MyProductModel myProductModel = (MyProductModel) model;
final ArticleApprovalStatus approvalStatus = chemicalProductModel.getApprovalStatus();
if (ArticleApprovalStatus.APPROVED == approvalStatus)
{
throw new InterceptorException("
The product cannot be saved in approved because there are no mandatory fields");
}
}
}
}
but a generic error message is shown in the backoffice

how can i show my error message?
Request clarification before answering.
I solved by changing the approach, i created a custom TypeConstraint validation
public class ApprovedProductValidator implements ConstraintValidator<ApprovedProduct, Object>
{
@Override
public boolean isValid(final Object model, final ConstraintValidatorContext arg1)
{
if (model instanceof MyProductModel)
{
final MyProductModel myProduct = (MyProductModel ) model;
final ArticleApprovalStatus approvalStatus = chemicalProductModel.getApprovalStatus();
if (ArticleApprovalStatus.APPROVED == approvalStatus)
{
.....
return false;
}
}
return true;
}
now saving is blocked, a message is shown

You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The implementation of the method toString on the ModelSavingExceptionTranslationHandler, wich is responsible for resolving the error messages shown in the bakoffice when ever a ModelSavingException occurs, has changed in the release 1905.
Hybris internally catchs all the InterceptorExeptions and then rethrow a ModelSavingException with thos exceptions as cause.
The 1905 release's implementation is as follow :
public String toString(Throwable exception) {
return this.getLabelByKey("unexpected.update.error");
}And with this implementation the error message will always the same, no matter what the cause is.
For the sake of comparaison, the implementation in 6.4 shows the detail message of the thrown exception and it is as follow :
public String toString(Throwable exception) {
ModelSavingException modelSavingException = (ModelSavingException)(exception instanceof ModelSavingException ? exception : exception.getCause());
String messageByCause = this.translateByCause(modelSavingException.getCause());
String message = (String)StringUtils.defaultIfBlank(messageByCause, modelSavingException.getLocalizedMessage());
return StringUtils.capitalize((String)StringUtils.defaultIfBlank(message, ""));
}This should be a bug because the documentation in 1905 still says that the thrown exception's message will be shown, otherwise the documentation is not up to date.
This could be fixed by overriding the ModelSavingExceptionTranslationHandler, but this would affect the hole platform and we don't know yet why the implementation has been changed in the first place. So it is more wise to report a ticket to the SAP support about this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
To solve this, we replaced the default ModelSavingExceptionTranslationHandler with our own implementation:
/**
* Exception translation handler that output the exception reason. This is needed because the SAP Commerce
* Handler ({@link de.hybris.platform.platformbackoffice.services.handlers.ModelSavingExceptionTranslationHandler})
* will only output a generic message that "something is wrong".
*/
public class CustomBackofficeExceptionTranslationsHandler implements ExceptionTranslationHandler {
@Override
public boolean canHandle(Throwable throwable) {
return true;
}
@Override
public String toString(Throwable throwable) {
StringBuilder exceptionText = new StringBuilder();
if (StringUtils.isNotEmpty(throwable.getMessage())) {
exceptionText.append(throwable.getMessage());
} else {
exceptionText.append(throwable.toString()).append(" ").append(throwable.getClass());
}
return exceptionText.toString();
}
}
To register it, the following bean is needed:
<alias name="customPlatformBackofficeExceptionTranslationStrategiesExtender" alias="platformBackofficeExceptionTranslationStrategiesExtender"/>
<cng:list-extender id="customPlatformBackofficeExceptionTranslationStrategiesExtender" property="exceptionHandlers" bean="exceptionTranslationService">
<cng:add value-type="com.hybris.cockpitng.service.ExceptionTranslationHandler">
<bean class="ch.your.package.path.CustomBackofficeExceptionTranslationsHandler" />
</cng:add>
</cng:list-extender>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is helpful, but to override the ModelSavingExceptionTranslationHandler class, the following bean definition is needed. By adding a custom bean you add new TranslationHandler to the list, and since the ExceptionTranslationService returns the first handler's response, it's not guaranteed to see your handler's message if any previous handler can handle the exception.
<alias name="defaultPlatformBackofficeExceptionTranslationStrategiesExtender"
alias="platformBackofficeExceptionTranslationStrategiesExtender"/>
<cng:list-extender id="defaultPlatformBackofficeExceptionTranslationStrategiesExtender" property="exceptionHandlers"
bean="exceptionTranslationService">
<cng:add value-type="com.hybris.cockpitng.service.ExceptionTranslationHandler">
<bean class="com.custom.backoffice.CustomModelSavingExceptionTranslationHandler"/>
<bean class="de.hybris.platform.platformbackoffice.services.handlers.ModelRemovalExceptionTranslationHandler"/>
</cng:add>
</cng:list-extender>
don't forget to change alias name and id
<alias name="customPlatformBackofficeExceptionTranslationStrategiesExtender"
alias="platformBackofficeExceptionTranslationStrategiesExtender"/>
<cng:list-extender id="customPlatformBackofficeExceptionTranslationStrategiesExtender" property="exceptionHandlers"
bean="exceptionTranslationService">
<cng:add value-type="com.hybris.cockpitng.service.ExceptionTranslationHandler">
<bean class="com.nexus2i.deliveryman.calendar.backoffice.logicHandler.CustomBackofficeExceptionTranslationsHandler"/>
<bean class="de.hybris.platform.platformbackoffice.services.handlers.ModelRemovalExceptionTranslationHandler"/>
</cng:add>
</cng:list-extender>
Thanks a lot, Crescenzo.
But I would like to stick to the original approach since I don't want to change all existing Interceptors (throwing an InterceptorException with custom message) and Unit-Tests in my project.
public class CustomerXInterceptor implements ValidateInterceptor<CustomerModel> {
@Override
public void onValidate(final CustomerModel customer, final InterceptorContext ctx)
throws InterceptorException {
if (condition) {
throw new InterceptorException("my custom error message");
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi torsten.lorenz,
to show custom messages, try to use
if (condiction){
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("My error message").addConstraintViolation();
return false;
}
Regards,
Crescenzo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Crescenzo,
I have the same problem. I used to use the same approach to display custom error messages in the Backoffice ...
throw new InterceptorException("my custom error message")So this seems to be a "new" problem popping up with release 1905.?!
Which version do you use?
Any comments from SAP/Hybris regarding this?
Best regards!
Torsten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.