-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom ExceptionMapper not invoked when Hibernate rollback #949
Comments
I can confirm that issue. Unfortunately, I can't give a solution now. The problem lies in the update to Jersey 2 in 0.8.0. |
I've made another look and can give a workaround for you. In 0.8.0 in In the Jersey architecture at this stage exception mappers are not taken into account, but mappers that implement the interface ResponseErrorMapper are. environment.jersey().property(ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true); After that you can refactor your exception mapper something like that: @Provider
public class SQLConstraintViolationExceptionMapper implements ResponseErrorMapper {
private static final Logger LOGGER = LoggerFactory.getLogger(SQLConstraintViolationExceptionMapper.class);
@Override
public Response toResponse(Throwable e) {
return Response.status(Response.Status.BAD_REQUEST)
.entity(Collections.singletonMap("errors",
Collections.singletonMap("message", getMessage((ConstraintViolationException) e))))
.build();
}
private List<String> getMessage(ConstraintViolationException e) {
LOGGER.error(e.getMessage(), e);
// a hack to convert exception to friendly error message
if ("fk_budgets_categories".equalsIgnoreCase(e.getConstraintName())) {
return Collections.singletonList("Failed to delete category due to references to existing budget(s).");
} else if ("fk_transactions_budgets".equalsIgnoreCase(e.getConstraintName())) {
return Collections.singletonList("Failed to delete budget due to references to existing transaction(s).");
}
return Collections.singletonList(e.getMessage());
}
} Could you test this approach? If it works for you, I will add information about response error mappers |
The issue is fixed after update based on your comment commit. However, I think the proper way to fix this is to rethrow exception as
|
That actually seems like a better solution. Jersey will rethrow the original exception if it's unmapped and map to I will try to create a PR for fixing that. |
Using DW 0.8.0, Java 8
I have one custom exception mapper to handle
hibernate
exception:I registered the mapper https://github.com/paukiatwee/budgetapp/blob/master/src/main/java/io/budgetapp/BudgetApplication.java#L143
When
org.hibernate.exception.ConstraintViolationException
exception is thrown when foreign key violation occurred,SQLConstraintViolationExceptionMapper
is no called.You can reproduce the problem by running this integration test https://github.com/paukiatwee/budgetapp/blob/master/src/test/java/io/budgetapp/resource/CategoryResourceIT.java#L65
This might caused by https://github.com/dropwizard/dropwizard/blob/master/dropwizard-hibernate/src/main/java/io/dropwizard/hibernate/UnitOfWorkApplicationListener.java#L87 exception not handled by container.
Working fine for DW 0.7.0
The text was updated successfully, but these errors were encountered: