Skip to content

Instantly share code, notes, and snippets.

@radutoev
Last active February 3, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radutoev/47e205987cd07daac27bce897ff6b186 to your computer and use it in GitHub Desktop.
Save radutoev/47e205987cd07daac27bce897ff6b186 to your computer and use it in GitHub Desktop.
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