Skip to content

Try throwException #257

@jainh

Description

@jainh

How to throw exception if one of the operations faile

Try<CompletableFuture<Boolean>, IOException> request = Try.withCatch(() -> Convertor.fromJsonToPojo(payload, PepRequest.class))
                .onFail(e -> LOG.error("failed to serialize Json payload:{} exception:{}",payload, e))
                .map(pepReq -> (new PdpRequest()).copyFromParameters(pepReq, user.getUsername()))
                .onFail(e -> LOG.error("failed to create pdp request model because of the following reason:{}",e))
                .flatMap(pdpRequest -> Try.withCatch(() -> new ObjectMapper().writeValueAsString(pdpRequest)))
                .onFail(e -> LOG.error("failed to convert pdp request to json reason:{}",e.getMessage()))
                .flatMap(requestBody -> Try.withCatch(() -> httpClient.execute(requestBody)))
                .onFail(e -> LOG.error("failed to execute request:{} because of the following reason:{}",payload, e.getMessage()));

If successful return completablefuture

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions