alibaba/Sentinel · error · IllegalArgumentException

Request cannot be null

Error message

Request cannot be null

What it means

HttpCommandUtils.getTarget extracts the routing target (handler name) from a CommandRequest's metadata. It throws IllegalArgumentException("Request cannot be null") as a programming-error guard when callers pass a null request, since it immediately dereferences request.getMetadata(). This is a fail-fast precondition, not an environmental condition.

Source

Thrown at sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/HttpCommandUtils.java:31

 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.transport.util;

import com.alibaba.csp.sentinel.command.CommandRequest;

/**
 * Util class for HTTP command center.
 *
 * @author Eric Zhao
 */
public final class HttpCommandUtils {

    public static final String REQUEST_TARGET = "command-target";

    public static String getTarget(CommandRequest request) {
        if (request == null) {
            throw new IllegalArgumentException("Request cannot be null");
        }
        return request.getMetadata().get(REQUEST_TARGET);
    }

    private HttpCommandUtils() {}
}

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Ensure a CommandRequest is always constructed (new CommandRequest()) before calling getTarget
  2. Add an explicit null check at the call site with a meaningful message or skip the call when no request exists
  3. In parsers, fail the connection early instead of propagating null downstream

Example fix

// before
CommandRequest req = parseRequest(channel); // may return null on bad input
String target = HttpCommandUtils.getTarget(req);

// after
CommandRequest req = parseRequest(channel);
if (req == null) {
    writeErrorResponse(400, "Bad request", ctx);
    return;
}
String target = HttpCommandUtils.getTarget(req);
Defensive patterns

Strategy: type-guard

Validate before calling

if (request == null) {
    throw new IllegalStateException("parser produced no CommandRequest");
}
String target = HttpCommandUtils.getTarget(request);

Type guard

if (request instanceof CommandRequest cr && cr.getMetadata() != null) { ... }

Prevention

When it happens

Trigger: HttpCommandUtils.getTarget(null), e.g. custom command-center or handler code where the request variable was never assigned or a conditional parse path returned null.

Common situations: Writing a custom transport or command handler that reimplements request dispatch; refactoring that moves request construction behind a conditional; unit tests calling helpers without building a request.

Related errors


AI-assisted analysis of alibaba/Sentinel@a3f40ba8e9 (2026-08-14). Data as JSON: /api/errors/7f8a76c47210068d. Report an issue: GitHub.