Skip to content

Commit 7229861

Browse files
author
maxbronnikov10
committed
refactor(core,express,fastify): HTTP adapter error mapping
1 parent fa7525a commit 7229861

File tree

7 files changed

+116
-89
lines changed

7 files changed

+116
-89
lines changed

packages/core/adapters/http-adapter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ export abstract class AbstractHttpAdapter<
143143
return path;
144144
}
145145

146+
public mapException(error: unknown): unknown {
147+
return error;
148+
}
149+
146150
abstract close();
147151
abstract initHttpServer(options: NestApplicationOptions);
148152
abstract useStaticAssets(...args: any[]);

packages/core/router/routes-resolver.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class RoutesResolver implements Resolver {
166166
res: TResponse,
167167
next: Function,
168168
) => {
169-
throw this.mapExternalException(err);
169+
throw this.container.getHttpAdapterRef().mapException(err);
170170
};
171171
const handler = this.routerExceptionsFilter.create(
172172
{},
@@ -182,31 +182,6 @@ export class RoutesResolver implements Resolver {
182182
);
183183
}
184184

185-
public mapExternalException(err: any) {
186-
switch (true) {
187-
// SyntaxError is thrown by Express body-parser when given invalid JSON (#422, #430)
188-
// URIError is thrown by Express when given a path parameter with an invalid percentage
189-
// encoding, e.g. '%FF' (#8915)
190-
case err instanceof SyntaxError || err instanceof URIError:
191-
return new BadRequestException(err.message);
192-
case this.isHttpFastifyError(err):
193-
return new HttpException(err.message, err.statusCode);
194-
default:
195-
return err;
196-
}
197-
}
198-
199-
private isHttpFastifyError(
200-
error: any,
201-
): error is Error & { statusCode: number } {
202-
// condition based on this code - https://github.com/fastify/fastify-error/blob/d669b150a82968322f9f7be992b2f6b463272de3/index.js#L22
203-
return (
204-
error.statusCode !== undefined &&
205-
error instanceof Error &&
206-
error.name === 'FastifyError'
207-
);
208-
}
209-
210185
private getModulePathMetadata(metatype: Type<unknown>): string | undefined {
211186
const modulesContainer = this.container.getModules();
212187
const modulePath = Reflect.getMetadata(

packages/core/test/router/routes-resolver.spec.ts

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
BadRequestException,
3-
HttpException,
4-
Module,
5-
Post,
6-
VersioningType,
7-
} from '@nestjs/common';
1+
import { Module, Post, VersioningType } from '@nestjs/common';
82
import { MODULE_PATH } from '@nestjs/common/constants';
93
import { expect } from 'chai';
104
import * as sinon from 'sinon';
@@ -18,7 +12,6 @@ import { GraphInspector } from '../../inspector/graph-inspector';
1812
import { SerializedGraph } from '../../inspector/serialized-graph';
1913
import { RoutesResolver } from '../../router/routes-resolver';
2014
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
21-
import { createError as createFastifyError } from '@fastify/error';
2215

2316
describe('RoutesResolver', () => {
2417
@Controller('global')
@@ -320,61 +313,6 @@ describe('RoutesResolver', () => {
320313
});
321314
});
322315

323-
describe('mapExternalExceptions', () => {
324-
describe('when exception prototype is', () => {
325-
describe('SyntaxError', () => {
326-
it('should map to BadRequestException', () => {
327-
const err = new SyntaxError();
328-
const outputErr = routesResolver.mapExternalException(err);
329-
expect(outputErr).to.be.instanceof(BadRequestException);
330-
});
331-
});
332-
describe('URIError', () => {
333-
it('should map to BadRequestException', () => {
334-
const err = new URIError();
335-
const outputErr = routesResolver.mapExternalException(err);
336-
expect(outputErr).to.be.instanceof(BadRequestException);
337-
});
338-
});
339-
describe('FastifyError', () => {
340-
it('should map FastifyError with status code to HttpException', () => {
341-
const FastifyErrorCls = createFastifyError(
342-
'FST_ERR_CTP_INVALID_MEDIA_TYPE',
343-
'Unsupported Media Type: %s',
344-
415,
345-
);
346-
const error = new FastifyErrorCls();
347-
348-
const result = routesResolver.mapExternalException(error);
349-
350-
expect(result).to.be.instanceOf(HttpException);
351-
expect(result.message).to.equal(error.message);
352-
expect(result.getStatus()).to.equal(415);
353-
});
354-
355-
it('should return FastifyError without user status code to Internal Server Error HttpException', () => {
356-
const FastifyErrorCls = createFastifyError(
357-
'FST_WITHOUT_STATUS_CODE',
358-
'Error without status code',
359-
);
360-
const error = new FastifyErrorCls();
361-
362-
const result = routesResolver.mapExternalException(error);
363-
expect(result).to.be.instanceOf(HttpException);
364-
expect(result.message).to.equal(error.message);
365-
expect(result.getStatus()).to.equal(500);
366-
});
367-
});
368-
describe('other', () => {
369-
it('should behave as an identity', () => {
370-
const err = new Error();
371-
const outputErr = routesResolver.mapExternalException(err);
372-
expect(outputErr).to.be.eql(err);
373-
});
374-
});
375-
});
376-
});
377-
378316
describe('registerNotFoundHandler', () => {
379317
it('should register not found handler', () => {
380318
routesResolver.registerNotFoundHandler();

packages/platform-express/adapters/express-adapter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
BadRequestException,
23
HttpStatus,
34
InternalServerErrorException,
45
Logger,
@@ -448,6 +449,18 @@ export class ExpressAdapter extends AbstractHttpAdapter<
448449
throw new Error('Unsupported versioning options');
449450
}
450451

452+
public mapException(error: unknown): unknown {
453+
switch (true) {
454+
// SyntaxError is thrown by Express body-parser when given invalid JSON (#422, #430)
455+
// URIError is thrown by Express when given a path parameter with an invalid percentage
456+
// encoding, e.g. '%FF' (#8915)
457+
case error instanceof SyntaxError || error instanceof URIError:
458+
return new BadRequestException(error.message);
459+
default:
460+
return error;
461+
}
462+
}
463+
451464
private trackOpenConnections() {
452465
this.httpServer.on('connection', (socket: Duplex) => {
453466
this.openConnections.add(socket);

packages/platform-express/test/adapters/express-adapter.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import { BadRequestException } from '@nestjs/common';
12
import { ExpressAdapter } from '@nestjs/platform-express';
23
import { expect } from 'chai';
34
import * as express from 'express';
45
import * as sinon from 'sinon';
56

67
describe('ExpressAdapter', () => {
8+
let expressAdapter: ExpressAdapter;
9+
10+
beforeEach(() => {
11+
expressAdapter = new ExpressAdapter();
12+
});
13+
714
afterEach(() => sinon.restore());
815

916
describe('registerParserMiddleware', () => {
@@ -43,4 +50,24 @@ describe('ExpressAdapter', () => {
4350
expect(useSpy.called).to.be.false;
4451
});
4552
});
53+
54+
describe('mapException', () => {
55+
it('should map URIError with status code to BadRequestException', () => {
56+
const error = new URIError();
57+
const result = expressAdapter.mapException(error) as BadRequestException;
58+
expect(result).to.be.instanceOf(BadRequestException);
59+
});
60+
61+
it('should map SyntaxError with status code to BadRequestException', () => {
62+
const error = new SyntaxError();
63+
const result = expressAdapter.mapException(error) as BadRequestException;
64+
expect(result).to.be.instanceOf(BadRequestException);
65+
});
66+
67+
it('should return error if it is not handler Error', () => {
68+
const error = new Error('Test error');
69+
const result = expressAdapter.mapException(error);
70+
expect(result).to.equal(error);
71+
});
72+
});
4673
});

packages/platform-fastify/adapters/fastify-adapter.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22
import { FastifyCorsOptions } from '@fastify/cors';
33
import {
4+
BadRequestException,
5+
HttpException,
46
HttpStatus,
57
Logger,
68
RawBodyRequest,
@@ -18,6 +20,7 @@ import { LegacyRouteConverter } from '@nestjs/core/router/legacy-route-converter
1820
import {
1921
FastifyBaseLogger,
2022
FastifyBodyParser,
23+
FastifyError,
2124
FastifyInstance,
2225
FastifyListenOptions,
2326
FastifyPluginAsync,
@@ -670,6 +673,25 @@ export class FastifyAdapter<
670673
return 'fastify';
671674
}
672675

676+
public mapException(error: unknown): unknown {
677+
if (this.isHttpFastifyError(error)) {
678+
return new HttpException(error.message, error.statusCode);
679+
}
680+
681+
return error;
682+
}
683+
684+
private isHttpFastifyError(
685+
error: any,
686+
): error is Error & { statusCode: number } {
687+
// condition based on this code - https://github.com/fastify/fastify-error/blob/d669b150a82968322f9f7be992b2f6b463272de3/index.js#L22
688+
return (
689+
error.statusCode !== undefined &&
690+
error instanceof Error &&
691+
error.name === 'FastifyError'
692+
);
693+
}
694+
673695
protected registerWithPrefix(
674696
factory:
675697
| FastifyPluginCallback<any>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { FastifyAdapter } from '../../adapters/fastify-adapter';
2+
import { expect } from 'chai';
3+
import { createError } from '@fastify/error';
4+
import { HttpException } from '@nestjs/common';
5+
6+
describe('FastifyAdapter', () => {
7+
let fastifyAdapter: FastifyAdapter;
8+
9+
beforeEach(() => {
10+
fastifyAdapter = new FastifyAdapter();
11+
});
12+
13+
describe('mapException', () => {
14+
it('should map FastifyError with status code to HttpException', () => {
15+
const FastifyErrorCls = createError(
16+
'FST_ERR_CTP_INVALID_MEDIA_TYPE',
17+
'Unsupported Media Type: %s',
18+
415,
19+
);
20+
const error = new FastifyErrorCls();
21+
22+
const result = fastifyAdapter.mapException(error) as HttpException;
23+
24+
expect(result).to.be.instanceOf(HttpException);
25+
expect(result.message).to.equal(error.message);
26+
expect(result.getStatus()).to.equal(415);
27+
});
28+
29+
it('should return FastifyError without user status code to Internal Server Error HttpException', () => {
30+
const FastifyErrorCls = createError(
31+
'FST_WITHOUT_STATUS_CODE',
32+
'Error without status code',
33+
);
34+
const error = new FastifyErrorCls();
35+
36+
const result = fastifyAdapter.mapException(error) as HttpException;
37+
expect(result).to.be.instanceOf(HttpException);
38+
expect(result.message).to.equal(error.message);
39+
expect(result.getStatus()).to.equal(500);
40+
});
41+
42+
it('should return error if it is not FastifyError', () => {
43+
const error = new Error('Test error');
44+
const result = fastifyAdapter.mapException(error);
45+
expect(result).to.equal(error);
46+
});
47+
});
48+
});

0 commit comments

Comments
 (0)