timoninmaxim commented on code in PR #12732: URL: https://github.com/apache/ignite/pull/12732#discussion_r2811170320
########## modules/core/src/test/java/org/apache/ignite/internal/client/thin/CacheExceptionsTest.java: ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client.thin; + +import java.util.Objects; +import org.apache.ignite.cache.query.ScanQuery; +import org.apache.ignite.client.ClientCache; +import org.apache.ignite.client.ClientException; +import org.apache.ignite.client.ClientTransaction; +import org.apache.ignite.client.IgniteClient; +import org.apache.ignite.testframework.GridTestUtils; +import org.junit.Test; + +/** + * Thin client cache exceptions tests. + */ +public class CacheExceptionsTest extends AbstractThinClientTest { + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + super.beforeTestsStarted(); + + startGrid(0); + } + + /** + * Tests cache name in wrapped cache exception for server errors. + */ + @Test + public void testCacheExceptionWrapped() throws Exception { Review Comment: Never throws ########## modules/core/src/main/java/org/apache/ignite/internal/client/thin/TcpClientCache.java: ########## @@ -1754,4 +1759,136 @@ private boolean canBlockTx(boolean isGetOp, TransactionConcurrency concurrency, return true; } + + /** */ + private static ClientException convertException(Exception e, String cacheName) { + String msg = "Failed to perform cache operation [cacheName=" + cacheName + "]: " + e.getMessage(); + + // Exception can be ClientProtocolError - it's an internal exception, can't be thrown to user. + if (!(e instanceof ClientException)) + return new ClientException(msg, e); + else if (X.hasCause(e, ClientServerError.class)) // Wrap server errors. + return new ClientException(msg, e); + else // Don't wrap authentication, authorization, connection errors. + return (ClientException)e; + } + + /** */ + private static class ReliableChannelWrapper implements ReliableChannelEx { + /** */ + private final ReliableChannelImpl delegate; + + /** */ + private final String cacheName; + + /** */ + public ReliableChannelWrapper(ReliableChannelImpl delegate, String cacheName) { + this.delegate = delegate; + this.cacheName = cacheName; + } + + /** {@inheritDoc} */ + @Override public <T> T service( + ClientOperation op, + Consumer<PayloadOutputChannel> payloadWriter, + Function<PayloadInputChannel, T> payloadReader + ) throws ClientException, ClientError { + try { + return delegate.service(op, payloadWriter, payloadReader); + } + catch (ClientException e) { + throw convertException(e, cacheName); + } + } + + /** {@inheritDoc} */ + @Override public <T> T service( + ClientOperation op, + Consumer<PayloadOutputChannel> payloadWriter, + Function<PayloadInputChannel, T> payloadReader, + List<UUID> targetNodes + ) throws ClientException, ClientError { + try { + return delegate.service(op, payloadWriter, payloadReader, targetNodes); + } + catch (ClientException e) { + throw convertException(e, cacheName); + } + } + + /** {@inheritDoc} */ + @Override public <T> IgniteClientFuture<T> serviceAsync( + ClientOperation op, + Consumer<PayloadOutputChannel> payloadWriter, + Function<PayloadInputChannel, T> payloadReader + ) throws ClientException, ClientError { + CompletableFuture<T> fut = new CompletableFuture<>(); + + delegate.serviceAsync(op, payloadWriter, payloadReader).whenComplete((res, err) -> { + if (err != null) + fut.completeExceptionally(convertException((Exception)err, cacheName)); Review Comment: For the `service` call it wraps ClientExceptions only, and for serviceAsync - any Exception. Is it expected? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
