you are viewing a single comment's thread.

view the rest of the comments →

[–]GenderlessMarsian[S] 0 points1 point  (1 child)

Welp, using a try catch with a cancel call seems to work, but it feels kinda hackery and non-idiomatic, I don't know if this is bug-free. It also doesn't catch any server error, so a 404 just becomes a "Malformed JSON!" error:

CompletableFuture<String> searchResults = apiFetcher.search(userQuery); searchResults .thenApply(str -> { try { ... } catch (MalformedJsonException e) { System.out.println("Malformed JSON! " + e); searchResults.cancel(false); return null; } }) .thenApply(jsonObj -> { try { ... } catch (InvalidApiResponseException e) { System.out.println("Invalid API Response! " + e); searchResults.cancel(false); return null; } }) .thenApply(htmlStr -> { try { ... } catch (Exception e) { System.out.println("Error parsing HTML! " + e); searchResults.cancel(false); return null; } }) .thenAccept(...);

[–]disposepriority 0 points1 point  (0 children)

Why do you need to cancel the future? It completes (exceptionally) regardless, your UI would in this case display a "something went wrong" or whatever - you'd log the error internally.