Error handling in the cycle for() - error-handling

How to make the cycle for() to restart or skip when there is an error?
My code moves on web pages, but fails on a non-existent page and returns an error and stops.
I tried try-catch, but I did not understand how to do a re-start or skip.
It should be a simple solution.

function A() {
// do something
}
function B() {
try {
// A();
}
catch(Exception e){
log ( e );
A();
}
finally {
// whatever you want whether theres an exception or not
}
}
I don't know if this might work, but it worths a try.

Related

Getting a warning when use objectmapper in flux inappropriate blocking method call in java reactor

i am new to reactor, i tried to create a flux from Iterable. then i want to convert my object into string by using object mapper. then the ide warns a message like this in this part of the code new ObjectMapper().writeValueAsString(event). the message Inappropriate blocking method call. there is no compile error. could you suggest a solution.
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});
I will give you an answer, but I don't pretty sure this is what you want. it seems like block the thread. so then you can't get the exact benefits of reactive if you block the thread. that's why the IDE warns you. you can create the mono with monoSink. like below.
AtomicReference<ObjectMapper> objectMapper = new AtomicReference<>(new ObjectMapper());
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
return Mono.create(monoSink -> {
try {
monoSink.success(objectMapper .writeValueAsString(event));
} catch (JsonProcessingException e) {
monoSink.error(e);
}
});
})
.cast(String.class) // this cast will help you to axact data type that you want to continue the pipeline
.subscribe(jsonString -> {
System.out.println("jsonString = " + jsonString);
});
please try out this method and check that error will be gone.
it doesn't matter if objectMapper is be a normal java object as you did. (if you don't change). it is not necessary for your case.
You need to do it like this:
Flux.fromIterable(Arrays.asList(new Event(), new Event()))
.flatMap(event -> {
try {
return Mono.just(new ObjectMapper().writeValueAsString(event));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
})
.subscribe(jsonStrin -> {
System.out.println("jsonStrin = " + jsonStrin);
});

mockk every {}.throws() Exception fails test

I need to verify that a certain call is not made, when a previous method call throws an Exception.
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
sut.live()
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
Problem with this code, it fails because of the Exception thrown and not because of the failed verification.
I understand that your WHEN block will always throw an exception.
In that case you have multiple options from my point of view:
Simple plain Kotlin. Wrap the WHEN block with a try-catch block, e.g. like this:
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
var exceptionThrown: Boolean = false
try {
sut.live()
} catch(exception: NotHungryException) {
// Maybe put some assertions on the exception here.
exceptionThrown = true
}
assertTrue(exceptionThrown)
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
For a bit nicer code, you can use JUnit5 API's Assertions. assertThrows will expect an exception being thrown by a specific piece of code. It will fail the test, if no exception is thrown. Also it will return the thrown exception, for you to inspect it.
import org.junit.jupiter.api.Assertions
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
val exception = Assertions.assertThrows(NotHungryException::class.java) { sut.live() }
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
If you're using Kotest you can use the shouldThrow assertion. Which also allows you to retrieve the thrown exception and validate its type.
import io.kotest.assertions.throwables.shouldThrow
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
val exception = shouldThrow<NotHungryException> { sut.live() }
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
I had similar issue and found that my method is not surrounded by try catch. This mean the method will always throw exception.
Test
The unit test to verify the result when the following method is called while stubbing it with predefine Exception
#Test
fun returnSearchError() {
every { searchService.search(query) }.throws(BadSearchException())
val result = searchRepository.search(query)
assertEquals(SearchStates.SearchError, result)
}
Faulty code
fun search(query: String): SearchStates {
val result = searchService.search(query) // No try catch for the thrown exception
return try {
SearchStates.MatchingResult(result)
} catch (badSearchException: BadSearchException) {
SearchStates.SearchError
}
}
Refactored it to
fun search(query: String): SearchStates {
return try {
val result = searchService.search(query)
SearchStates.MatchingResult(result)
} catch (badSearchException: BadSearchException) {
SearchStates.SearchError
}
}

Failure failing in CATCH

I'm probably overlooking something simple, but I do not expect the below code to fail. It is behaving as if I wrote die instead of fail in the catch block.
The Failure does not get properly handled and the code dies.
sub foo()
{
try {
say 1 / 0;
CATCH { default { fail "FAIL" } }
}
return True;
}
with foo() {
say "done";
}
else
{
say "handled {.exception.message}"
}
Output:
FAIL
in block at d:\tmp\x.pl line 5
in any at d:\tmp\x.pl line 5
in sub foo at d:\tmp\x.pl line 4
in block <unit> at d:\tmp\x.pl line 11
To bring home to later readers the full force of what Yoda said in their comment, the simplest solution is to unlearn the notion that you have to try in order to CATCH. You don't:
sub foo()
{
say 1 / 0;
CATCH { default { fail "FAIL" } }
return True;
}
with foo() {
say "done";
}
else
{
say "handled {.exception.message}"
}
correctly displays:
handled FAIL
According to the Failure documentation this seems to be the defined behavior.
Sink (void) context causes a Failure to throw, i.e. turn into a normal exception. The use fatal pragma causes this to happen in all contexts within the pragma's scope. Inside try blocks, use fatal is automatically set, and you can disable it with no fatal.
You can try to use the no fatal pragma.
sub foo() {
try {
no fatal;
say 1 / 0;
CATCH { default { fail "FAIL" } }
}
}
unless foo() {
say "handled"
}

How to use If-else condition in Selenium (Java) to make the condition in else block work

I am trying to find an element in the else block, if the condition in the if block doesn't work.
try
{
if(Driver.driver.findElement(By.xpath("//div[#id='Tpay_success']")).isDisplayed())
{
System.out.println("Payment is successful");
Reporter.log("Payment is successful");
}
else
{
Thread.sleep(2000);
{
if(Driver.driver.findElement(By.id("pay_decline")).isEnabled())
{
System.out.println("pay declined");
action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
Reporter.log("PAYMENT DECLINED!!");
}
}
catch(ExceptionInInitializerError ex)
{
System.out.println(ex);
}
}
I am getting an error saying:
Unable to locate element: {"method":"xpath","selector":"//div[#id='Tpay_succes']"}
I want the else block to be executed if the if block doesn't get executed.
Any suggestions are welcome. Thanks.
if(Driver.driver.findElement(By.xpath("//div[#id='Tpay_success']")).isDisplayed())
This doesn't work the way you think it does. findElement() will throw an error if the element is not present in the DOM of the currently loaded page. This means that isDisplayed() will not be called in such a case.
You can do what you want with a try...catch:
try
{
Driver.driver.findElement(By.xpath("//div[#id='Tpay_success']"))
System.out.println("Payment is successful");
Reporter.log("Payment is successful");
} catch(ExceptionInInitializerError ex) {
Thread.sleep(2000);
if(Driver.driver.findElement(By.id("pay_decline")).isEnabled()) {
System.out.println("pay declined");
action.moveToElement(By.id("pay_decline")).isEnabled()).click().perform();
Reporter.log("PAYMENT DECLINED!!");
}
}
Note that you should learn about how to make the selenium driver wait for a specific element rather than using Thread.sleep().

How to use espresso matcher as a condition for If else statement?

The snippet of my code looks like this:
private void SelectOnline(String env) {
onView(withText("Some Text")).perform(click());
if (onView(withText(env)).check(matches(isChecked()))) {
onView(withId(R.id.dialogCancel)).perform(click());
}else {
onView(withText(env)).perform(click());
}
}
I got an error with message
required: boolean
found: ViewInteraction
This means that you cannot use espresso View Matchers as a condition for if..else. Is there some other way to implement if..else statement ?
Espresso was designed in a way to discourage devs from using conditionals, so there's no officially supported way to do this.
However, there are hacks you can try. I use try/catch statements. In your case, it would be something along the lines of:
private void SelectTransitBackendOnline(String env) {
onView(withText("Some Text")).perform(click());
try {
onView(withText(env)).check(matches(isChecked())))
onView(withId(R.id.dialogCancel)).perform(click());
} catch (AssertionFailedError e) {
onView(withText(env)).perform(click());
}
}
Depending on what you are doing in the try block, change the catch block's exception. I wanted to click on cancel_button if it exists, so I changed it to:
try {
onView(withId(R.id.cancel_button)).perform(click());
} catch (NoMatchingViewException ignore) {
}