Java Code Examples for scala.util.Try#isFailure()

The following examples show how to use scala.util.Try#isFailure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultMessageMapperFactory.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Nullable
private static MessageMapper createAnyMessageMapper(final Class<?> clazz,
        final DynamicAccess dynamicAccess) {
    final ClassTag<MessageMapper> tag = scala.reflect.ClassTag$.MODULE$.apply(MessageMapper.class);
    final Try<MessageMapper> mapperTry = dynamicAccess.createInstanceFor(clazz, List$.MODULE$.empty(), tag);

    if (mapperTry.isFailure()) {
        final Throwable error = mapperTry.failed().get();
        if (error instanceof ClassNotFoundException || error instanceof InstantiationException ||
                error instanceof ClassCastException) {
            return null;
        } else {
            throw new IllegalStateException("There was an unknown error when trying to creating instance for '"
                    + clazz + "'", error);
        }
    }

    return mapperTry.get();
}
 
Example 2
Source File: ParseTest.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args){
 Try<BinaryXpr> parsed=XprParser.parseXpr("dico > 5");
 //Try<BinaryXpr> parsed=XprParser$.MODULE$.parseXpr("dfsfsdf");
 if (parsed.isFailure())
  System.out.println("abcdcdc "+parsed.failed().get());
 else System.out.println(parsed.get().toString());
}
 
Example 3
Source File: HttpPublisherActor.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
private void processResponse(final Pair<Try<HttpResponse>, HttpPushContext> responseWithMessage) {
    final Try<HttpResponse> tryResponse = responseWithMessage.first();
    final HttpPushContext context = responseWithMessage.second();
    final ExternalMessage message = context.getExternalMessage();
    final Uri requestUri = context.getRequestUri();
    if (tryResponse.isFailure()) {
        final Throwable error = tryResponse.toEither().left().get();
        final String errorDescription = MessageFormat.format("Failed to send HTTP request to <{0}>.",
                stripUserInfo(requestUri));
        log.debug("Failed to send message <{}> due to <{}>", message, error);
        responsePublishedMonitor.failure(message, errorDescription);
        escalate(error, errorDescription);
    } else {
        final HttpResponse response = tryResponse.toEither().right().get();
        log.debug("Sent message <{}>. Got response <{} {}>", message, response.status(), response.getHeaders());
        if (response.status().isSuccess()) {
            responsePublishedMonitor.success(message,
                    "HTTP call to <{0}> successfully responded with status <{1}>.",
                    stripUserInfo(requestUri), response.status());
            response.discardEntityBytes(materializer);
        } else {
            getResponseBody(response, materializer)
                    .thenAccept(body -> responsePublishedMonitor.failure(message,
                            "HTTP call to <{0}> responded with status <{1}> and body: {2}.",
                            stripUserInfo(requestUri),
                            response.status(), body)
                    )
                    .exceptionally(bodyReadError -> {
                        responsePublishedMonitor.failure(message,
                                "HTTP call to <{0}> responded with status <{1}>. Failed to read body within {2} ms",
                                stripUserInfo(requestUri), response.status(), READ_BODY_TIMEOUT_MS);
                        LogUtil.enhanceLogWithCorrelationId(log, message.getInternalHeaders());
                        log.info("Got <{}> when reading body of publish response to <{}>", bodyReadError,
                                message);
                        return null;
                    });
        }
    }
}