{"record":{"id":"6691f06879b52167","repo":"moodle/moodle","slug":"errors-0-code","errorCode":null,"errorMessage":"$errors[0]->code","messagePattern":"\\$errors\\[0\\]->code","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/contentbank/contenttype/h5p/classes/content.php","lineNumber":104,"sourceCode":"     * @throws \\moodle_exception If file operations fail\n     * @param \\stored_file $file File to store in the content file area.\n     * @return \\stored_file|null the stored content file or null if the file is discarted.\n     */\n    public function import_file(\\stored_file $file): ?\\stored_file {\n        // The H5P content can be only deployed if the author of the .h5p file can update libraries or if all the\n        // content-type libraries exist, to avoid users without the h5p:updatelibraries capability upload malicious content.\n        $onlyupdatelibs = !\\core_h5p\\helper::can_update_library($file);\n\n        if (!\\core_h5p\\api::is_valid_package($file, $onlyupdatelibs)) {\n            $factory = new factory();\n            $errors = $factory->get_framework()->getMessages('error');\n            foreach ($errors as $error) {\n                notification::error($error->message);\n            }\n            if (empty($errors) || count($errors) > 1) {\n                throw new \\moodle_exception('notvalidpackage', 'h5p');\n            }\n            throw new \\moodle_exception($errors[0]->code, 'h5p');\n        }\n        return parent::import_file($file);\n    }\n}\n","sourceCodeStart":86,"sourceCodeEnd":109,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/contentbank/contenttype/h5p/classes/content.php#L86-L109","documentation":"Same import_file() failure path as notvalidpackage, but taken when the H5P framework reports exactly one error: the exception message is the raw framework error code ($errors[0]->code, e.g. 'invalid-content-folder', 'missing-h5p-json') thrown as a moodle_exception in the h5p component. The developer sees the machine error code of the single H5P validation failure.","triggerScenarios":"Uploading an .h5p package that fails exactly one H5P framework validation check, e.g. missing h5p.json, invalid content folder, or an invalid library dependency — each maps to a distinct $error->code.","commonSituations":"Packages missing required manifest files, wrong archive layout, or referencing libraries not installed on the site.","solutions":["Look up the thrown code in H5P's error-code list and fix that specific defect in the package (e.g. add h5p.json, fix content folder).","Check on-screen H5P notifications, which carry the human-readable $error->message for the same failure.","Install missing H5P libraries or update existing ones, then re-upload the package."],"exampleFix":"// before\n// thrown: moodle_exception with code 'missing-h5p-json'\n// after\n// add h5p.json at the zip root:\n// {\"title\":\"Demo\",\"mainLibrary\":\"H5P.GreetingCard\",\"embedTypes\":[\"iframe\"]}","handlingStrategy":"validation","validationCode":"$validator = new \\\\H5P\\\\Validator(/* ... */);\n$errors = $validator->getErrors('error');\nif (count($errors) === 1) {\n    // Surface $errors[0]->code / ->message to the user before upload.\n}","typeGuard":null,"tryCatchPattern":"try {\n    $content->import_file($file);\n} catch (\\moodle_exception $e) {\n    // Map known H5P codes (e.g. 'missing-h5p-json') to actionable user messages.\n    $msg = get_string($e->errorcode, 'h5p', null, true) ?? $e->errorcode;\n    notification::error($msg);\n}","preventionTips":["Read the on-screen H5P notifications, which carry the human-readable message matching the thrown code.","Cross-reference the code against the H5P validator error-code documentation.","Pre-validate packages with the H5P CLI/validator before importing in production."],"tags":["moodle","h5p","contentbank","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}