Skip to content

Commit

Permalink
Merge branch 'quangtran88-fix/handle-error-steram-grpc-controller'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Aug 28, 2023
2 parents 974f2c6 + 8857e7e commit 76223af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/microservices/server/server-grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
isString,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import { EMPTY, fromEvent, lastValueFrom, Subject } from 'rxjs';
import { EMPTY, Subject, defaultIfEmpty, fromEvent, lastValueFrom } from 'rxjs';
import { catchError, takeUntil } from 'rxjs/operators';
import {
CANCEL_EVENT,
Expand Down Expand Up @@ -289,6 +289,7 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
callback(err, null);
return EMPTY;
}),
defaultIfEmpty(undefined),
),
);

Expand Down
35 changes: 29 additions & 6 deletions packages/microservices/test/server/server-grpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from '@nestjs/common';
import { expect } from 'chai';
import { join } from 'path';
import { of, ReplaySubject, Subject } from 'rxjs';
import { of, ReplaySubject, throwError } from 'rxjs';
import * as sinon from 'sinon';
import { CANCEL_EVENT } from '../../constants';
import { InvalidGrpcPackageException } from '../../errors/invalid-grpc-package.exception';
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('ServerGrpc', () => {
callback = sinon.spy();
bindEventsStub = sinon
.stub(server, 'bindEvents')
.callsFake(() => ({} as any));
.callsFake(() => ({}) as any);
});

it('should call "bindEvents"', async () => {
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('ServerGrpc', () => {
callback = sinon.spy();
bindEventsStub = sinon
.stub(serverMulti, 'bindEvents')
.callsFake(() => ({} as any));
.callsFake(() => ({}) as any);
});

it('should call "bindEvents"', async () => {
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('ServerGrpc', () => {

const spy = sinon
.stub(server, 'createServiceMethod')
.callsFake(() => ({} as any));
.callsFake(() => ({}) as any);
(server as any).messageHandlers = handlers;
await server.createService(
{
Expand All @@ -246,7 +246,7 @@ describe('ServerGrpc', () => {
.onFirstCall()
.returns('test2');

sinon.stub(server, 'createServiceMethod').callsFake(() => ({} as any));
sinon.stub(server, 'createServiceMethod').callsFake(() => ({}) as any);
(server as any).messageHandlers = handlers;
await server.createService(
{
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('ServerGrpc', () => {
.onSecondCall()
.returns('test2');

sinon.stub(server, 'createServiceMethod').callsFake(() => ({} as any));
sinon.stub(server, 'createServiceMethod').callsFake(() => ({}) as any);
(server as any).messageHandlers = handlers;
await server.createService(
{
Expand Down Expand Up @@ -520,6 +520,29 @@ describe('ServerGrpc', () => {

expect(responseCallback.called).to.be.true;
});

it('should handle error thrown in handler', async () => {
const error = new Error('Error');
const handler = async () => throwError(() => error);
const fn = server.createRequestStreamMethod(handler, false);
const call = {
on: (event, callback) => {
if (event !== CANCEL_EVENT) {
callback();
}
},
off: sinon.spy(),
end: sinon.spy(),
write: sinon.spy(),
};

const responseCallback = sinon.spy();
await fn(call as any, responseCallback);

expect(responseCallback.calledOnce).to.be.true;
expect(responseCallback.firstCall.args).to.eql([error, null]);
});

describe('when response is a stream', () => {
it('should call write() and end()', async () => {
const handler = async () => ({ test: true });
Expand Down

0 comments on commit 76223af

Please sign in to comment.