Skip to content

Instantly share code, notes, and snippets.

@arindam89
Created April 25, 2020 12:32
Show Gist options
  • Save arindam89/26950b62707a8f8f7581c3d019f9499a to your computer and use it in GitHub Desktop.
Save arindam89/26950b62707a8f8f7581c3d019f9499a to your computer and use it in GitHub Desktop.
Error Handling Example Real World
interface CustomerProvider {
List<String> getPersonalRecomendations(final String customerId) throws IOException,IllegalArgumentException;
}
interface GenericSuggestionProvider {
List<String> getGeneralRecomendations() throws IOException;
}
interface CacheSuggestionProvider {
List<String> getCachedRecomendations();
}
CustomerProvider customer;
GenericSuggestionProvider suggestion;
CacheSuggestionProvider cache;
public List<String> emptySuggestionList() {
return new ArrayList<>();
}
List<String> getRecomendationsForUI(final String customerId) {
return Try.of(() -> customer.getPersonalRecomendations(customerId))
.orElse(() -> Try.of(() -> suggestion.getGeneralRecomendations()))
.recover(IllegalArgumentException.class, cache.getCachedRecomendations())
.recover(IOException.class, cache.getCachedRecomendations())
.andFinally(System.out::println) // You can log metrics etc. here.
.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment