moodle/moodle · error · moodle_exception
h5pinvalidurl
h5pinvalidurl
Error message
h5pinvalidurl
What it means
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.
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
Example fix
// before
$player = new \core_h5p\player($url, $config);
// after
if (empty($url)) {
throw new \moodle_exception('h5pfilemissing', 'mod_h5pactivity');
}
$player = new \core_h5p\player($url, $config); Defensive patterns
Strategy: validation
Validate before calling
if (empty($url) || !is_string($url)) { throw new \coding_exception('H5P player requires a pluginfile URL'); } Type guard
function isValidH5pUrl($url): bool {
return is_string($url) && $url !== '' && filter_var($url, FILTER_VALIDATE_URL) !== false;
} Try / catch
try {
$player = new \core_h5p\player($url, $config);
} catch (\moodle_exception $e) {
if ($e->errorcode === 'h5pinvalidurl') { /* render fallback UI */ }
throw $e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- $errors[0]->code
- Missing 'machinename' or 'id' in librarydata parameter
- missingfield
- notvalidpackage
- Reactivity event required
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/925000c012c2bee9.
Report an issue: GitHub.
Appendix: source
Thrown at public/h5p/classes/player.php:118
/**
* @var bool Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions.
*/
private $preventredirect;
/**
* Inits the H5P player for rendering the content.
*
* @param string $url Local URL of the H5P file to display.
* @param \stdClass $config Configuration for H5P buttons.
* @param bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
* @param string $component optional moodle component to sent xAPI tracking
* @param bool $skipcapcheck Whether capabilities should be checked or not to get the pluginfile URL because sometimes they
* might be controlled before calling this method.
*/
public function __construct(string $url, \stdClass $config, bool $preventredirect = true, string $component = '',
bool $skipcapcheck = false) {
if (empty($url)) {
throw new \moodle_exception('h5pinvalidurl', 'core_h5p');
}
$this->url = new \moodle_url($url);
$this->preventredirect = $preventredirect;
$this->factory = new \core_h5p\factory();
$this->messages = new \stdClass();
$this->component = $component;
// Create \core_h5p\core instance.
$this->core = $this->factory->get_core();
// Get the H5P identifier linked to this URL.
list($file, $this->h5pid) = api::create_content_from_pluginfile_url(
$url,
$config,
$this->factory,View on GitHub (pinned to 48ea8c3322)