Skip to content

Commit 3b342f4

Browse files
committed
feat: Add code to errors to easily detect them
1 parent 0c2891d commit 3b342f4

File tree

6 files changed

+51
-17
lines changed

6 files changed

+51
-17
lines changed

packages/react-docgen/src/__tests__/main-test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { builtinHandlers, parse } from '../main.js';
2-
import { ERROR_MISSING_DEFINITION } from '../parse.js';
1+
import { builtinHandlers, parse, ERROR_CODES } from '../main.js';
32
import { describe, expect, test } from 'vitest';
43

54
// TODO make fixtures out of them?
@@ -189,7 +188,11 @@ describe('main', () => {
189188
export default NotAComponent;
190189
`;
191190

192-
expect(() => parse(source)).toThrowError(ERROR_MISSING_DEFINITION);
191+
expect(() => parse(source)).toThrowError(
192+
expect.objectContaining({
193+
code: ERROR_CODES.MISSING_DEFINITION,
194+
}),
195+
);
193196
});
194197
});
195198
});

packages/react-docgen/src/__tests__/parse-test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type { ObjectExpression } from '@babel/types';
22
import fs from 'fs';
33
import { directory as tempDirectory } from 'tempy';
44
import { parse as testParse, noopImporter } from '../../tests/utils';
5-
import parse, { ERROR_MISSING_DEFINITION } from '../parse.js';
5+
import parse from '../parse.js';
66
import { describe, expect, test, vi } from 'vitest';
7+
import { ERROR_CODES } from '../error';
78

89
describe('parse', () => {
910
test('allows custom component definition resolvers', () => {
@@ -32,7 +33,11 @@ describe('parse', () => {
3233
importer: noopImporter,
3334
babelOptions: {},
3435
}),
35-
).toThrowError(ERROR_MISSING_DEFINITION);
36+
).toThrowError(
37+
expect.objectContaining({
38+
code: ERROR_CODES.MISSING_DEFINITION,
39+
}),
40+
);
3641
expect(resolver).toBeCalled();
3742

3843
expect(() =>
@@ -42,7 +47,11 @@ describe('parse', () => {
4247
importer: noopImporter,
4348
babelOptions: {},
4449
}),
45-
).toThrowError(ERROR_MISSING_DEFINITION);
50+
).toThrowError(
51+
expect.objectContaining({
52+
code: ERROR_CODES.MISSING_DEFINITION,
53+
}),
54+
);
4655
expect(resolver).toBeCalled();
4756
});
4857

@@ -99,6 +108,10 @@ describe('parse', () => {
99108
},
100109
},
101110
}),
102-
).toThrowError(ERROR_MISSING_DEFINITION);
111+
).toThrowError(
112+
expect.objectContaining({
113+
code: ERROR_CODES.MISSING_DEFINITION,
114+
}),
115+
);
103116
});
104117
});

packages/react-docgen/src/error.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export enum ERROR_CODES {
2+
MISSING_DEFINITION = 'ERR_REACTDOCGEN_MISSING_DEFINITION',
3+
MULTIPLE_DEFINITIONS = 'ERR_REACTDOCGEN_MULTIPLE_DEFINITIONS',
4+
}
5+
6+
const messages = new Map([
7+
[ERROR_CODES.MISSING_DEFINITION, 'No suitable component definition found.'],
8+
[
9+
ERROR_CODES.MULTIPLE_DEFINITIONS,
10+
'Multiple exported component definitions found.',
11+
],
12+
]);
13+
14+
export class ReactDocgenError extends Error {
15+
code: string | undefined;
16+
constructor(code: ERROR_CODES) {
17+
super(messages.get(code));
18+
19+
this.code = code;
20+
}
21+
}

packages/react-docgen/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { Handler } from './handlers/index.js';
1414
import type FileState from './FileState.js';
1515
import type { Config } from './config.js';
1616
import { createConfig, defaultHandlers } from './config.js';
17+
import { ERROR_CODES } from './error.js';
1718

1819
const builtinImporters = {
1920
fsImporter,
@@ -63,6 +64,7 @@ export {
6364
makeFsImporter,
6465
defaultParse as parse,
6566
utils,
67+
ERROR_CODES,
6668
};
6769

6870
export type { Importer, Handler, Resolver, FileState, Config, Documentation };

packages/react-docgen/src/parse.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import type { Handler } from './handlers/index.js';
77
import type { ComponentNode } from './resolver/index.js';
88
import FileState from './FileState.js';
99
import type { InternalConfig } from './config.js';
10-
11-
const ERROR_MISSING_DEFINITION = 'No suitable component definition found.';
10+
import { ERROR_CODES, ReactDocgenError } from './error.js';
1211

1312
function executeHandlers(
1413
handlers: Handler[],
@@ -58,10 +57,8 @@ export default function parse(
5857
const componentDefinitions = resolver(fileState);
5958

6059
if (componentDefinitions.length === 0) {
61-
throw new Error(ERROR_MISSING_DEFINITION);
60+
throw new ReactDocgenError(ERROR_CODES.MISSING_DEFINITION);
6261
}
6362

6463
return executeHandlers(handlers, componentDefinitions);
6564
}
66-
67-
export { ERROR_MISSING_DEFINITION };

packages/react-docgen/src/resolver/findExportedComponentDefinition.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import type FileState from '../FileState.js';
1414
import resolveComponentDefinition, {
1515
isComponentDefinition,
1616
} from '../utils/resolveComponentDefinition.js';
17-
18-
const ERROR_MULTIPLE_DEFINITIONS =
19-
'Multiple exported component definitions found.';
17+
import { ERROR_CODES, ReactDocgenError } from '../error.js';
2018

2119
interface TraverseState {
2220
foundDefinition: NodePath<ComponentNode> | null;
@@ -48,7 +46,7 @@ function exportDeclaration(
4846
}
4947
if (definitions.length > 1 || state.foundDefinition) {
5048
// If a file exports multiple components, ... complain!
51-
throw new Error(ERROR_MULTIPLE_DEFINITIONS);
49+
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
5250
}
5351
const definition = resolveComponentDefinition(definitions[0]);
5452

@@ -84,7 +82,7 @@ const explodedVisitors = visitors.explode<TraverseState>({
8482
}
8583
if (state.foundDefinition) {
8684
// If a file exports multiple components, ... complain!
87-
throw new Error(ERROR_MULTIPLE_DEFINITIONS);
85+
throw new ReactDocgenError(ERROR_CODES.MULTIPLE_DEFINITIONS);
8886
}
8987
const definition = resolveComponentDefinition(resolvedPath);
9088

0 commit comments

Comments
 (0)