Last active
February 3, 2020 14:14
-
-
Save radutoev/47e205987cd07daac27bce897ff6b186 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
protected final Function<UserNotPresentError, ResponseEntity<?>> userFacadeErrorHandler = | |
userNotPresentError -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); | |
protected final Function<Either<ItemsError, ?>, ResponseEntity> resultHandler = serviceResult -> serviceResult.fold( | |
itemsError -> handleItemsError(itemsError), | |
successValue -> Match(successValue).of( | |
Case($(), () -> ResponseEntity.ok(successValue).build()) | |
) | |
); | |
protected ResponseEntity handleItemsError(ItemsError itemsError) { | |
return Match(itemsError).of( | |
Case($(instanceOf(DbError.class)), (e) -> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.reason)), | |
Case($(instanceOf(ItemsProcessingError.class)), (e) -> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.reason)), | |
); | |
} | |
@GetMapping("/some-items-endpoing") | |
ResponseEntity controllerMethod() { | |
authenticatedItemsFacade.itemsFn().fold(userFacadeErrorHandler, resultHandler); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment