DIYgod/RSSHub · error · Error
Unknown handled type: ${element.type}, ${JSON.stringify(elem
Error message
Unknown handled type: ${element.type}, ${JSON.stringify(element)} What it means
Thrown by the GoCN renderHTML utility when processing a JSON document representing rich text content. Each element is looked up in the elementMap (code_block, a, blockquote, br, h1-h6, img, p, strong, ol, ul, li, lic). If no handler exists AND the element has no 'text' property as a fallback, this error fires with the full JSON of the unrecognized element.
Source
Thrown at lib/routes/gocn/utils.ts:31
p: (element) => `<p>${renderHTML(element.children)}</p>`,
strong: (element) => `<strong>${renderHTML(element.children)}</strong>`,
ol: (element) => `<ol>${renderHTML(element.children)}</ol>`,
ul: (element) => `<ul>${renderHTML(element.children)}</ul>`,
li: (element) => `<li>${renderHTML(element.children)}</li>`,
lic: (element) => element.children[0].text,
};
function renderHTML(json) {
return json
.map((element) => {
const handler = elementMap[element.type];
if (handler) {
return handler(element);
}
if (Object.hasOwn(element, 'text')) {
return element.text;
}
throw new Error(`Unknown handled type: ${element.type}, ${JSON.stringify(element)}`);
})
.join('');
}
export { renderHTML };
View on GitHub (pinned to bed535e087)
Solutions
- Inspect the element JSON in the error message to identify the new type
- Add a handler for the new type to the elementMap object in utils.ts
- As a temporary fix, add the new type to a fallback that extracts text or returns an empty string
- If the element is decorative (e.g., 'hr'), add a simple handler like hr: () => '<hr>'
Example fix
// before (only catches, then throws)
if (Object.hasOwn(element, 'text')) {
return element.text;
}
throw new Error(`Unknown handled type: ${element.type}, ${JSON.stringify(element)}`);
// after (add new handlers + graceful fallback)
// 1. Add to elementMap:
// hr: () => '<hr>',
// em: (element) => `<em>${renderHTML(element.children)}</em>`,
// table: (element) => `<table>${renderHTML(element.children)}</table>`,
// 2. Use a non-throwing fallback:
// return element.text || ''; // instead of throwing Defensive patterns
Strategy: type-guard
Validate before calling
// Validate element types before rendering
const KNOWN_TYPES = new Set([...Object.keys(elementMap), 'text']);
function validateElements(json: any[]): void {
for (const el of json) {
if (!KNOWN_TYPES.has(el.type) && !Object.hasOwn(el, 'text')) {
console.warn(`Unknown element type: ${el.type}`, el);
}
if (el.children && Array.isArray(el.children)) {
validateElements(el.children);
}
}
} Type guard
function isKnownElement(element: unknown): boolean {
if (typeof element !== 'object' || element === null) return false;
const el = element as Record<string, unknown>;
return Object.hasOwn(elementMap, el.type as string) || Object.hasOwn(el, 'text');
} Prevention
- Add new element types to elementMap proactively when the upstream schema changes
- Use a non-throwing fallback (return element.text || '') for unknown types in production
- Log unknown types for investigation instead of crashing the entire feed
- Write tests that cover diverse content tree shapes
When it happens
Trigger: The GoCN API returns a JSON content tree containing an element with a 'type' value not present in the elementMap (e.g., 'table', 'hr', 'em', 'italic', 'inline_code') and the element does not have a top-level 'text' property. This is reached during renderHTML(json) which maps over every element in the array.
Common situations: The upstream content source (GoCN) added a new rich-text element type (e.g., tables, horizontal rules, inline formatting); the content includes nested structures that the flat elementMap doesn't cover; a schema version change introduced new element types.
Related errors
- Unknown type: ${x}
- 无法获取提交数据
- No content found. The page structure might have changed.
- Unknown host: ${url.host}
- Data source ID not found
AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12).
Data as JSON: /api/errors/3e16f0256a27a8c9.
Report an issue: GitHub.