Created
April 25, 2020 12:32
-
-
Save arindam89/26950b62707a8f8f7581c3d019f9499a to your computer and use it in GitHub Desktop.
Error Handling Example Real World
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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