Skip to content

Commit 40437ee

Browse files
committed
#324: fix of NPE in error handling
1 parent 51b92f1 commit 40437ee

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

modules/service/src/main/java/com/devonfw/module/service/common/base/client/async/AbstractAsyncServiceClient.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public abstract class AbstractAsyncServiceClient<S> implements AsyncServiceClien
3030
/** The {@link #setErrorHandler(Consumer)} */
3131
protected Consumer<Throwable> errorHandler;
3232

33+
/** The most recent invocation. */
34+
private ServiceClientInvocation<S> invocation;
35+
3336
/**
3437
* The constructor.
3538
*
@@ -66,9 +69,8 @@ public void setErrorHandler(Consumer<Throwable> errorHandler) {
6669
@Override
6770
public <R> void call(R result, Consumer<R> resultHandler) {
6871

69-
ServiceClientInvocation<S> invocation = getInvocation();
7072
try {
71-
doCall(invocation, resultHandler);
73+
doCall(getInvocation(), resultHandler);
7274
} catch (Throwable t) {
7375
this.errorHandler.accept(t);
7476
}
@@ -78,40 +80,42 @@ public <R> void call(R result, Consumer<R> resultHandler) {
7880
public void callVoid(Runnable serviceInvoker, Consumer<Void> resultHandler) {
7981

8082
serviceInvoker.run();
81-
ServiceClientInvocation<S> invocation = getInvocation();
8283
try {
83-
doCall(invocation, resultHandler);
84+
doCall(getInvocation(), resultHandler);
8485
} catch (Throwable t) {
8586
this.errorHandler.accept(t);
8687
}
8788
}
8889

8990
private ServiceClientInvocation<S> getInvocation() {
9091

91-
ServiceClientInvocation<S> invocation = this.stub.getInvocation();
92-
Objects.requireNonNull(invocation, "invocation");
93-
ServiceContext<S> context = invocation.getContext();
92+
this.invocation = this.stub.getInvocation();
93+
Objects.requireNonNull(this.invocation, "invocation");
94+
ServiceContext<S> context = this.invocation.getContext();
9495
Objects.requireNonNull(context, "context");
95-
Method method = invocation.getMethod();
96+
Method method = this.invocation.getMethod();
9697
Objects.requireNonNull(method, "method");
97-
Object[] parameters = invocation.getParameters();
98+
Object[] parameters = this.invocation.getParameters();
9899
assert (method.getParameterCount() == getLength(parameters));
99-
return invocation;
100+
return this.invocation;
100101
}
101102

102103
private void logError(Throwable error) {
103104

104105
ServiceContext<S> context = this.stub.getContext();
105-
LOG.error("Failed to invoke service {}#{} at {}", context.getApi().getName(),
106-
this.stub.getInvocation().getMethod().getName(), context.getUrl(), error);
106+
String methodName = "undefined";
107+
if (this.invocation != null) {
108+
methodName = this.invocation.getMethod().getName();
109+
}
110+
LOG.error("Failed to invoke service {}#{} at {}", context.getApi().getName(), methodName, context.getUrl(), error);
107111
}
108112

109113
/**
110114
* @param <R> type of the return/result type.
111-
* @param invocation the {@link ServiceClientInvocation}.
115+
* @param serviceInvocation the {@link ServiceClientInvocation}.
112116
* @param resultHandler - see {@link #call(Object, Consumer)}.
113117
*/
114-
protected abstract <R> void doCall(ServiceClientInvocation<S> invocation, Consumer<R> resultHandler);
118+
protected abstract <R> void doCall(ServiceClientInvocation<S> serviceInvocation, Consumer<R> resultHandler);
115119

116120
private static int getLength(Object[] parameters) {
117121

0 commit comments

Comments
 (0)