Let Nest exception filter handle unexpected exceptions
This commit is contained in:
parent
739d05b095
commit
7f7a51d19b
|
@ -1,17 +1,16 @@
|
||||||
import { Controller, Inject, UseInterceptors, UsePipes } from '@nestjs/common';
|
|
||||||
import { GrpcMethod, RpcException } from '@nestjs/microservices';
|
|
||||||
import { RpcValidationPipe } from '@mobicoop/ddd-library';
|
import { RpcValidationPipe } from '@mobicoop/ddd-library';
|
||||||
import { RpcExceptionCode } from '@mobicoop/ddd-library';
|
|
||||||
import { MatchingPaginatedResponseDto } from '../dtos/matching.paginated.response.dto';
|
|
||||||
import { QueryBus } from '@nestjs/cqrs';
|
|
||||||
import { MatchRequestDto } from './dtos/match.request.dto';
|
|
||||||
import { MatchQuery } from '@modules/ad/core/application/queries/match/match.query';
|
|
||||||
import { MatchEntity } from '@modules/ad/core/domain/match.entity';
|
|
||||||
import { AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
|
import { AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
|
||||||
import { MatchMapper } from '@modules/ad/match.mapper';
|
|
||||||
import { MatchingResult } from '@modules/ad/core/application/queries/match/match.query-handler';
|
|
||||||
import { CacheInterceptor, CacheKey } from '@nestjs/cache-manager';
|
|
||||||
import { GeorouterPort } from '@modules/ad/core/application/ports/georouter.port';
|
import { GeorouterPort } from '@modules/ad/core/application/ports/georouter.port';
|
||||||
|
import { MatchQuery } from '@modules/ad/core/application/queries/match/match.query';
|
||||||
|
import { MatchingResult } from '@modules/ad/core/application/queries/match/match.query-handler';
|
||||||
|
import { MatchEntity } from '@modules/ad/core/domain/match.entity';
|
||||||
|
import { MatchMapper } from '@modules/ad/match.mapper';
|
||||||
|
import { CacheInterceptor, CacheKey } from '@nestjs/cache-manager';
|
||||||
|
import { Controller, Inject, UseInterceptors, UsePipes } from '@nestjs/common';
|
||||||
|
import { QueryBus } from '@nestjs/cqrs';
|
||||||
|
import { GrpcMethod } from '@nestjs/microservices';
|
||||||
|
import { MatchingPaginatedResponseDto } from '../dtos/matching.paginated.response.dto';
|
||||||
|
import { MatchRequestDto } from './dtos/match.request.dto';
|
||||||
|
|
||||||
@UsePipes(
|
@UsePipes(
|
||||||
new RpcValidationPipe({
|
new RpcValidationPipe({
|
||||||
|
@ -32,7 +31,6 @@ export class MatchGrpcController {
|
||||||
@UseInterceptors(CacheInterceptor)
|
@UseInterceptors(CacheInterceptor)
|
||||||
@GrpcMethod('MatcherService', 'Match')
|
@GrpcMethod('MatcherService', 'Match')
|
||||||
async match(data: MatchRequestDto): Promise<MatchingPaginatedResponseDto> {
|
async match(data: MatchRequestDto): Promise<MatchingPaginatedResponseDto> {
|
||||||
try {
|
|
||||||
const matchingResult: MatchingResult = await this.queryBus.execute(
|
const matchingResult: MatchingResult = await this.queryBus.execute(
|
||||||
new MatchQuery(data, this.routeProvider),
|
new MatchQuery(data, this.routeProvider),
|
||||||
);
|
);
|
||||||
|
@ -45,11 +43,5 @@ export class MatchGrpcController {
|
||||||
perPage: matchingResult.perPage,
|
perPage: matchingResult.perPage,
|
||||||
total: matchingResult.total,
|
total: matchingResult.total,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
|
||||||
throw new RpcException({
|
|
||||||
code: RpcExceptionCode.UNKNOWN,
|
|
||||||
message: e.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { RpcExceptionCode } from '@mobicoop/ddd-library';
|
|
||||||
import { AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
|
import { AD_ROUTE_PROVIDER } from '@modules/ad/ad.di-tokens';
|
||||||
import { MatchingResult } from '@modules/ad/core/application/queries/match/match.query-handler';
|
import { MatchingResult } from '@modules/ad/core/application/queries/match/match.query-handler';
|
||||||
import { AlgorithmType } from '@modules/ad/core/application/types/algorithm.types';
|
import { AlgorithmType } from '@modules/ad/core/application/types/algorithm.types';
|
||||||
|
@ -14,7 +13,6 @@ import { MatchGrpcController } from '@modules/ad/interface/grpc-controllers/matc
|
||||||
import { MatchMapper } from '@modules/ad/match.mapper';
|
import { MatchMapper } from '@modules/ad/match.mapper';
|
||||||
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||||
import { QueryBus } from '@nestjs/cqrs';
|
import { QueryBus } from '@nestjs/cqrs';
|
||||||
import { RpcException } from '@nestjs/microservices';
|
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { bareMockGeorouter } from '../georouter.mock';
|
import { bareMockGeorouter } from '../georouter.mock';
|
||||||
|
|
||||||
|
@ -56,9 +54,7 @@ const recurrentMatchRequestDto: MatchRequestDto = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockQueryBus = {
|
const mockQueryBus = {
|
||||||
execute: jest
|
execute: jest.fn().mockImplementationOnce(
|
||||||
.fn()
|
|
||||||
.mockImplementationOnce(
|
|
||||||
() =>
|
() =>
|
||||||
<MatchingResult>{
|
<MatchingResult>{
|
||||||
id: '43c83ae2-f4b0-4ac6-b8bf-8071801924d4',
|
id: '43c83ae2-f4b0-4ac6-b8bf-8071801924d4',
|
||||||
|
@ -177,10 +173,7 @@ const mockQueryBus = {
|
||||||
],
|
],
|
||||||
total: 1,
|
total: 1,
|
||||||
},
|
},
|
||||||
)
|
),
|
||||||
.mockImplementationOnce(() => {
|
|
||||||
throw new Error();
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockMatchMapper = {
|
const mockMatchMapper = {
|
||||||
|
@ -317,16 +310,4 @@ describe('Match Grpc Controller', () => {
|
||||||
expect(matchingPaginatedResponseDto.perPage).toBe(10);
|
expect(matchingPaginatedResponseDto.perPage).toBe(10);
|
||||||
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
|
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw a generic RpcException', async () => {
|
|
||||||
jest.spyOn(mockQueryBus, 'execute');
|
|
||||||
expect.assertions(3);
|
|
||||||
try {
|
|
||||||
await matchGrpcController.match(recurrentMatchRequestDto);
|
|
||||||
} catch (e: any) {
|
|
||||||
expect(e).toBeInstanceOf(RpcException);
|
|
||||||
expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN);
|
|
||||||
}
|
|
||||||
expect(mockQueryBus.execute).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue