Skip to content

Commit

Permalink
🥅 andle MongoDB errors 168 (InvalidPipelineOperator), 17276 (Use of u…
Browse files Browse the repository at this point in the history
…ndefined variable), 17287+31254 (Cannot do exclusion in inclusion projection) to return status code 400
  • Loading branch information
ujibang committed Nov 3, 2023
1 parent d4dc551 commit 3a98f12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import java.util.Optional;

import com.mongodb.MongoException;
import io.undertow.server.HttpServerExchange;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.json.JsonParseException;
Expand All @@ -37,6 +35,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.undertow.server.HttpServerExchange;

/**
*
* @author Andrea Di Cesare {@literal <[email protected]>}
Expand Down Expand Up @@ -114,30 +114,19 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
return;
}

try {
data = dbs.getCollectionData(
Optional.ofNullable(request.getClientSession()),
request.rsOps(),
request.getDBName(),
request.getCollectionName(),
request.getPage(),
request.getPagesize(),
sort,
filter,
request.getHintDocument(),
request.getProjectionDocument(),
request.isCache());
} catch (MongoException me) {
if (me.getMessage().matches(".*Can't canonicalize query.*")) {
// error with the filter expression during query execution
LOGGER.debug("invalid filter expression {}", request.getFilter(), me);
MongoResponse.of(exchange).setInError(HttpStatus.SC_BAD_REQUEST, "wrong request, filter expression is invalid", me);
next(exchange);
return;
} else {
throw me;
}
}
data = dbs.getCollectionData(
Optional.ofNullable(request.getClientSession()),
request.rsOps(),
request.getDBName(),
request.getCollectionName(),
request.getPage(),
request.getPagesize(),
sort,
filter,
request.getHintDocument(),
request.getProjectionDocument(),
request.isCache());

}

if (exchange.isComplete()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public static int getHttpStatusFromErrorCode(int code) {
case 15998 -> HttpStatus.SC_BAD_REQUEST;
// 31249 Path collision
case 31249 -> HttpStatus.SC_BAD_REQUEST;
// 168 InvalidPipelineOperator
case 168 -> HttpStatus.SC_BAD_REQUEST;
// 17276 Use of undefined variable
case 17276 -> HttpStatus.SC_BAD_REQUEST;
// 1728, Can't canonicalize query: BadValue Projection cannot have a mix of inclusion and exclusion (old error message)
case 17287, 31254 -> HttpStatus.SC_BAD_REQUEST;
// 31254 Cannot do exclusion on field x in inclusion projection
default -> HttpStatus.SC_INTERNAL_SERVER_ERROR;
};
}
Expand All @@ -179,14 +186,14 @@ public static String getMessageFromMongoException(MongoException me) {
// Query failed with error code 51091 and error message 'Regular expression is invalid: unmatched parentheses'
// Query failed with error code 51108 with name 'Location51108' and error message 'invalid flag in regex options: z' on server 127.0.0.1:27017'
// Command failed with error 31249 (Location31249): 'Path collision at page.children.children.displayname remaining portion children.children.displayname' on server...
case 2, 51091, 51108, 31249 -> {
case 2, 51091, 51108, 31249, 168, 17276, 31254 -> {
var msg = me.getMessage();

var b = msg.indexOf(": '");
var e = msg.indexOf("' on server");

if (b >= 0 && e >= 0) {
yield "Invalid filter: " + msg.substring(b+3, e).strip();
yield "Invalid query parameter: " + msg.substring(b+3, e).strip();
} else {
yield msg;
}
Expand Down

0 comments on commit 3a98f12

Please sign in to comment.