Skip to content

Commit 9179308

Browse files
committed
Throw ProtocolException when QueryType is unknown (neo4j#1193)
This update ensures that `ProtocolException` is thrown when server provides an unknown `QueryType`.
1 parent 27f38b5 commit 9179308

File tree

5 files changed

+66
-16
lines changed

5 files changed

+66
-16
lines changed

driver/src/main/java/org/neo4j/driver/internal/handlers/LegacyPullAllResponseHandler.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.neo4j.driver.Query;
3131
import org.neo4j.driver.Record;
3232
import org.neo4j.driver.Value;
33+
import org.neo4j.driver.exceptions.Neo4jException;
3334
import org.neo4j.driver.internal.InternalRecord;
3435
import org.neo4j.driver.internal.messaging.request.PullAllMessage;
3536
import org.neo4j.driver.internal.spi.Connection;
@@ -92,12 +93,27 @@ public boolean canManageAutoRead()
9293
public synchronized void onSuccess( Map<String,Value> metadata )
9394
{
9495
finished = true;
95-
summary = extractResultSummary( metadata );
96+
Neo4jException exception = null;
97+
try
98+
{
99+
summary = extractResultSummary( metadata );
100+
}
101+
catch ( Neo4jException e )
102+
{
103+
exception = e;
104+
}
96105

97-
completionListener.afterSuccess( metadata );
106+
if ( exception == null )
107+
{
108+
completionListener.afterSuccess( metadata );
98109

99-
completeRecordFuture( null );
100-
completeFailureFuture( null );
110+
completeRecordFuture( null );
111+
completeFailureFuture( null );
112+
}
113+
else
114+
{
115+
onFailure( exception );
116+
}
101117
}
102118

103119
@Override
@@ -335,7 +351,7 @@ private boolean completeFailureFuture( Throwable error )
335351
private ResultSummary extractResultSummary( Map<String,Value> metadata )
336352
{
337353
long resultAvailableAfter = runResponseHandler.resultAvailableAfter();
338-
return metadataExtractor.extractSummary(query, connection, resultAvailableAfter, metadata );
354+
return metadataExtractor.extractSummary( query, connection, resultAvailableAfter, metadata );
339355
}
340356

341357
private void enableAutoRead()

driver/src/main/java/org/neo4j/driver/internal/handlers/pulln/BasicPullResponseHandler.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.neo4j.driver.Query;
2525
import org.neo4j.driver.Record;
2626
import org.neo4j.driver.Value;
27+
import org.neo4j.driver.exceptions.Neo4jException;
2728
import org.neo4j.driver.internal.InternalRecord;
2829
import org.neo4j.driver.internal.handlers.PullResponseCompletionListener;
2930
import org.neo4j.driver.internal.handlers.RunResponseHandler;
@@ -112,9 +113,18 @@ protected void completeWithFailure( Throwable error )
112113
protected void completeWithSuccess( Map<String,Value> metadata )
113114
{
114115
completionListener.afterSuccess( metadata );
115-
ResultSummary summary = extractResultSummary( metadata );
116-
117-
complete( summary, null );
116+
ResultSummary summary;
117+
Neo4jException exception = null;
118+
try
119+
{
120+
summary = extractResultSummary( metadata );
121+
}
122+
catch ( Neo4jException e )
123+
{
124+
summary = extractResultSummary( emptyMap() );
125+
exception = e;
126+
}
127+
complete( summary, exception );
118128
}
119129

120130
protected void successHasMore()

driver/src/main/java/org/neo4j/driver/internal/util/MetadataExtractor.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.function.Function;
2425

2526
import org.neo4j.driver.Bookmark;
2627
import org.neo4j.driver.Query;
2728
import org.neo4j.driver.Value;
29+
import org.neo4j.driver.exceptions.ProtocolException;
2830
import org.neo4j.driver.exceptions.UntrustedServerException;
2931
import org.neo4j.driver.internal.InternalBookmark;
3032
import org.neo4j.driver.internal.spi.Connection;
@@ -49,6 +51,9 @@
4951
public class MetadataExtractor
5052
{
5153
public static final int ABSENT_QUERY_ID = -1;
54+
private static final String UNEXPECTED_TYPE_MSG_FMT = "Unexpected query type '%s', consider updating the driver";
55+
private static final Function<String,ProtocolException> UNEXPECTED_TYPE_EXCEPTION_SUPPLIER =
56+
( type ) -> new ProtocolException( String.format( UNEXPECTED_TYPE_MSG_FMT, type ) );
5257
private final String resultAvailableAfterMetadataKey;
5358
private final String resultConsumedAfterMetadataKey;
5459

@@ -98,14 +103,14 @@ public long extractResultAvailableAfter( Map<String,Value> metadata )
98103
return -1;
99104
}
100105

101-
public ResultSummary extractSummary(Query query, Connection connection, long resultAvailableAfter, Map<String,Value> metadata )
106+
public ResultSummary extractSummary( Query query, Connection connection, long resultAvailableAfter, Map<String,Value> metadata )
102107
{
103108
ServerInfo serverInfo =
104109
new InternalServerInfo( connection.serverAgent(), connection.serverAddress(), connection.serverVersion(), connection.protocol().version() );
105110
DatabaseInfo dbInfo = extractDatabaseInfo( metadata );
106-
return new InternalResultSummary(query, serverInfo, dbInfo, extractQueryType( metadata ), extractCounters( metadata ), extractPlan( metadata ),
107-
extractProfiledPlan( metadata ), extractNotifications( metadata ), resultAvailableAfter,
108-
extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
111+
return new InternalResultSummary( query, serverInfo, dbInfo, extractQueryType( metadata ), extractCounters( metadata ), extractPlan( metadata ),
112+
extractProfiledPlan( metadata ), extractNotifications( metadata ), resultAvailableAfter,
113+
extractResultConsumedAfter( metadata, resultConsumedAfterMetadataKey ) );
109114
}
110115

111116
public static DatabaseInfo extractDatabaseInfo( Map<String,Value> metadata )
@@ -160,7 +165,7 @@ private static QueryType extractQueryType( Map<String,Value> metadata )
160165
Value typeValue = metadata.get( "type" );
161166
if ( typeValue != null )
162167
{
163-
return QueryType.fromCode( typeValue.asString() );
168+
return QueryType.fromCode( typeValue.asString(), UNEXPECTED_TYPE_EXCEPTION_SUPPLIER );
164169
}
165170
return null;
166171
}

driver/src/main/java/org/neo4j/driver/summary/QueryType.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
*/
1919
package org.neo4j.driver.summary;
2020

21+
import java.util.function.Function;
22+
2123
import org.neo4j.driver.exceptions.ClientException;
24+
import org.neo4j.driver.exceptions.Neo4jException;
2225

2326
/**
2427
* The type of query executed.
28+
*
2529
* @since 1.0
2630
*/
2731
public enum QueryType
@@ -31,7 +35,16 @@ public enum QueryType
3135
WRITE_ONLY,
3236
SCHEMA_WRITE;
3337

34-
public static QueryType fromCode(String type )
38+
private static final String UNEXPECTED_TYPE_MSG_FMT = "Unknown query type: `%s`.";
39+
private static final Function<String,ClientException> UNEXPECTED_TYPE_EXCEPTION_SUPPLIER =
40+
( type ) -> new ClientException( String.format( UNEXPECTED_TYPE_MSG_FMT, type ) );
41+
42+
public static QueryType fromCode( String type )
43+
{
44+
return fromCode( type, UNEXPECTED_TYPE_EXCEPTION_SUPPLIER );
45+
}
46+
47+
public static QueryType fromCode( String type, Function<String,? extends Neo4jException> exceptionFunction )
3548
{
3649
switch ( type )
3750
{
@@ -44,7 +57,14 @@ public static QueryType fromCode(String type )
4457
case "s":
4558
return QueryType.SCHEMA_WRITE;
4659
default:
47-
throw new ClientException( "Unknown query type: `" + type + "`." );
60+
if ( exceptionFunction != null )
61+
{
62+
throw exceptionFunction.apply( type );
63+
}
64+
else
65+
{
66+
return null;
67+
}
4868
}
4969
}
5070
}

testkit-backend/src/main/java/neo4j/org/testkit/backend/messages/requests/StartTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class StartTest implements TestkitRequest
4141

4242
static
4343
{
44-
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_invalid_query_type$", "Does not report type exception" );
4544
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_no_notifications$", "An empty list is returned when there are no notifications" );
4645
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_no_notification_info$", "An empty list is returned when there are no notifications" );
4746
COMMON_SKIP_PATTERN_TO_REASON.put( "^.*\\.test_notifications_without_position$", "Null value is provided when position is absent" );

0 commit comments

Comments
 (0)