{"record":{"id":"d991eb4d14e8283a","repo":"getgrav/grav","slug":"bad-data-d991eb","errorCode":null,"errorMessage":"Bad Data","messagePattern":"Bad Data","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Framework/File/DataFile.php","lineNumber":49,"sourceCode":"     */\n    public function __construct($filepath, FileFormatterInterface $formatter)\n    {\n        parent::__construct($filepath);\n\n        $this->formatter = $formatter;\n    }\n\n    /**\n     * {@inheritdoc}\n     * @see FileInterface::load()\n     */\n    public function load()\n    {\n        $raw = parent::load();\n\n        try {\n            if (!is_string($raw)) {\n                throw new RuntimeException('Bad Data');\n            }\n\n            return $this->formatter->decode($raw);\n        } catch (RuntimeException $e) {\n            throw new RuntimeException(sprintf(\"Failed to load file '%s': %s\", $this->getFilePath(), $e->getMessage()), $e->getCode(), $e);\n        }\n    }\n\n    /**\n     * {@inheritdoc}\n     * @see FileInterface::save()\n     */\n    public function save($data): void\n    {\n        if (is_string($data)) {\n            // Make sure that the string is valid data.\n            try {\n                $this->formatter->decode($data);","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/File/DataFile.php#L31-L67","documentation":"DataFile::load() reads raw bytes via parent::load() (file_get_contents) and requires a string; anything else throws 'Bad Data'. file_get_contents returns false exactly when the read failed — file missing, unreadable by the PHP user, or blocked by open_basedir — so despite the message the real cause is a failed read, not corrupt content. DataFile immediately re-wraps it as \"Failed to load file '...': Bad Data\" with this as the previous exception.","triggerScenarios":"DataFile::load() on a file that does not exist yet (first run before save, deleted data), is not readable by the PHP user (wrong ownership/mode), or sits outside open_basedir.","commonSituations":"Plugins reading user/data files before ever creating them; files created by a root CLI run so the web user cannot read; migrations copying files without preserving modes; shared hosting with path restrictions.","solutions":["Check existence and readability before loading: is_file($path) && is_readable($path); treat 'missing' as empty state and initialize defaults instead of calling load().","Fix ownership/mode of the file for the PHP user (chown/chmod).","Confirm the path is absolute, correctly spelled, and within open_basedir.","If the file was half-written by an earlier crash, restore from backup or delete it so defaults regenerate."],"exampleFix":"// before\n$data = $dataFile->load(); // RuntimeException: Bad Data\n\n// after\n$path = $dataFile->getFilePath();\n$data = (is_file($path) && is_readable($path)) ? $dataFile->load() : $defaults;","handlingStrategy":"validation","validationCode":"$path = $dataFile->getFilePath();\nif (!is_file($path) || !is_readable($path)) {\n    // load() would throw 'Bad Data'; treat as empty state and use defaults\n    return $defaults;\n}","typeGuard":null,"tryCatchPattern":"try {\n    $data = $dataFile->load();\n} catch (\\RuntimeException $e) {\n    // message contains path + 'Bad Data': file missing/unreadable\n    $data = $defaults;\n}","preventionTips":["Use a create-if-missing pattern: save defaults on first run so the file exists.","Keep file ownership aligned with the PHP user across CLI/web usage.","Check file presence after restores and migrations before enabling readers."],"tags":["php","grav","filesystem","file-read","data-file"],"backgroundTag":"file-read-failed","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}