I've got a quick question. In my grails project I am making some web service calls. One of the calls (for a search function) tends to timeout if not enough characters are given to search for. I can't increase the number of required characters so I instead am trying to catch the exception and display an error page asking the user to add more detailed parameters.
The method looks like this:
import org.apache.http.client.HttpResponseException
class RestSearchService implements SearchService {
List<Person> getPersonSearch( String fName, String lName) throws HttpResponseException {
...
// Make the call
...
}
}
I then catch the thrown exception in the controller to redirect to the error page. I've tested it and this code appears to work fine. The problem is that the method above is underlined (I'm using SpringSource Tool Suite for an IDE) and says
Exception HttpResponseException is not compatible with
throws clause in SearchService.getPersonSearch(String, String)
Does anyone know what might be causing this? Also, might it mean that there is an actual problem or circumstance where this will cause the app to break? Like I said, from what I can tell the throw/redirect is working like a champ, but that error makes me nervous about moving the app to production.
Thanks in advance,
-Mike
I would say that your interface SearchService is not right! what is the signature of the method 'getPersonSearch' in the interface?
its like this:
List<Person> getPersonSearch( String fName, String lName);
or like this:
List<Person> getPersonSearch( String fName, String lName) throws HttpResponseException;
the second one is correct, if you have the first one, thats should be the problem!
Related
So yeah, I have an issue where I can't test my Minecraft mod, and it does not explain what's the cause. I've done it correctly, yet I can't get over this error. Its a toilet mod. IDE I use is IntelliJ. It's also on fabric, if you are a forge modder. There's no further explanation.
The error
This is the output I get. The output just shows me the error, and I've tried the suggestions, but it seems to worsen
#Override
public void onInitialize() {
Registry.register(Registry.BLOCK, new Identifier("toilet", "ceramic_toilet"), CERAMIC_TOILET);
Registry.register(new Identifier("toilet", "ceramic_toilet"), new BlockItem(CERAMIC_TOILET, new Item.Settings().group((ItemGroup.MISC)));
}
}
Given the information you provide, as Joseph Sible-Reinstate Monica suggests: it's a syntax error.
The #Override annotation should not have a semicolon after it.
Also your second register method is missing a closing bracket, add another one like so:
Registry.register(new Identifier("toilet", "ceramic_toilet"), new BlockItem(CERAMIC_TOILET, new Item.Settings().group((ItemGroup.MISC))));
i have a problem that i don't exactly know how to solve. I'm implementing an E2E test in which using selenium i need to click in a Link and check that is sending me to the right URL.
Here starts the problem...
There are 3 possibilities, mix of 2 types of links, just one type of link or the other type of link. No problems with the situations in which there are both types of links but when there is just one type when it searches for the identifier we use for the links that are not in page it gives me a timeoutException. This is not a failure because it's a posible situation but i will like to know if there is a way in which to check that if it finds no links it asserts that the exception is thrown.
I though using a runCatching (or try catch) wait for the link to appear and if it doesn't appear the test asserts that when i look for the element the timeout exception is thrown again.
It smells a bit for me this way of doing it and i don't know if it's correctly done.
EDIT: Im ussing AssertK and JUnit5 for testing.
EDIT 2: I've done this, i dont know if it a correct way of doing it
runCatching {
driver.waitFor(numberOfWidgetsToBeMoreThan(BrowserSelector.cssSelector(OFFERS_WITH_PRICE_AND_DATE), 0), ofMillis(2000))
}.onFailure {
assertThrows<WaitTimeoutException> {
findLink(OFFERS_WITH_PRICE_AND_DATE)
}
}.onSuccess {
val widget = findLink(OFFERS_WITH_PRICE_AND_DATE)
widget.click()
assertThat(driver.url).contains(NO_DATE_TEXT)
}
I'm not sure I understood your problem correctly, but you can use assertFails to assert that a piece of code throws an exception:
#Test
fun test() {
val exception = assertFails {
// some code that should throw
}
// some more assertions on the type of exception etc. may go here
}
I am using cucumber-jvm.
I have an init method to initialize all the necessary stuff, such as the browser dimensions, application url etc.
I have put this init method under a #Before (cucumber.api) tag.
#Before
public void initLoginPage() throws Exception {
getBrowserDimension();
setBrowserCapabilities();
init(getApplicationUrl());
}
My life was fine with this running smoothly.
Now, I also wanted to use #Before for some tags at scenario levels.
Say my scenario looks like:
#myTag
When I do blah
Then I should get blah-blah
And I wanted to use something like:
#Before(#myTag)
public void beforeScenario(){
blah = true;
}
But the moment I give it another #Before, it starts giving a NullPointerException. I tracked it back to the runBeforeHooks and runHookIfTagsMatch methods in Cucumber's Runtime class.
They are throwing the exception for the #Before (for initLoginPage()) itself.
Is there a conflict getting created with multiple #Before's?
How can I resolve this?
I found the solution to get this working.
The problem was that any of the #Before codes were getting picked up in a random order. It wasn't based on the assumption that a #Before without parameters will be executed before #Before("myTag").
So the trick is to assign order parameter (in #Before) some value. The default order that gets assigned to #Before is 10000. So, if we define the order value explicitly, it should work.
So basically, my code for initializer could look like:
#Before(order=1)
public void initLoginPage() throws Exception {
getBrowserDimension();
setBrowserCapabilities();
init(getApplicationUrl());
}
That solved my problem
According to Phillip Riand (see: discussion on openNTF) this is not possible... They need to know the design element to find out who signed it. Therefore, it is only available in SSJS.
There are 2 ways that I know of to use the sessionAsSigner object in Java beans:
1 By resolving the sessionAsSigner object:
FacesContext context = FacesContext.getCurrentInstance();
Session sessionAsSigner = context.getApplication().getVariableResolver().
resolveVariable(context, "sessionAsSigner");
2 By using the getCurrentSessionAsSigner() function from the com.ibm.xsp.extlib.util.ExtLibUtil class in the Extension Library.
To be able to use it (in Java as wel as SSJS) you'll want to make sure that all design elements were signed by the same user ID. If that's not the case, the sessionAsSigner object will not be available ('undefined').
I found that the solution is right at hand :-)
I changed my XPage (in this example an XAgent) to:
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
This is an xAgent returning json data...
<xp:this.afterRenderResponse><![CDATA[#{javascript:Controller.verify(sessionAsSigner)}]]></xp:this.afterRenderResponse>
and in the bean I simply used the session in the argument when I needed to open a database/document as signer. Sometimes the solution is so simple :-)
/John
This is quite an old post that I just stumbled upon. Tried some of the solutions mentioned above:
resolveVariable did not work for me, at least not for sessionAsSigner as this throws a runtime error (I can resolve plain old session, though...)
to be honest I didn't quite understand the Controller.verify(sessionAsSigner) method; is Controller something specific to XAgents? If so, I don't have an XAgent here, so can't use it
didn't feel like importing extra ExtLib classes here...
So I came up with another solution that appears to be very simple:
created a method in my javaBean that takes a session object as argument; since sessionAsSigner belongs to the same class as session I don't have to import something new.
Javacode is:
public void testSession(Session s) throws Exception{
System.out.println(" > test effective user for this session = "
+ s.getEffectiveUserName());
}
This is called from SSJS as either
mybean.testSession(session);
or
myBean.testSession(sessionAsSigner);
Maybe helps others, too
What's the best practice for handling exceptions in NHibernate?
I've got a SubjectRepository with the following:
public void Add(Subject subject)
{
using (ISession session = HibernateUtil.CurrentSession)
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(subject);
transaction.Commit();
}
}
And a Unit Test as follows:
[Test]
public void TestSaveDuplicate()
{
var subject = new Subject
{
Code = "En",
Name = "English"
};
_subjectRepository.Add(subject);
var duplicateSubject = new Subject
{
Code = "En",
Name = "English1"
};
_subjectRepository.Add(duplicateSubject);
}
I got to the point of handling the error generated by the unit test and got a bit stuck. This fails as expected, though with a GenericADOException, I was expecting a ConstraintViolationException or something similar (there is a uniqueness constraint on the subject code at database level).
The ADOException wraps a MySQL Exception that has a sensible error message but I don't want to start breaking encapsulation by just throwing the inner exception. Particularly as MySQL isn't finalised as the back end for this project.
Ideally I'd like to be able to catch the exception and return a sensible error to the user at this point. Are there any documented best practice approaches to handling NHibernate Exceptions and reporting back up to the user what went wrong and why?
Thanks,
Matt
I would handle it in the Add method as such:
public void Add(Subject subject)
{
using (ISession session = HibernateUtil.CurrentSession)
using (ITransaction transaction = session.BeginTransaction())
{
try
{
session.Save(subject);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
// log exception
throw;
}
}
}
In the catch block, you should first rollback the transaction and log the exception. Then your options are:
Rethrow the same exception, which is what my version does
Wrap it in your own exception and throw that
Swallow the exception by doing nothing, which is very rarely a good idea
You don't have any real options for handling the exception in this method. Assuming that the UI calls this method, it should call it in its own try..catch and handle it by displaying a meaningful error message to the user. You can make your unit test pass by using the ExpectedException(type) attribute.
To answer your question directly, you should create your own "sensible error" by extending Exception and throw that with the original exception as its InnerException. That's the exception wrapping technique I listed in (2).
All the Nhibernate exceptions are non recoverable, you could revisit the design of the app/data layer if you are trying to recover from nhibernate exceptions .
You can also Take a look at spring.net 's exception translation implementaion
Also you manually handling transactions on exceptions is tedious and error prone, take a look at nhibernate's contextual sessions .
Spring.net also has some nice helpers around nhibernate .
The general question is going to be, what do you want to tell the user, and who is the user?
If the user will sometimes be another computer (i.e., this is a web service), then you would want to use the appropriate mechanism to return a SOAP Fault or HTTP error.
If the user will sometimes be a UI of some sort, then you may want to display a message to the user, but what would you tell the user so he can do something about it? For instance, most web sites will say, "sorry, we had an unexpected error", no matter what the reason. That's because there's usually nothing the user could do about the error.
But in either case, the choice of how to tell "the user" is a matter for the Presentation layer (UI tier), not for the DAL. You should possibly wrap exceptions from the DAL in another exception type, but only if you're going to change the message. You don't need your own exception class, unless your callers would do something different if it's a data access exception rather than some other kind.
I'd probably validate the input before saving the object; that way you can implement whatever validation you like (e.g. check the length of the Subject Code as well as the fact that there aren't any duplicates), and pass back meaningful validation errors back to the user.
The logic is as follows; exceptions are used to indicate exceptional circumstances that your program doesn't cater for. A user entering a duplicate Subject Code in your example above is something your program should be catering for; so, rather than handling an exception because a DB constraint gets violated (which is an exceptional event and shouldn't be occurring), you'd want to handle that scenario first and only attempt to save the data when you know that the data you're saving is correct.
The advantage with implementing all validation rules in your DAL is that you can then ensure that the data going into your DB is valid as per your business processes in a consistent manner, rather than relying on the constraints in your DB to catch those for you.