Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(wip): GRPC exception and mapping #1957

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,823 changes: 1,094 additions & 729 deletions integration/graphql/package-lock.json

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion integration/microservices/e2e/sum-grpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ describe('GRPC transport', () => {
});
});

after(async () => {
it(`handle exception correctly`, () => {
return (
request(server)
.post('/divide')
.send({ first: 42, last: 0 })
// TODO: this will change once the grpc client raise the closest HTTP exception to OutOfBound
.expect(500, { message: 'Dividing by 0 is Strictly Forbidden' })
);
});

afterEach(async () => {
await app.close();
client.close();
});
Expand Down
26 changes: 24 additions & 2 deletions integration/microservices/src/grpc/grpc.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Body, Controller, HttpCode, Post } from '@nestjs/common';
import { Body, Controller, HttpCode, Post, UseFilters } from '@nestjs/common';
import {
Client,
ClientGrpc,
GrpcMethod,
GrpcStreamCall,
GrpcStreamMethod,
Transport,
GrpcOutOfRangeException,
} from '@nestjs/microservices';
import { join } from 'path';
import { Observable, of } from 'rxjs';

interface DivideParams {
first: number;
last: number;
}

@Controller()
export class GrpcController {
@Client({
Expand All @@ -23,7 +29,7 @@ export class GrpcController {

@Post()
@HttpCode(200)
call(@Body() data: number[]): Observable<number> {
sumHttp(@Body() data: number[]): Observable<number> {
const svc = this.client.getService<any>('Math');
return svc.sum({ data });
}
Expand Down Expand Up @@ -57,4 +63,20 @@ export class GrpcController {
stream.write({ result: msg.data.reduce((a, b) => a + b) });
});
}

@Post('/divide')
@HttpCode(200)
divideHttp(@Body() { first, last }: DivideParams): Observable<number> {
const svc = this.client.getService<any>('Math');
return svc.sum({ first, last });
}

@GrpcMethod('Math')
async divide({ first, last }: DivideParams): Promise<any> {
const result = first / last;
if (!isFinite(result)) {
throw new GrpcOutOfRangeException('Dividing by 0 is Strictly Forbidden');
}
return result;
}
}
20 changes: 12 additions & 8 deletions integration/microservices/src/grpc/math.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ syntax = "proto3";
package math;

service Math {
rpc Sum (RequestSum) returns (SumResult);
rpc SumStream(stream RequestSum) returns(stream SumResult);
rpc SumStreamPass(stream RequestSum) returns(stream SumResult);
rpc Sum(RequestSum) returns (SumResult);
rpc SumStream(stream RequestSum) returns (stream SumResult);
rpc SumStreamPass(stream RequestSum) returns (stream SumResult);
rpc Divide(RequestDivide) returns (DivideResult) {}
}

message SumResult {
int32 result = 1;
}
message SumResult { int32 result = 1; }

message RequestSum { repeated int32 data = 1; }

message DivideResult { int32 result = 1; }

message RequestSum {
repeated int32 data = 1;
message RequestDivide {
int32 first = 1;
int32 last = 2;
}
Loading