halo-dev/halo · warning · IllegalArgumentException

Direction must not contain whitespace

Error message

Direction must not contain whitespace

What it means

Thrown as IllegalArgumentException by SortUtils.toDirection when the direction token contains a space. SortUtils parses sort strings like 'name,desc' and delegates the final token to Sort.Direction.fromString, but first rejects whitespace to avoid ambiguous/encoded tokens.

Source

Thrown at application/src/main/java/run/halo/app/infra/utils/SortUtils.java:38

        if (CollectionUtils.isEmpty(directionParams)) {
            return Sort.unsorted();
        }
        Sort.Order[] orders = new Sort.Order[directionParams.size()];
        for (int i = 0; i < directionParams.size(); i++) {
            String[] parts = directionParams.get(i).split(delimiter);
            if (parts.length == 1) {
                orders[i] = new Sort.Order(Sort.Direction.ASC, parts[0]);
            } else {
                orders[i] = new Sort.Order(toDirection(parts[1]), parts[0]);
            }
        }
        return Sort.by(orders);
    }

    private static Sort.Direction toDirection(String direction) {
        Assert.notNull(direction, "Direction must not be null");
        if (direction.contains(" ")) {
            throw new IllegalArgumentException("Direction must not contain whitespace");
        }
        return Sort.Direction.fromString(direction);
    }
}

View on GitHub (pinned to d2f5165f9c)

Solutions

  1. Trim each part before calling SortUtils: strip leading/trailing whitespace from the direction token.
  2. Send the sort param without spaces: 'name,desc'.
  3. Validate direction against ASC/DESC (case-insensitive) on the client before sending.
  4. Normalize the input by removing all internal whitespace in the direction segment.

Example fix

// before
SortUtils.resolve(sort); // sort = "name, desc"

// after
String normalized = Arrays.stream(sort.split(","))
    .map(String::trim).collect(Collectors.joining(","));
SortUtils.resolve(normalized); // "name,desc"
Defensive patterns

Strategy: validation

Validate before calling

String[] parts = sortParam.split(",");
String dir = parts.length > 1 ? parts[1].trim() : "";
if (dir.contains(" ")) {
    // strip whitespace or reject
    parts[1] = dir.replaceAll("\\s+", "");
}

Type guard

static boolean isValidDirectionToken(String s) {
    return s != null && !s.contains(" ")
        && (s.equalsIgnoreCase("asc") || s.equalsIgnoreCase("desc"));
}

Try / catch

try {
    Sort sort = SortUtils.resolve(rawSort);
} catch (IllegalArgumentException e) {
    // normalize and retry, or return 400
    String cleaned = Arrays.stream(rawSort.split(",")).map(String::trim).collect(Collectors.joining(","));
    sort = SortUtils.resolve(cleaned);
}

Prevention

When it happens

Trigger: A sort query parameter whose direction segment contains whitespace, e.g. 'name, desc' or 'name,DESC ' (space inside the direction token after splitting on ',').

Common situations: Frontends sending 'field, asc' with a space; URL-encoded spaces decoded into the direction; user-typed sort input not trimmed; copy-pasted sort strings with stray spaces.

Related errors


AI-assisted analysis of halo-dev/halo@d2f5165f9c (2026-08-14). Data as JSON: /api/errors/7d869a676836d1e4. Report an issue: GitHub.