A library to extract information from React components for documentation generation.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Error handling system for parsing failures and validation issues in react-docgen.
Standard error codes used throughout react-docgen for consistent error reporting.
enum ERROR_CODES {
/** No suitable component definition found in the provided source code */
MISSING_DEFINITION = 'ERR_REACTDOCGEN_MISSING_DEFINITION',
/** Multiple exported component definitions found when expecting one */
MULTIPLE_DEFINITIONS = 'ERR_REACTDOCGEN_MULTIPLE_DEFINITIONS',
}Usage Example:
import { parse, ERROR_CODES } from "react-docgen";
try {
const docs = parse(sourceCode);
console.log(docs);
} catch (error) {
if (error.code === ERROR_CODES.MISSING_DEFINITION) {
console.log('No React components found in source code');
} else if (error.code === ERROR_CODES.MULTIPLE_DEFINITIONS) {
console.log('Multiple components found - consider using FindAllDefinitionsResolver');
}
}Missing Definition Error:
Multiple Definitions Error:
Basic Error Handling:
import { parse } from "react-docgen";
function parseComponent(sourceCode: string) {
try {
return parse(sourceCode);
} catch (error) {
console.error('Failed to parse component:', error.message);
return [];
}
}Error Code Specific Handling:
import { parse, ERROR_CODES, builtinResolvers } from "react-docgen";
function parseComponentWithFallback(sourceCode: string) {
try {
return parse(sourceCode);
} catch (error) {
switch (error.code) {
case ERROR_CODES.MISSING_DEFINITION:
// Try with different resolver
return parse(sourceCode, {
resolver: new builtinResolvers.FindAllDefinitionsResolver()
});
case ERROR_CODES.MULTIPLE_DEFINITIONS:
// Accept multiple definitions
return parse(sourceCode, {
resolver: new builtinResolvers.FindExportedDefinitionsResolver({ limit: Infinity })
});
default:
throw error; // Re-throw unknown errors
}
}
}Validation Error Handling:
import { parse } from "react-docgen";
function validateAndParse(sourceCode: string, filename: string) {
if (!sourceCode || typeof sourceCode !== 'string') {
throw new Error('Source code must be a non-empty string');
}
try {
return parse(sourceCode, { filename });
} catch (error) {
// Add context to parsing errors
throw new Error(`Failed to parse ${filename}: ${error.message}`);
}
}Best Practices:
Common Pitfalls:
Install with Tessl CLI
npx tessl i tessl/npm-react-docgen