or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

configuration.mderror-handling.mdhandlers.mdimporters.mdindex.mdparsing.mdresolvers.mdutilities.md

error-handling.mddocs/

0

# Error Handling

1

2

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

3

4

## Capabilities

5

6

### Error Codes

7

8

Standard error codes used throughout react-docgen for consistent error reporting.

9

10

```typescript { .api }

11

enum ERROR_CODES {

12

/** No suitable component definition found in the provided source code */

13

MISSING_DEFINITION = 'ERR_REACTDOCGEN_MISSING_DEFINITION',

14

/** Multiple exported component definitions found when expecting one */

15

MULTIPLE_DEFINITIONS = 'ERR_REACTDOCGEN_MULTIPLE_DEFINITIONS',

16

}

17

```

18

19

**Usage Example:**

20

21

```typescript

22

import { parse, ERROR_CODES } from "react-docgen";

23

24

try {

25

const docs = parse(sourceCode);

26

console.log(docs);

27

} catch (error) {

28

if (error.code === ERROR_CODES.MISSING_DEFINITION) {

29

console.log('No React components found in source code');

30

} else if (error.code === ERROR_CODES.MULTIPLE_DEFINITIONS) {

31

console.log('Multiple components found - consider using FindAllDefinitionsResolver');

32

}

33

}

34

```

35

36

### Error Types

37

38

**Missing Definition Error:**

39

- Occurs when no React component definitions are found in the source code

40

- Common causes: non-React code, components not exported, syntax errors

41

- Solutions: Verify component exports, check for compilation errors, use different resolvers

42

43

**Multiple Definitions Error:**

44

- Occurs when multiple component definitions are found but only one is expected

45

- Common with FindExportedDefinitionsResolver when limit is 1

46

- Solutions: Use FindAllDefinitionsResolver, increase limit, or filter components manually

47

48

### Error Handling Patterns

49

50

**Basic Error Handling:**

51

52

```typescript

53

import { parse } from "react-docgen";

54

55

function parseComponent(sourceCode: string) {

56

try {

57

return parse(sourceCode);

58

} catch (error) {

59

console.error('Failed to parse component:', error.message);

60

return [];

61

}

62

}

63

```

64

65

**Error Code Specific Handling:**

66

67

```typescript

68

import { parse, ERROR_CODES, builtinResolvers } from "react-docgen";

69

70

function parseComponentWithFallback(sourceCode: string) {

71

try {

72

return parse(sourceCode);

73

} catch (error) {

74

switch (error.code) {

75

case ERROR_CODES.MISSING_DEFINITION:

76

// Try with different resolver

77

return parse(sourceCode, {

78

resolver: new builtinResolvers.FindAllDefinitionsResolver()

79

});

80

81

case ERROR_CODES.MULTIPLE_DEFINITIONS:

82

// Accept multiple definitions

83

return parse(sourceCode, {

84

resolver: new builtinResolvers.FindExportedDefinitionsResolver({ limit: Infinity })

85

});

86

87

default:

88

throw error; // Re-throw unknown errors

89

}

90

}

91

}

92

```

93

94

**Validation Error Handling:**

95

96

```typescript

97

import { parse } from "react-docgen";

98

99

function validateAndParse(sourceCode: string, filename: string) {

100

if (!sourceCode || typeof sourceCode !== 'string') {

101

throw new Error('Source code must be a non-empty string');

102

}

103

104

try {

105

return parse(sourceCode, { filename });

106

} catch (error) {

107

// Add context to parsing errors

108

throw new Error(`Failed to parse ${filename}: ${error.message}`);

109

}

110

}

111

```

112

113

### Error Prevention

114

115

**Best Practices:**

116

- Always provide a filename for better error context

117

- Use appropriate resolvers for your use case

118

- Validate source code before parsing

119

- Handle import resolution errors with custom importers

120

- Use TypeScript for compile-time error detection

121

122

**Common Pitfalls:**

123

- Parsing non-React code without component definitions

124

- Using FindExportedDefinitionsResolver with low limits on multi-component files

125

- Not handling import resolution failures in complex component hierarchies

126

- Ignoring Babel parser errors due to unsupported syntax