Copilot commented on code in PR #4369: URL: https://github.com/apache/solr/pull/4369#discussion_r3146978061
########## solr/core/src/test/org/apache/solr/handler/component/QueryComponentFieldExistsQueryErrorHandlingTest.java: ########## @@ -0,0 +1,106 @@ +/* + * 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.solr.handler.component; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.search.QueryCommand; +import org.apache.solr.search.SolrIndexSearcher; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +public class QueryComponentFieldExistsQueryErrorHandlingTest extends SolrTestCaseJ4 { + + @BeforeClass + public static void beforeClass() { + assumeWorkingMockito(); + } + + @Test + public void mapsFieldExistsQueryIllegalStateExceptionToBadRequest() throws Exception { + SolrIndexSearcher searcher = Mockito.mock(SolrIndexSearcher.class); + IllegalStateException fieldExistsError = + new IllegalStateException("FieldExistsQuery requires norms, docValues, or vectors"); + Mockito.when(searcher.search(Mockito.any(QueryCommand.class))).thenThrow(fieldExistsError); + + InvocationTargetException thrown = + expectThrows( + InvocationTargetException.class, + () -> invokeDoProcessUngroupedSearch(newResponseBuilder(searcher), new QueryCommand())); + + assertTrue(thrown.getCause() instanceof SolrException); + SolrException solrException = (SolrException) thrown.getCause(); + assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, solrException.code()); + assertTrue(solrException.getMessage().startsWith("FieldExistsQuery requires")); + } + + @Test + public void mapsNestedFieldExistsQueryIllegalStateExceptionToBadRequest() throws Exception { + SolrIndexSearcher searcher = Mockito.mock(SolrIndexSearcher.class); + IllegalStateException rootCause = + new IllegalStateException("FieldExistsQuery requires norms, docValues, or vectors"); + IllegalStateException wrapped = new IllegalStateException("outer", rootCause); + Mockito.when(searcher.search(Mockito.any(QueryCommand.class))).thenThrow(wrapped); + + InvocationTargetException thrown = + expectThrows( + InvocationTargetException.class, + () -> invokeDoProcessUngroupedSearch(newResponseBuilder(searcher), new QueryCommand())); + + assertTrue(thrown.getCause() instanceof SolrException); + SolrException solrException = (SolrException) thrown.getCause(); + assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, solrException.code()); + assertTrue(solrException.getMessage().startsWith("FieldExistsQuery requires")); + } + + @Test + public void leavesOtherIllegalStateExceptionsUnchanged() throws Exception { + SolrIndexSearcher searcher = Mockito.mock(SolrIndexSearcher.class); + IllegalStateException nonFieldExistsError = + new IllegalStateException("not a field exists query"); + Mockito.when(searcher.search(Mockito.any(QueryCommand.class))).thenThrow(nonFieldExistsError); + + InvocationTargetException thrown = + expectThrows( + InvocationTargetException.class, + () -> invokeDoProcessUngroupedSearch(newResponseBuilder(searcher), new QueryCommand())); + + assertSame(nonFieldExistsError, thrown.getCause()); + } + + private static void invokeDoProcessUngroupedSearch(ResponseBuilder rb, QueryCommand cmd) + throws Exception { + Method method = + QueryComponent.class.getDeclaredMethod( + "doProcessUngroupedSearch", ResponseBuilder.class, QueryCommand.class); + method.setAccessible(true); + method.invoke(new QueryComponent(), rb, cmd); Review Comment: This test uses reflection with `setAccessible(true)` to reach a private method, but it isn’t annotated with `@SuppressForbidden`. The build enables forbidden-apis’ `jdk-reflection` signatures (see `gradle/validation/forbidden-apis.gradle`), and similar reflection-based tests in this package suppress it (e.g., `AsyncTrackerSemaphoreLeakTest.java:236`). Add an appropriate `@SuppressForbidden` annotation (method or class) with a brief reason to avoid forbidden-apis failures. ########## solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java: ########## @@ -435,6 +436,9 @@ public void process(ResponseBuilder rb) throws IOException { } } catch (SyntaxError e) { throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); + } catch (IllegalStateException e) { + maybeThrowBadRequestForFieldExistsQueryFailure(e); + throw e; Review Comment: The PR changes error mapping for the grouped execution path (the `groupingSpec != null` branch), but the added tests only exercise the ungrouped `doProcessUngroupedSearch` path. Please add a test that triggers the grouped path and asserts the same BAD_REQUEST mapping, to prevent regressions specific to grouped execution. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
