didi/DoKit · error · IllegalArgumentException

input must be shorter than or equal to the number of…

Error message

input must be shorter than or equal to the number of spaces: " + size

What it means

Thrown by DoKit's StringUtil.padLeft(input, size) when the input string is longer than the target padded length. padLeft prepends spaces until the string reaches exactly `size` characters; if the input already exceeds that length there is no valid way to pad it, so the method rejects the call with IllegalArgumentException.

Solutions

  1. Truncate or verify the input before calling: if (input.length() > size) input = input.substring(0, size);
  2. Reconsider whether a hard fail is desired — for display purposes a truncate-and-warn policy is usually friendlier than an exception
  3. Pass a size that is always >= the maximum possible input length

Example fix

// before
String s = StringUtil.padLeft(longUrl, 8); // throws if longUrl > 8 chars

// after
String s = longUrl.length() > 8 ? longUrl.substring(0, 8) : StringUtil.padLeft(longUrl, 8);
Defensive patterns

Strategy: validation

Validate before calling

if (input != null && input.length() <= size) { String s = StringUtil.padLeft(input, size); } else { /* truncate or reject */ }

Prevention

When it happens

Trigger: Calling StringUtil.padLeft("hello", 3) or any variant where input.length() > size, e.g. while formatting log/network trace columns in the loginfo kit with a string longer than the configured column width.

Common situations: Fixed-width formatting of dynamic content (URLs, headers, tag strings) where the developer assumed the input would fit; column width configured smaller than the longest expected value.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/5de2eb8db8b22c8b. Report an issue: GitHub.

Appendix: source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/loginfo/util/StringUtil.java:23

import java.util.ArrayList;
import java.util.List;

/**
 * @author nolan
 */
public class StringUtil {

    /**
     * Pad the specified number of spaces to the input string to make it that length
     *
     * @param input
     * @param size
     * @return
     */
    public static String padLeft(String input, int size) {

        if (input.length() > size) {
            throw new IllegalArgumentException("input must be shorter than or equal to the number of spaces: " + size);
        }

        StringBuilder sb = new StringBuilder();
        for (int i = input.length(); i < size; i++) {
            sb.append(" ");
        }
        return sb.append(input).toString();
    }

    /**
     * same as the String.split(), except it doesn't use regexes, so it's faster.
     *
     * @param str       - the string to split up
     * @param delimiter the delimiter
     * @return the split string
     */
    public static String[] split(String str, String delimiter) {
        List<String> result = new ArrayList<>();

View on GitHub (pinned to 626827cddb)