diff --git a/packages/microservices/errors/invalid-grpc-package-definition-missing-package-definition.exception.ts b/packages/microservices/errors/invalid-grpc-package-definition-missing-package-definition.exception.ts index 40ce6854c07..c5483358ec2 100644 --- a/packages/microservices/errors/invalid-grpc-package-definition-missing-package-definition.exception.ts +++ b/packages/microservices/errors/invalid-grpc-package-definition-missing-package-definition.exception.ts @@ -1,7 +1,9 @@ import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.exception'; -export class InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException extends RuntimeException { +export class InvalidGrpcPackageDefinitionMissingPackageDefinitionException extends RuntimeException { constructor() { - super('protoPath or packageDefinition must be defined'); + super( + `Invalid gRPC configuration. protoPath or packageDefinition must be defined.`, + ); } } diff --git a/packages/microservices/errors/invalid-grpc-package-definition-mutex.exception.ts b/packages/microservices/errors/invalid-grpc-package-definition-mutex.exception.ts index 8720b688acb..baa4f8be71f 100644 --- a/packages/microservices/errors/invalid-grpc-package-definition-mutex.exception.ts +++ b/packages/microservices/errors/invalid-grpc-package-definition-mutex.exception.ts @@ -3,7 +3,7 @@ import { RuntimeException } from '@nestjs/core/errors/exceptions/runtime.excepti export class InvalidGrpcPackageDefinitionMutexException extends RuntimeException { constructor() { super( - 'Both protoPath and packageDefinition were provided, but only one can be defined', + `Invalid gRPC configuration. Both protoPath and packageDefinition cannot be defined at the same time.`, ); } } diff --git a/packages/microservices/helpers/grpc-helpers.ts b/packages/microservices/helpers/grpc-helpers.ts index b1d6460a415..56bf9ad2463 100644 --- a/packages/microservices/helpers/grpc-helpers.ts +++ b/packages/microservices/helpers/grpc-helpers.ts @@ -1,6 +1,6 @@ -import { GrpcOptions } from '../interfaces'; +import { InvalidGrpcPackageDefinitionMissingPackageDefinitionException } from '../errors/invalid-grpc-package-definition-missing-package-definition.exception'; import { InvalidGrpcPackageDefinitionMutexException } from '../errors/invalid-grpc-package-definition-mutex.exception'; -import { InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException } from '../errors/invalid-grpc-package-definition-missing-package-definition.exception'; +import { GrpcOptions } from '../interfaces'; export function getGrpcPackageDefinition( options: GrpcOptions['options'], @@ -9,12 +9,11 @@ export function getGrpcPackageDefinition( const file = options['protoPath']; const packageDefinition = options['packageDefinition']; - if ([file, packageDefinition].every(x => x != undefined)) { + if (file && packageDefinition) { throw new InvalidGrpcPackageDefinitionMutexException(); } - - if ([file, packageDefinition].every(x => x == undefined)) { - throw new InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException(); + if (!file && !packageDefinition) { + throw new InvalidGrpcPackageDefinitionMissingPackageDefinitionException(); } return ( diff --git a/packages/microservices/server/server-grpc.ts b/packages/microservices/server/server-grpc.ts index c097d44a210..fbcc280efd0 100644 --- a/packages/microservices/server/server-grpc.ts +++ b/packages/microservices/server/server-grpc.ts @@ -23,10 +23,10 @@ import { Transport } from '../enums'; import { InvalidGrpcPackageException } from '../errors/invalid-grpc-package.exception'; import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definition.exception'; import { ChannelOptions } from '../external/grpc-options.interface'; +import { getGrpcPackageDefinition } from '../helpers'; import { CustomTransportStrategy, MessageHandler } from '../interfaces'; import { GrpcOptions } from '../interfaces/microservice-configuration.interface'; import { Server } from './server'; -import { getGrpcPackageDefinition } from '../helpers'; let grpcPackage: any = {}; let grpcProtoLoaderPackage: any = {}; diff --git a/packages/microservices/test/helpers/grpc-helpers.spec.ts b/packages/microservices/test/helpers/grpc-helpers.spec.ts index 4348ecb5922..e8844a1f9dd 100644 --- a/packages/microservices/test/helpers/grpc-helpers.spec.ts +++ b/packages/microservices/test/helpers/grpc-helpers.spec.ts @@ -1,56 +1,64 @@ import { expect } from 'chai'; -import { getGrpcPackageDefinition } from '../../helpers/grpc-helpers'; +import { InvalidGrpcPackageDefinitionMissingPackageDefinitionException } from '../../errors/invalid-grpc-package-definition-missing-package-definition.exception'; import { InvalidGrpcPackageDefinitionMutexException } from '../../errors/invalid-grpc-package-definition-mutex.exception'; -import { InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException } from '../../errors/invalid-grpc-package-definition-missing-package-definition.exception'; +import { getGrpcPackageDefinition } from '../../helpers/grpc-helpers'; const grpcProtoLoaderPackage = { loadSync: (a, b) => 'withLoader' }; describe('getGrpcPackageDefinition', () => { - it('missing both protoPath and packageDefinition', () => { - expect(() => - getGrpcPackageDefinition( - { - package: 'somePackage', - }, - grpcProtoLoaderPackage, - ), - ).to.throw(InvalidGrpcPackageDefinitionMissingPacakgeDefinitionException); + describe('when missing both protoPath and packageDefinition', () => { + it('should throw InvalidGrpcPackageDefinitionMissingPackageDefinitionException', () => { + expect(() => + getGrpcPackageDefinition( + { + package: 'somePackage', + }, + grpcProtoLoaderPackage, + ), + ).to.throw(InvalidGrpcPackageDefinitionMissingPackageDefinitionException); + }); }); - it('got both protoPath and packageDefinition', () => { - expect(() => - getGrpcPackageDefinition( - { - package: 'somePackage', - protoPath: 'some/path', - packageDefinition: {}, - }, - grpcProtoLoaderPackage, - ), - ).to.throw(InvalidGrpcPackageDefinitionMutexException); + describe('when both protoPath and packageDefinition are defined', () => { + it('should throw InvalidGrpcPackageDefinitionMutexException', () => { + expect(() => + getGrpcPackageDefinition( + { + package: 'somePackage', + protoPath: 'some/path', + packageDefinition: {}, + }, + grpcProtoLoaderPackage, + ), + ).to.throw(InvalidGrpcPackageDefinitionMutexException); + }); }); - it('success with protoPath', () => { - expect(() => - getGrpcPackageDefinition( - { - package: 'somePackage', - protoPath: 'some/path', - }, - grpcProtoLoaderPackage, - ), - ).to.not.throw(Error); + describe('when only protoPath is defined', () => { + it('should not throw any exception', () => { + expect(() => + getGrpcPackageDefinition( + { + package: 'somePackage', + protoPath: 'some/path', + }, + grpcProtoLoaderPackage, + ), + ).to.not.throw(Error); + }); }); - it('success with packageDef', () => { - expect(() => - getGrpcPackageDefinition( - { - package: 'somePackage', - packageDefinition: {}, - }, - grpcProtoLoaderPackage, - ), - ).to.not.throw(Error); + describe('when only packageDefinition is defined', () => { + it('should not throw any exception', () => { + expect(() => + getGrpcPackageDefinition( + { + package: 'somePackage', + packageDefinition: {}, + }, + grpcProtoLoaderPackage, + ), + ).to.not.throw(Error); + }); }); }); diff --git a/packages/microservices/test/server/server-grpc.spec.ts b/packages/microservices/test/server/server-grpc.spec.ts index 65d6b87255f..3e0fb4147cd 100644 --- a/packages/microservices/test/server/server-grpc.spec.ts +++ b/packages/microservices/test/server/server-grpc.spec.ts @@ -1,21 +1,14 @@ import { Logger } from '@nestjs/common'; import { expect } from 'chai'; import { join } from 'path'; -import { - async, - Observable, - of, - ReplaySubject, - Subject, - throwError, -} from 'rxjs'; +import { ReplaySubject, Subject, throwError } from 'rxjs'; import * as sinon from 'sinon'; import { CANCEL_EVENT } from '../../constants'; import { InvalidGrpcPackageException } from '../../errors/invalid-grpc-package.exception'; -import { GrpcMethodStreamingType } from '../../index'; -import { ServerGrpc } from '../../server'; import { InvalidProtoDefinitionException } from '../../errors/invalid-proto-definition.exception'; import * as grpcHelpers from '../../helpers/grpc-helpers'; +import { GrpcMethodStreamingType } from '../../index'; +import { ServerGrpc } from '../../server'; class NoopLogger extends Logger { log(message: any, context?: string): void {}