{"record":{"id":"925000c012c2bee9","repo":"moodle/moodle","slug":"h5pinvalidurl","errorCode":"h5pinvalidurl","errorMessage":"h5pinvalidurl","messagePattern":"h5pinvalidurl","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/h5p/classes/player.php","lineNumber":118,"sourceCode":"    /**\n     * @var bool Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions.\n     */\n    private $preventredirect;\n\n    /**\n     * Inits the H5P player for rendering the content.\n     *\n     * @param string $url Local URL of the H5P file to display.\n     * @param \\stdClass $config Configuration for H5P buttons.\n     * @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions\n     * @param string $component optional moodle component to sent xAPI tracking\n     * @param bool $skipcapcheck Whether capabilities should be checked or not to get the pluginfile URL because sometimes they\n     *     might be controlled before calling this method.\n     */\n    public function __construct(string $url, \\stdClass $config, bool $preventredirect = true, string $component = '',\n            bool $skipcapcheck = false) {\n        if (empty($url)) {\n            throw new \\moodle_exception('h5pinvalidurl', 'core_h5p');\n        }\n        $this->url = new \\moodle_url($url);\n        $this->preventredirect = $preventredirect;\n\n        $this->factory = new \\core_h5p\\factory();\n\n        $this->messages = new \\stdClass();\n\n        $this->component = $component;\n\n        // Create \\core_h5p\\core instance.\n        $this->core = $this->factory->get_core();\n\n        // Get the H5P identifier linked to this URL.\n        list($file, $this->h5pid) = api::create_content_from_pluginfile_url(\n            $url,\n            $config,\n            $this->factory,","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/h5p/classes/player.php#L100-L136","documentation":"Thrown by the H5P player constructor when it must abort instead of redirecting. The player normally forwards the browser to an error/choose-file page; when the caller sets $preventredirect true (CLI scripts, RSS feeds, web services — contexts that cannot redirect), the constructor throws this exception instead of redirecting. It fires when the given $url is not a valid local H5P file URL, e.g. empty, malformed, or pointing outside the allowed H5P file area. Callers in non-redirect contexts should catch it and report the invalid URL to the user or log.","triggerScenarios":"Constructing new \\core_h5p\\player($url, $config, ...) with an empty string (or '' after trimming) for $url — e.g. the pluginfile URL was never built, a module config field is blank, or a variable containing the URL was not initialized.","commonSituations":"Custom activity modules embedding H5P where the h5pfile URL comes from user data; code paths that call the player before the content file URL is generated; template/debug scripts invoked with missing parameters.","solutions":["Ensure a valid moodle_url/pluginfile URL string is passed; build it via moodle_url::make_pluginfile_url(...) from the context and file record","Check that the field/setting supplying the URL is populated before constructing the player","Guard the call site: skip player creation when the URL is empty and show a friendly message instead"],"exampleFix":"// before\n$player = new \\core_h5p\\player($url, $config);\n// after\nif (empty($url)) {\n    throw new \\moodle_exception('h5pfilemissing', 'mod_h5pactivity');\n}\n$player = new \\core_h5p\\player($url, $config);","handlingStrategy":"validation","validationCode":"if (empty($url) || !is_string($url)) { throw new \\coding_exception('H5P player requires a pluginfile URL'); }","typeGuard":"function isValidH5pUrl($url): bool {\n    return is_string($url) && $url !== '' && filter_var($url, FILTER_VALIDATE_URL) !== false;\n}","tryCatchPattern":"try {\n    $player = new \\core_h5p\\player($url, $config);\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'h5pinvalidurl') { /* render fallback UI */ }\n    throw $e;\n}","preventionTips":["Build the URL via moodle_url::make_pluginfile_url rather than string concatenation","Check the source field (module config, file record) is populated before instantiating the player","Add pre-construction assertions in dev environments"],"tags":["moodle","h5p","validation","constructor"],"backgroundTag":"empty-required-field","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"}