moodle/moodle · error · moodle_exception

$errors[0]->code

Error message

$errors[0]->code

What it means

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.

Solutions

  1. 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).
  2. Check on-screen H5P notifications, which carry the human-readable $error->message for the same failure.
  3. Install missing H5P libraries or update existing ones, then re-upload the package.

Example fix

// before
// thrown: moodle_exception with code 'missing-h5p-json'
// after
// add h5p.json at the zip root:
// {"title":"Demo","mainLibrary":"H5P.GreetingCard","embedTypes":["iframe"]}
Defensive patterns

Strategy: validation

Validate before calling

$validator = new \\H5P\\Validator(/* ... */);
$errors = $validator->getErrors('error');
if (count($errors) === 1) {
    // Surface $errors[0]->code / ->message to the user before upload.
}

Try / catch

try {
    $content->import_file($file);
} catch (\moodle_exception $e) {
    // Map known H5P codes (e.g. 'missing-h5p-json') to actionable user messages.
    $msg = get_string($e->errorcode, 'h5p', null, true) ?? $e->errorcode;
    notification::error($msg);
}

Prevention

When it happens

Trigger: 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.

Common situations: Packages missing required manifest files, wrong archive layout, or referencing libraries not installed on the site.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at public/contentbank/contenttype/h5p/classes/content.php:104

     * @throws \moodle_exception If file operations fail
     * @param \stored_file $file File to store in the content file area.
     * @return \stored_file|null the stored content file or null if the file is discarted.
     */
    public function import_file(\stored_file $file): ?\stored_file {
        // The H5P content can be only deployed if the author of the .h5p file can update libraries or if all the
        // content-type libraries exist, to avoid users without the h5p:updatelibraries capability upload malicious content.
        $onlyupdatelibs = !\core_h5p\helper::can_update_library($file);

        if (!\core_h5p\api::is_valid_package($file, $onlyupdatelibs)) {
            $factory = new factory();
            $errors = $factory->get_framework()->getMessages('error');
            foreach ($errors as $error) {
                notification::error($error->message);
            }
            if (empty($errors) || count($errors) > 1) {
                throw new \moodle_exception('notvalidpackage', 'h5p');
            }
            throw new \moodle_exception($errors[0]->code, 'h5p');
        }
        return parent::import_file($file);
    }
}

View on GitHub (pinned to 48ea8c3322)