alibaba/Sentinel · error · IllegalArgumentException

Config encoder cannot be null

Error message

Config encoder cannot be null

What it means

Thrown by the FileWritableDataSource constructor when the Converter<T, String> config encoder is null. The encoder turns rule entities into the text written to disk, so a null encoder would NPE on the first write(); Sentinel validates it in the constructor instead.

Source

Thrown at sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/FileWritableDataSource.java:56

    private final File file;
    private final Charset charset;

    private final Lock lock = new ReentrantLock(true);

    public FileWritableDataSource(String filePath, Converter<T, String> configEncoder) {
        this(new File(filePath), configEncoder);
    }

    public FileWritableDataSource(File file, Converter<T, String> configEncoder) {
        this(file, configEncoder, DEFAULT_CHARSET);
    }

    public FileWritableDataSource(File file, Converter<T, String> configEncoder, Charset charset) {
        if (file == null || file.isDirectory()) {
            throw new IllegalArgumentException("Bad file");
        }
        if (configEncoder == null) {
            throw new IllegalArgumentException("Config encoder cannot be null");
        }
        if (charset == null) {
            throw new IllegalArgumentException("Charset cannot be null");
        }
        this.configEncoder = configEncoder;
        this.file = file;
        this.charset = charset;
    }

    @Override
    public void write(T value) throws Exception {
        lock.lock();
        try {
            String convertResult = configEncoder.convert(value);
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                byte[] bytesArray = convertResult.getBytes(charset);

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Supply a real encoder, e.g. rules -> JSON.toJSONString(rules) (FastJSON) or an equivalent Jackson/Gson lambda.
  2. In Spring, make the encoder a bean and inject it into the data source bean definition; check startup logs for missing-bean errors.
  3. Add a unit test that constructs the data source with the production wiring to catch null wiring early.

Example fix

// before
new FileWritableDataSource<>(file, null);

// after
new FileWritableDataSource<>(file, rules -> JSON.toJSONString(rules));
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(encoder, "config encoder must be provided");
new FileWritableDataSource<>(file, encoder, charset);

Try / catch

try {
    new FileWritableDataSource<>(file, encoder, charset);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("FileWritableDataSource misconfigured (encoder/file/charset)", e);
}

Prevention

When it happens

Trigger: new FileWritableDataSource(file, null) or passing an encoder field that was never initialized (e.g. forgotten @Autowired/bean wiring in Spring).

Common situations: Spring bean where the encoder dependency is missing or the @Bean method returns null; refactoring that drops the encoder argument; ordering issue where the data source bean is built before the converter bean.

Related errors


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