Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor orElseThrow block to return RuntimeException

Tags:

java

java-8

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) ?

like image 528
developer Avatar asked Feb 21 '18 18:02

developer


2 Answers

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);
}
like image 56
Vinay Prajapati Avatar answered Nov 18 '22 16:11

Vinay Prajapati


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);
like image 3
Mehraj Malik Avatar answered Nov 18 '22 16:11

Mehraj Malik