0

There is a problem that causes the code to land in the handleAmbiguousException method, but I do not know what the problem is that causes the code to get there. My question is this: how can I rewrite the code below in order to actually figure out what the real exception is?

Inside handleAmbiguousException, it says there is a problem calling an upstream service, but I have determined that there is no problem from the upstream service. So I think the issue must be in one of the following methods.

First there is the doSomething() method, which calls retrieveConfiguration(). retrieveConfiguration() accepts a func, which I believe is where the problem is.

public void doSomething(final HttpServletRequest httpServletRequest,
                            final AbstractCreateTokenRequest request,
                            final AsyncResponse asyncResponse){


retrieveConfiguration(asyncResponse,
                            configuration -> doInternalMethod(asyncResponse, request, configuration), InternalConfiguration.class);

}`

private <T extends SomeConfiguration> void
    retrieveConfiguration(final AsyncResponse asyncResponse,
                          final Consumer<T> func,
                          final Class<T> configClass) {

    CompletionStage<T> configuration = configurationService.getConfiguration(configClass);

    configuration.thenAccept(func).
            exceptionally(throwable -> {
                handleAmbiguousException(asyncResponse, throwable);
                return null;
            });
}`

Based off log statements I added, I don't think the code ever reaches doInternalMethod, but I have pasted it below here in case it is helpful:

private CompletionStage<Either<ServiceErrorResponse, TokenResponse>>
    doInternalMethod(final AsyncResponse asyncResponse,
                        final AbstractCreateTokenRequest request,
                        final TokenConfiguration configuration) {

    final CompletionStage<Either<ServiceErrorResponse, TokenResponse>> stage = tokenCreator
            .createToken(configuration)
            .exceptionally(this::nestedException);
    stage.whenComplete((response, throwable) -> {
        MDC.clear();
    });
    setAsyncResponseFromEither(stage, asyncResponse, REQUEST_TIMEOUT_MS, request.toString());
    return stage;
}`

I tried adding log statements right before configuration.thenAccept(func), and they successfully logged to console. But my logs in doInternalMethod() were never reached. So it seems like something is happening at the configuration.thenAccept(func) part. But I do not know how to debug/troubleshoot this to figure out what exactly the error is.

People asked for handleAmbiguousException code, and here it is. This is also where I got the stack trace from:

private void
    handleAmibguousException(final AsyncResponse asyncResponse,
                              final Throwable throwable) {
   asyncResponse.resume(ExceptionHandler.mapTheError(throwable));
    }

Printing the stack trace only gives me this: java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)

Converting the throwable to string gets me this: java.util.concurrent.CompletionException: java.lang.NullPointerException

Printing the message of the throwable gets me this: MESSAGE java.lang.NullPointerException

Curiosity
  • 1
  • 1
  • 1
    You can first log the stacktrace of the exception, that will tell you why it occurred. – Lae Jan 24 '23 at 19:31
  • Please make this a [mre], which should include the code of `handleAmbiguousException()`, and provide the exception stack trace. – Didier L Jan 24 '23 at 22:02
  • @Lae, Stack trace says: java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273). And that's the whole stack trace - does that tell us anything? – Curiosity Jan 24 '23 at 23:51
  • @DidierL, I added in handleAmbiguousException code. – Curiosity Jan 25 '23 at 00:00
  • Please refer to https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Didier L Jan 25 '23 at 00:22
  • 1
    You can use [printStackTrace()](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Throwable.html#printStackTrace()) to obtain the whole stack trace. If you read the documentation on that method it will help you understand it. – Lae Jan 25 '23 at 01:02

0 Answers0