moodle/moodle · error · moodle_exception

apierror

Error message

apierror

What it means

Thrown by block_tag_youtube::get_categories() when the YouTube API response cannot be turned into a category mapping — i.e. the call to the YouTube video-category endpoint failed or returned no usable data. The block requires the category list to render its video widget, so it aborts with a block-specific 'apierror'.

Solutions

  1. Set/verify a valid YouTube Data API v3 key in the block settings (Admin > Plugins > Blocks > Tag YouTube).
  2. Check the API quota in Google Cloud Console; wait for reset or raise the quota.
  3. Confirm outbound HTTPS to www.googleapis.com works from the server.
  4. Verify the plugin is a current version compatible with YouTube Data API v3.
  5. Add caching or fallback rendering so a transient API failure does not break the page.

Example fix

// before
$cats = $this->get_categories(); // throws on any API hiccup
// after
try {
    $cats = $this->get_categories();
} catch (\moodle_exception $e) {
    debugging($e->getMessage());
    $cats = []; // render block without categories
}
Defensive patterns

Strategy: try-catch

Validate before calling

$apikey = get_config('block_tag_youtube', 'apikey'); if (empty($apikey)) { /* block cannot work */ }

Try / catch

try {
    $categories = $block->get_categories();
} catch (\moodle_exception $e) {
    if ($e->errorcode === 'apierror') { debugging('YouTube API failure: ' . $e->getMessage()); $categories = []; }
    else { throw $e; }
}

Prevention

When it happens

Trigger: YouTube Data API request fails: invalid/expired API key, quota exceeded, network failure, YouTube blocking the region, or the response lacking the expected category items.

Common situations: YouTube API key missing or revoked; Google deprecated/enforced quota limits; server blocked from googleapis.com; plugin not updated after a YouTube API change (legacy API removed).

Related errors


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

Appendix: source

Thrown at public/blocks/tag_youtube/block_tag_youtube.php:347

            // Return an array with the relevant categories.
            return array_reduce($categoryitems, function($categories, $category) use ($categorytranslations) {
                $categoryid = $category['id'];
                $categoryname = $category['snippet']['title'];
                // Videos can be associated with this category.
                if ($category['snippet']['assignable']) {
                    // If the category name can be mapped with a translation, add it to the categories array.
                    if (array_key_exists($categoryname, $categorytranslations)) {
                        $categories[$categoryid] = $categorytranslations[$categoryname];
                    } else { // Otherwise, display the untranslated category name and show a debugging message.
                        $categories[$categoryid] = $categoryname;
                        debugging("The category '{$categoryname}' does not have a translatable language string.");
                    }
                }
                return $categories;
            }, []);
        } else {
            throw new \moodle_exception('apierror', 'block_tag_youtube');
        }
    }

    /**
     * Method that provides mapping between the video category names and their translations.
     *
     * @return array The array that maps the video category names with their translations
     */
    private function category_map_translation() {
        return [
            'Film & Animation' => get_string('filmsanimation', 'block_tag_youtube'),
            'Autos & Vehicles' => get_string('autosvehicles', 'block_tag_youtube'),
            'Music' => get_string('music', 'block_tag_youtube'),
            'Pets & Animals' => get_string('petsanimals', 'block_tag_youtube'),
            'Sports' => get_string('sports', 'block_tag_youtube'),
            'Travel & Events' => get_string('travel', 'block_tag_youtube'),
            'Gaming' => get_string('gadgetsgames', 'block_tag_youtube'),
            'People & Blogs' => get_string('peopleblogs', 'block_tag_youtube'),

View on GitHub (pinned to 48ea8c3322)