CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-docgen

A library to extract information from React components for documentation generation.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

error-handling.mddocs/

Error Handling

Error handling system for parsing failures and validation issues in react-docgen.

Capabilities

Error Codes

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');
  }
}

Error Types

Missing Definition Error:

  • Occurs when no React component definitions are found in the source code
  • Common causes: non-React code, components not exported, syntax errors
  • Solutions: Verify component exports, check for compilation errors, use different resolvers

Multiple Definitions Error:

  • Occurs when multiple component definitions are found but only one is expected
  • Common with FindExportedDefinitionsResolver when limit is 1
  • Solutions: Use FindAllDefinitionsResolver, increase limit, or filter components manually

Error Handling Patterns

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}`);
  }
}

Error Prevention

Best Practices:

  • Always provide a filename for better error context
  • Use appropriate resolvers for your use case
  • Validate source code before parsing
  • Handle import resolution errors with custom importers
  • Use TypeScript for compile-time error detection

Common Pitfalls:

  • Parsing non-React code without component definitions
  • Using FindExportedDefinitionsResolver with low limits on multi-component files
  • Not handling import resolution failures in complex component hierarchies
  • Ignoring Babel parser errors due to unsupported syntax

Install with Tessl CLI

npx tessl i tessl/npm-react-docgen

docs

configuration.md

error-handling.md

handlers.md

importers.md

index.md

parsing.md

resolvers.md

utilities.md

tile.json