moodle/moodle · error · InvalidArgumentException

file does not exist or is unreadable

Error message

file does not exist or is unreadable

What it means

UserRefreshCredentials::__construct accepts the key as a JSON string or a file path. If a string is passed that is treated as a path but no file exists there (file_exists() fails), it throws InvalidArgumentException('file does not exist or is unreadable').

Solutions

  1. Verify the file path exists and is readable: run is_file() and is_readable() on it before constructing.
  2. Use an absolute path or __DIR__-based path to avoid working-directory issues.
  3. If you have inline JSON, pass a decoded array instead of a string.

Example fix

// before
$creds = new UserRefreshCredentials($scope, 'key.json'); // relative path may not resolve
// after
$path = __DIR__ . '/key.json';
if (!is_file($path) || !is_readable($path)) { throw new RuntimeException("missing key file: $path"); }
$creds = new UserRefreshCredentials($scope, $path);
Defensive patterns

Strategy: validation

Validate before calling

$path = 'key.json';
if (!is_string($path) || !is_file($path) || !is_readable($path)) {
    throw new RuntimeException("Credential file missing or unreadable: $path");
}

Type guard

function isValidKeyPath(mixed $jsonKey): bool {
    return is_string($jsonKey)
        ? is_file($jsonKey) && is_readable($jsonKey)
        : is_array($jsonKey);
}

Try / catch

try {
    $creds = new UserRefreshCredentials($scope, $jsonKey);
} catch (\InvalidArgumentException $e) {
    error_log('Credential key problem: ' . $e->getMessage());
    throw new RuntimeException('Provide an existing key file path or a decoded JSON array', 0, $e);
}

Prevention

When it happens

Trigger: Calling new UserRefreshCredentials($scope, '/path/to/key.json') where the path does not exist or is not readable by the PHP process; note that valid inline JSON containing spaces/braces is NOT accepted as a path here, so passing raw JSON as a string can also hit this check.

Common situations: Typo in the credentials path, relative path resolved against the wrong working directory, file deleted in deployment, or confusion between passing a file path vs. a JSON string.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/0ca93b575ee5f8eb. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/google2-auth/src/Credentials/UserRefreshCredentials.php:82

    private bool $isIdTokenRequest = false;

    /**
     * Create a new UserRefreshCredentials.
     *
     * @param string|string[]|null $scope the scope of the access request, expressed
     *   either as an Array or as a space-delimited String.
     * @param string|array<mixed> $jsonKey JSON credential file path or JSON credentials
     *   as an associative array
     * @param string|null $targetAudience The audience for the ID token.
     */
    public function __construct(
        $scope,
        $jsonKey,
        ?string $targetAudience = null
    ) {
        if (is_string($jsonKey)) {
            if (!file_exists($jsonKey)) {
                throw new InvalidArgumentException('file does not exist or is unreadable');
            }
            $json = file_get_contents($jsonKey);
            if (!$jsonKey = json_decode((string) $json, true)) {
                throw new LogicException('invalid json for auth config');
            }
        }
        if (!array_key_exists('client_id', $jsonKey)) {
            throw new InvalidArgumentException(
                'json key is missing the client_id field'
            );
        }
        if (!array_key_exists('client_secret', $jsonKey)) {
            throw new InvalidArgumentException(
                'json key is missing the client_secret field'
            );
        }
        if (!array_key_exists('refresh_token', $jsonKey)) {
            throw new InvalidArgumentException(

View on GitHub (pinned to 48ea8c3322)