w7corp/easywechat · error · InvalidArgumentException

"%s" cannot be empty.\r\n

Error message

"%s" cannot be empty.\r\n

What it means

Thrown by EasyWeChat\Kernel\Config::checkMissingKeys() from the Config constructor when one or more required keys are absent from the config array. Each application subclass declares its own requiredKeys: OfficialAccount needs app_id; OpenPlatform needs app_id, secret, aes_key; Work needs corp_id, secret, token, aes_key; Pay needs mch_id, secret_key, private_key, certificate. The message lists exactly which keys are missing (note the check is key existence via Arr::has, not non-empty values).

Source

Thrown at src/Kernel/Config.php:128

    /**
     * @throws InvalidArgumentException
     */
    public function checkMissingKeys(): bool
    {
        if (empty($this->requiredKeys)) {
            return true;
        }

        $missingKeys = [];

        foreach ($this->requiredKeys as $key) {
            if (! $this->has($key)) {
                $missingKeys[] = $key;
            }
        }

        if (! empty($missingKeys)) {
            throw new InvalidArgumentException(sprintf("\"%s\" cannot be empty.\r\n", implode(',', $missingKeys)));
        }

        return true;
    }
}

View on GitHub (pinned to f0cf0a8b83)

Solutions

  1. Read the exception message: it names the exact missing keys — add each one to the config array passed to the app factory
  2. Check spelling/case of keys against the app's Config subclass (snake_case: app_id, corp_id, mch_id, secret_key, private_key, certificate, aes_key, token, secret)
  3. For Pay\erchant: make sure all four of mch_id, secret_key, private_key (path string to apiclient_key.pem) and certificate are provided
  4. Assert required env vars exist before building config (e.g. fail fast when WECHAT_APP_ID is unset instead of silently omitting the key)

Example fix

// before (OfficialAccount)
$config = ['app_id' => getenv('WX_APP_ID'), 'secret' => '...'];
// Fatal: "app_id" cannot be empty when env var unset (key absent)

// after
$config = array_filter([
    'app_id' => getenv('WX_APP_ID'),
    'secret' => getenv('WX_SECRET'),
]);
if (!isset($config['app_id'])) {
    throw new RuntimeException('WX_APP_ID env var is not set');
}
Defensive patterns

Strategy: validation

Validate before calling

// Before creating the app
$required = ['app_id']; // OfficialAccount; Work: corp_id, secret, token, aes_key; Pay: mch_id, secret_key, private_key, certificate
$missing = array_diff($required, array_keys(array_filter($config, fn ($v) => $v !== null && $v !== '')));
if ($missing) {
    throw new InvalidArgumentException('Missing WeChat config keys: '.implode(', ', $missing));
}
$app = OfficialAccount::create($config); // or new Config($config)

Try / catch

try {
    $app = OfficialAccount::create($config);
} catch (\EasyWeChat\Kernel\Exceptions\InvalidArgumentException $e) {
    // message names the missing keys, e.g. "app_id" cannot be empty.
    log_config_error($e->getMessage());
    fail_deploy_fast();
}

Prevention

When it happens

Trigger: Instantiating an app whose config array omits a declared key, e.g. creating a Pay client with only mch_id and secret_key (private_key/certificate missing), or an OfficialAccount with ['appid' => 'wx...'] instead of ['app_id' => 'wx...']. Also triggered when config is built from env vars and a missing env var makes the key never get set.

Common situations: Typos in key names (appid vs app_id, secret_key vs secret), missing .env entries in a new environment (staging/CI), assuming Pay only needs merchant credentials, forgetting that Work requires token and aes_key even when you only plan API calls.

Related errors


AI-assisted analysis of w7corp/easywechat@f0cf0a8b83 (2026-08-21). Data as JSON: /api/errors/23eedc12163e0768. Report an issue: GitHub.