Skip to content

Commit eeaff7c

Browse files
committed
Distinguish request and validation errors and export them
1 parent 856ec93 commit eeaff7c

File tree

8 files changed

+326
-77
lines changed

8 files changed

+326
-77
lines changed

src/compileOperation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { namedTypes, builders } from 'ast-types';
33
import { Compiler } from './compiler';
44
import { OpenAPIOperation } from './types';
55
import { compileValueSchema } from './compileValueSchema';
6-
import { ValidationErrorIdentifier, buildValidationError } from './error';
6+
import { ValidationErrorIdentifier, buildRequestError } from './error';
77
import { OpenAPIParsedPath, getPathParamIndex, openapiPathToRegex } from './paths';
88

99
/**
@@ -127,7 +127,7 @@ export function compileOperation(
127127
builders.identifier('undefined'),
128128
),
129129
builders.blockStatement([
130-
builders.returnStatement(buildValidationError('body is required')),
130+
builders.returnStatement(buildRequestError(400, 'body is required')),
131131
]),
132132
),
133133
);
@@ -185,7 +185,7 @@ export function compileOperation(
185185
builders.identifier('undefined'),
186186
),
187187
builders.blockStatement([
188-
builders.returnStatement(buildValidationError('body is not allowed')),
188+
builders.returnStatement(buildRequestError(400, 'body is not allowed')),
189189
]),
190190
),
191191
);

src/compilePath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { namedTypes, builders } from 'ast-types';
33
import { Compiler } from './compiler';
44
import { OpenAPIPath } from './types';
55
import { compileOperation } from './compileOperation';
6-
import { buildValidationError } from './error';
6+
import { buildRequestError } from './error';
77
import { OpenAPIParsedPath } from './paths';
88

99
/**
@@ -52,7 +52,7 @@ export function compilePath(
5252
);
5353
});
5454

55-
nodes.push(builders.returnStatement(buildValidationError('method not supported')));
55+
nodes.push(builders.returnStatement(buildRequestError(405, 'method not supported')));
5656

5757
return builders.functionDeclaration(
5858
functionId,

src/compileValidateRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { namedTypes, builders } from 'ast-types';
33
import { Compiler } from './compiler';
44
import { OpenAPISpec } from './types';
55
import { compilePath } from './compilePath';
6-
import { buildValidationError } from './error';
6+
import { buildRequestError } from './error';
77
import { OpenAPIParsedPath, openapiPathToRegex } from './paths';
88

99
const COMMENT = `*
@@ -58,7 +58,7 @@ export function compileValidateRequest(compiler: Compiler, spec: OpenAPISpec) {
5858
});
5959

6060
// Otherwise, return an error
61-
nodes.push(builders.returnStatement(buildValidationError('no operation match path')));
61+
nodes.push(builders.returnStatement(buildRequestError(404, 'no operation match path')));
6262

6363
return [
6464
{

src/compiler.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import escodegen from 'escodegen';
22
import { namedTypes, builders } from 'ast-types';
3-
import { ValidationErrorClass, ValidationErrorIdentifier } from './error';
3+
import { RequestErrorClass, ValidationErrorClass, ValidationErrorIdentifier } from './error';
44
import { OpenAPIRef, OpenAPISpec } from './types';
55
import { compileValueSchema } from './compileValueSchema';
66
import { hash } from './hash';
@@ -25,7 +25,11 @@ export class Compiler {
2525
| namedTypes.FunctionDeclaration
2626
| namedTypes.ClassDeclaration
2727
| namedTypes.VariableDeclaration
28-
)[] = [ValidationErrorClass];
28+
| namedTypes.ExportNamedDeclaration
29+
)[] = [
30+
builders.exportNamedDeclaration(RequestErrorClass),
31+
builders.exportNamedDeclaration(ValidationErrorClass),
32+
];
2933

3034
/** Map of hashes already processed */
3135
private processedHashes: Set<string> = new Set();
@@ -116,12 +120,12 @@ export class Compiler {
116120
/**
117121
* Build the AST from the entire spec.
118122
*/
119-
public build() {
123+
public indexAllComponents() {
120124
// Index all the schema components.
121-
// const schemas = this.input.components?.schemas ?? {};
122-
// Object.values(schemas).forEach((schema) => {
123-
// compileValueSchema(this, schema);
124-
// });
125+
const schemas = this.input.components?.schemas ?? {};
126+
Object.values(schemas).forEach((schema) => {
127+
compileValueSchema(this, schema);
128+
});
125129
}
126130

127131
/**

src/error.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
import { namedTypes, builders } from 'ast-types';
22

3-
export const ValidationErrorIdentifier = builders.identifier('ValidationError');
3+
export const RequestErrorIdentifier = builders.identifier('RequestError');
4+
export const RequestErrorClass = builders.classDeclaration.from({
5+
id: RequestErrorIdentifier,
6+
superClass: builders.identifier('Error'),
7+
body: builders.classBody([
8+
builders.methodDefinition(
9+
'constructor',
10+
builders.identifier('constructor'),
11+
builders.functionExpression(
12+
null,
13+
[builders.identifier('code'), builders.identifier('message')],
14+
builders.blockStatement([
15+
builders.expressionStatement(
16+
builders.callExpression(builders.super(), [builders.identifier('message')]),
17+
),
18+
builders.expressionStatement(
19+
builders.assignmentExpression(
20+
'=',
21+
builders.memberExpression(
22+
builders.thisExpression(),
23+
builders.identifier('code'),
24+
),
25+
builders.identifier('code'),
26+
),
27+
),
28+
]),
29+
),
30+
),
31+
]),
32+
});
433

34+
export const ValidationErrorIdentifier = builders.identifier('ValidationError');
535
export const ValidationErrorClass = builders.classDeclaration.from({
636
id: ValidationErrorIdentifier,
7-
superClass: builders.identifier('Error'),
37+
superClass: RequestErrorIdentifier,
838
body: builders.classBody([
939
builders.methodDefinition(
1040
'constructor',
@@ -14,7 +44,10 @@ export const ValidationErrorClass = builders.classDeclaration.from({
1444
[builders.identifier('path'), builders.identifier('message')],
1545
builders.blockStatement([
1646
builders.expressionStatement(
17-
builders.callExpression(builders.super(), [builders.identifier('message')]),
47+
builders.callExpression(builders.super(), [
48+
builders.literal(409),
49+
builders.identifier('message'),
50+
]),
1851
),
1952
builders.expressionStatement(
2053
builders.assignmentExpression(
@@ -33,11 +66,11 @@ export const ValidationErrorClass = builders.classDeclaration.from({
3366
});
3467

3568
/**
36-
* Build an empty error.
69+
* Build a request error.
3770
*/
38-
export function buildValidationError(message: string) {
39-
return builders.newExpression(ValidationErrorIdentifier, [
40-
builders.arrayExpression([]),
71+
export function buildRequestError(code: number, message: string) {
72+
return builders.newExpression(RequestErrorIdentifier, [
73+
builders.literal(code),
4174
builders.literal(message),
4275
]);
4376
}

0 commit comments

Comments
 (0)