I have got the below findProductBetweenPriceRange() method which throws BadRequestException as shown below:
public Product findProductBetweenPriceRange(int price1, int price2) {
Optional<Product> product = productService.findProductBetween(price1, price2);
return product.orElseThrow(() -> {
String debugInfo = "price1="+price1+";price2="+price2;
throw new BadRequestException("No Product found for price range",debugInfo);
});
}
BadRequestException class:
public final class BadRequestException extends RuntimeException {
public BadRequestException(String errorMessage, String debugInfo) {
this.errorMessage = errorMessage;
this.debugInfo = debugInfo;
}
//fields & getters
}
The above code works fine, but, I just wanted to refactor the orElseThrow() block to a different method as shown below.
I tried creating throwBadRequestException() method which throws BadRequestException & I am calling it from orElseThrow(),
but I am facing an error like "no instance of type variable exists so that void conforms".
public Product findProductBetweenPriceRange(int price1, int price2) {
Optional<Product> product = productService.findProductBetween(price1, price2);
return product.orElseThrow(() -> throwBadRequestException(price1, price2));
}
private void throwBadRequestException(int price1, int price2) {
String debugInfo = "price1="+price1+";price2="+price2;
throw new BadRequestException("No Product found for price range", debugInfo);
}
I clearly know that BadRequestException is an unchecked exception so the compiler is not able to find that the method throws the required exception. Just to double check, I also tried to add the BadRequestException using throws clause to the method signature to hint the compiler, but no luck.
So, the question is that is there any way that I can refactor orElse block nicely to a separate method and throw the RuntimeException (i.e., BadRequestException) ?
So just return the BadRequestException and you are done.
The void is basically causing the issue. orElseThrow takes a supplier, not a function.
So, you need to modify the code as below:
public Product findProductBetweenPriceRange(int price1, int price2) {
Optional<Product> product = productService.findProductBetween(price1, price2);
return product.orElseThrow(() -> throwBadRequestException(price1, price2));
}
private BadRequestException throwBadRequestException(int price1, int price2) {
String debugInfo = "price1="+price1+";price2="+price2;
return new BadRequestException("No Product found for price range", debugInfo);
}
In Java-8 you can use BiFunction to do it.
private BiFunction<String, String, BadRequestException> badReqExceptionSupplier = (msg, info) -> new BadRequestException(msg, info);
Use it as below :
throw badReqExceptionSupplier.apply("No Product found for price range",debugInfo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With