-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Make CheckedTemplate support exceptions #38839
Comments
//cc: Maybe this is relevant @FroMage, if not sorry for the tag. |
/cc @mkouba (qute) |
I'm not sure what you mean here. Exceptions thrown during the controller's endpoint method will cause a rollback of the transaction, whether they return a It's possible that exceptions that occur during rendering of the template, which happens after the endpoint method is called, do not cause a rollback, because the transaction has been committed at this time. Perhaps this is what you're referring to? |
I agree, generally the rollback works as excepted when an exception is thrown, but in this case i use a try catch block in order to display the correct error message based on the exception so it won't cause the transaction to rollback. In order for the transaction to rollback i would have to rethrow an exception like this: @Path("login")
public TemplateInstance loginPage(User user)
try {
authService.register(user);
} catch (Exception e) {
// transaction is rollbacked
var response = Response
.ok()
.entity(AdminTemplate.auth(e))
.build();
throw new WebApplicationException(response);
}
return AdminTemplate.auth();
} So, instead of rethrowing the exception, a special CheckedTemplate (type,annotation,etc,) could inform the transaction to rollback. |
Oh, I see what you mean, you want to handle the exception but force a rollback. How about https://quarkus.io/guides/transaction#declarative-approach: @Inject TransactionManager tm;
@Path("login")
public TemplateInstance loginPage(User user)
try {
authService.register(user);
} catch (Exception e) {
tm.setRollbackOnly();
return AdminTemplate.auth(e);
}
return AdminTemplate.auth();
} This should work if you're using Hibernate ORM (not reactive). |
That is definitely working (just tested it in Renarde). What i'm looking for is more about the Developer Joy (and less error prone, forget calling the rb method) otherwise i could use either of the following two static methods: public static TemplateInstance rb(TemplateInstance templateInstance){
try {
Arc.container().instance(TransactionManager.class).get().setRollbackOnly();
} catch (SystemException e) {
throw new RuntimeException(e);
}
return templateInstance;
}
public static TemplateInstance rb(TemplateInstance templateInstance){
var response = Response
.ok()
.entity(templateInstance)
.build();
throw new WebApplicationException(response);
} @Path("login")
public TemplateInstance loginPage(User user)
try {
authService.register(user);
} catch (Exception e) {
return rb(AdminTemplate.auth(e));
}
return AdminTemplate.auth();
} Well, if this is out of the scope i think i can live with that :) |
I suppose I could add a |
Alright, I opened an issue in Renarde :) |
Thanks |
Description
When i return
TemplateInstance
in my Renarde application it means that any exception that i catch in a try catch block is not rollbacked.In the following example the transaction is not rollbacked.
In order to fix this issue we could change the
AdminTemplate
to support "exceptions". One way could be to add a new return typeExceptionTemplateInstance
. Another way could be to have a special nameauth$Exception()
(or something like that), or a method with throwsauthException() throws WebApplicationException
or add a new@Rollback
annotation to the method. In all cases instead of just rendering the template, it would throw WebApplicationException and as it's content it would be the rendered template.This is more or less my brainstorming ideas, what do you think?
Implementation ideas
No response
The text was updated successfully, but these errors were encountered: