Skip to content

Commit 22c5b99

Browse files
committed
Move error handling and span lifecycle (span.error(), span.end()) from Reactor's doOnError/doFinally operators into the async callback, before emitting the result to the subscriber.
1 parent e2cf66c commit 22c5b99

1 file changed

Lines changed: 26 additions & 24 deletions

File tree

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/OperationExecutorImpl.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,21 @@ public <T> Mono<T> execute(final ReadOperation<?, T> operation, final ReadPrefer
108108
try {
109109
binding.release();
110110
} finally {
111+
if (t != null) {
112+
Throwable exceptionToHandle = t instanceof MongoException
113+
? OperationHelper.unwrap((MongoException) t) : t;
114+
labelException(session, exceptionToHandle);
115+
unpinServerAddressOnTransientTransactionError(session, exceptionToHandle);
116+
if (span != null) {
117+
span.error(t);
118+
}
119+
}
120+
if (span != null) {
121+
span.end();
122+
}
111123
sinkToCallback(sink).onResult(result, t);
112124
}
113-
})).doOnError((t) -> {
114-
Throwable exceptionToHandle = t instanceof MongoException ? OperationHelper.unwrap((MongoException) t) : t;
115-
labelException(session, exceptionToHandle);
116-
unpinServerAddressOnTransientTransactionError(session, exceptionToHandle);
117-
if (span != null) {
118-
span.error(t);
119-
}
120-
}).doFinally(signal -> {
121-
if (span != null) {
122-
span.end();
123-
}
124-
});
125+
}));
125126
}
126127
}).subscribe(subscriber)
127128
);
@@ -153,20 +154,21 @@ public <T> Mono<T> execute(final WriteOperation<T> operation, final ReadConcern
153154
try {
154155
binding.release();
155156
} finally {
157+
if (t != null) {
158+
Throwable exceptionToHandle = t instanceof MongoException
159+
? OperationHelper.unwrap((MongoException) t) : t;
160+
labelException(session, exceptionToHandle);
161+
unpinServerAddressOnTransientTransactionError(session, exceptionToHandle);
162+
if (span != null) {
163+
span.error(t);
164+
}
165+
}
166+
if (span != null) {
167+
span.end();
168+
}
156169
sinkToCallback(sink).onResult(result, t);
157170
}
158-
})).doOnError((t) -> {
159-
Throwable exceptionToHandle = t instanceof MongoException ? OperationHelper.unwrap((MongoException) t) : t;
160-
labelException(session, exceptionToHandle);
161-
unpinServerAddressOnTransientTransactionError(session, exceptionToHandle);
162-
if (span != null) {
163-
span.error(t);
164-
}
165-
}).doFinally(signal -> {
166-
if (span != null) {
167-
span.end();
168-
}
169-
});
171+
}));
170172
}
171173
).subscribe(subscriber)
172174
);

0 commit comments

Comments
 (0)