theonedev/onedev · error · ExplicitException

Cannot order by field:

Error message

Cannot order by field: 

What it means

After parsing the criteria, parse() iterates the query's 'order by' clauses and looks each field name up in CodeComment.SORT_FIELDS. If the field is not sortable for code comments, ExplicitException "Cannot order by field: <name>" is thrown. Only fields registered in SORT_FIELDS can be used in 'order by'.

Source

Thrown at server-core/src/main/java/io/onedev/server/search/entity/codecomment/CodeCommentQuery.java:256

						return new AndCriteria<>(childCriterias);
					}

					@Override
					public Criteria<CodeComment> visitNotCriteria(NotCriteriaContext ctx) {
						return new NotCriteria<>(visit(ctx.criteria()));
					}

				}.visit(criteriaContext);
			} else {
				commentCriteria = null;
			}

			List<EntitySort> commentSorts = new ArrayList<>();
			for (OrderContext order : queryContext.order()) {
				var fieldName = getValue(order.Quoted().getText());
				var sortField = SORT_FIELDS.get(fieldName);
				if (sortField == null)
					throw new ExplicitException("Cannot order by field: " + fieldName);

				EntitySort commentSort = new EntitySort();
				commentSort.setField(fieldName);
				if (order.direction != null) {
					if (order.direction.getText().equals("desc"))
						commentSort.setDirection(DESCENDING);
					else
						commentSort.setDirection(ASCENDING);
				} else {
					commentSort.setDirection(sortField.getDefaultDirection());
				}
				commentSorts.add(commentSort);
			}

			return new CodeCommentQuery(commentCriteria, commentSorts);
		} else {
			return new CodeCommentQuery();
		}

View on GitHub (pinned to d44925c47c)

Solutions

  1. Use only sortable fields in 'order by', e.g. "order by \"Create Date\"" or "order by \"Last Activity Date\"".
  2. Check CodeComment.SORT_FIELDS (or the UI's order-by dropdown) for the exact allowed field names.
  3. Remove the order clause if default ordering is acceptable.
  4. Catch ExplicitException around parse() to give the user a friendly message listing valid sort fields.

Example fix

// before
var query = CodeCommentQuery.parse(project, "Resolved order by \"Content\"", true); // not sortable
// after
var query = CodeCommentQuery.parse(project, "Resolved order by \"Last Activity Date\" desc", true);
Defensive patterns

Strategy: validation

Validate before calling

// only sortable fields may appear in 'order by'
static boolean isSortableField(String fieldName) {
    return fieldName.equals("Create Date") || fieldName.equals("Last Activity Date");
}
// check every field after 'order by' in the query text before calling parse()

Try / catch

try {
    var query = CodeCommentQuery.parse(project, queryString, true);
} catch (ExplicitException e) {
    if (e.getMessage().startsWith("Cannot order by field"))
        showUserError(e.getMessage() + " — sortable fields: Create Date, Last Activity Date");
    else
        throw e;
}

Prevention

When it happens

Trigger: A query with an order clause on a non-sortable field, e.g. "order by content" or any field name not present in SORT_FIELDS (typically 'create date' and 'last activity date' are sortable).

Common situations: Copy-pasting an order clause from another entity's query (issue/build queries have different sortable fields); typos in the field name; a field that became non-sortable after a OneDev upgrade.

Understand the failure class

Background: "Invalid query parameter" / "Failed to parse value of ...": fixing bad query string parameters across APIs — this error's family across 36 libraries.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/be6dee04b21ebdc1. Report an issue: GitHub.