Jsoup connection to URL I have simple class that I want to ask if is there any possible to improve it? I mean, for me it looks poor. Is there any way to use here try-with-resources, stream or optional?

package bookstore.scraper;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

@Slf4j
public class JSoupConnector {

    public static Document prepareConnectionToURL(String URL) {
        Document document = null;
        try {
            document = Jsoup.connect(URL).get();
        } catch (IOException e) {
            log.warn("Cannot connect to URL!");
        }
        return document;
    }
}
This error handling looks pretty useless to me:


The log message doesn't record which URL was inaccessible.
If an error occurs, then the code will continue to execute. That means that whoever calls this function will have to handle the possibility of a null result. If that isn't done, then it will cause a NullPointerException.


A better idea would be to simply propagate the exception, so that the caller will stop trying to process a Document that isn't there.

Furthermore, what does it mean to "prepare a connection"? It looks like this code goes way beyond preparing a connection — it actually makes the connection and fetches the content as a Document!

So, the class should look more like this:

public class JSoupConnector {
    public static Document connect(String url) throws IOException {
        try {
            return Jsoup.connect(url).get();
        } catch (IOException e) {
            log.warn("Cannot connect to URL " + url);
            throw e;
        }
    }
}


At that point, this wrapper is hardly doing anything at all, and you might consider eliminating this helper functional altogether.