Skip to content

Commit f7d9baa

Browse files
authored
Merge pull request #2585 from murgatroid99/example_error_handling
Add error handling example
2 parents 220ee8b + 0ebfe60 commit f7d9baa

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

examples/error_handling/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Error Handling
2+
3+
This example demonstrates basic RPC error handling in gRPC for unary and
4+
streaming response cardinalities.
5+
6+
## Start the server
7+
8+
Run the server, whcih returns an error if the RPC request's `name` field is
9+
empty.
10+
11+
```
12+
node server.js
13+
```
14+
15+
## Run the client
16+
17+
Then run the client in another terminal, which makes two requests for each of
18+
unary and streaming responses: one with an empty Name field and one with it
19+
populated with the current username provided by os/user.
20+
21+
```
22+
node client.js
23+
```

examples/error_handling/client.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
*
3+
* Copyright 2023 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
const grpc = require('@grpc/grpc-js');
20+
const protoLoader = require('@grpc/proto-loader');
21+
const parseArgs = require('minimist');
22+
const os = require('os');
23+
24+
const PROTO_PATH = __dirname + '/../protos/helloworld.proto';
25+
26+
const packageDefinition = protoLoader.loadSync(
27+
PROTO_PATH,
28+
{keepCase: true,
29+
longs: String,
30+
enums: String,
31+
defaults: true,
32+
oneofs: true
33+
});
34+
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
35+
36+
function unaryCall(client, requestId, name, expectedCode) {
37+
console.log(`[${requestId}] Calling SayHello with name:"${name}"`);
38+
return new Promise((resolve, reject) => {
39+
client.sayHello({name: name}, (error, value) => {
40+
if (error) {
41+
if (error.code === expectedCode) {
42+
console.log(`[${requestId}] Received error ${error.message}`);
43+
} else {
44+
console.log(`[${requestId}] Received unexpected error ${error.message}`);
45+
}
46+
}
47+
if (value) {
48+
console.log(`[${requestId}] Received response ${value.message}`);
49+
}
50+
resolve();
51+
});
52+
});
53+
}
54+
55+
function streamingCall(client, requestId, name, expectedCode) {
56+
console.log(`[${requestId}] Calling SayHelloStreamReply with name:"${name}"`);
57+
return new Promise((resolve, reject) => {
58+
const call = client.sayHelloStreamReply({name: name});
59+
call.on('data', value => {
60+
console.log(`[${requestId}] Received response ${value.message}`);
61+
});
62+
call.on('status', status => {
63+
console.log(`[${requestId}] Received status with code=${grpc.status[status.code]} details=${status.details}`);
64+
resolve();
65+
});
66+
call.on('error', error => {
67+
if (error.code === expectedCode) {
68+
console.log(`[${requestId}] Received expected error ${error.message}`);
69+
} else {
70+
console.log(`[${requestId}] Received unexpected error ${error.message}`);
71+
}
72+
});
73+
});
74+
}
75+
76+
async function main() {
77+
let argv = parseArgs(process.argv.slice(2), {
78+
string: 'target',
79+
default: {target: 'localhost:50052'}
80+
});
81+
const client = new helloProto.Greeter(argv.target, grpc.credentials.createInsecure());
82+
const name = os.userInfo().username ?? 'unknown';
83+
await unaryCall(client, 1, '', grpc.status.INVALID_ARGUMENT);
84+
await unaryCall(client, 2, name, grpc.status.OK);
85+
await streamingCall(client, 3, '', grpc.status.INVALID_ARGUMENT);
86+
await streamingCall(client, 4, name, grpc.status.OK);
87+
}
88+
89+
main();

examples/error_handling/server.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
*
3+
* Copyright 2023 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
var PROTO_PATH = __dirname + '/../protos/helloworld.proto';
20+
21+
var grpc = require('@grpc/grpc-js');
22+
var protoLoader = require('@grpc/proto-loader');
23+
var packageDefinition = protoLoader.loadSync(
24+
PROTO_PATH,
25+
{keepCase: true,
26+
longs: String,
27+
enums: String,
28+
defaults: true,
29+
oneofs: true
30+
});
31+
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
32+
33+
/**
34+
* Implements the SayHello RPC method.
35+
*/
36+
function sayHello(call, callback) {
37+
if (call.request.name === '') {
38+
callback({code: grpc.status.INVALID_ARGUMENT, details: 'request missing required field: name'});
39+
}
40+
callback(null, {message: 'Hello ' + call.request.name});
41+
}
42+
43+
const REPLY_COUNT = 5;
44+
45+
function sayHelloStreamReply(call) {
46+
if (call.request.name === '') {
47+
call.emit('error', {code: grpc.status.INVALID_ARGUMENT, details: 'request missing required field: name'});
48+
} else {
49+
for (let i = 0; i < REPLY_COUNT; i++) {
50+
call.write({message: 'Hello ' + call.request.name});
51+
}
52+
call.end();
53+
}
54+
}
55+
56+
/**
57+
* Starts an RPC server that receives requests for the Greeter service at the
58+
* sample server port
59+
*/
60+
function main() {
61+
var server = new grpc.Server();
62+
server.addService(hello_proto.Greeter.service, {sayHello: sayHello, sayHelloStreamReply: sayHelloStreamReply});
63+
server.bindAsync('0.0.0.0:50052', grpc.ServerCredentials.createInsecure(), () => {
64+
server.start();
65+
});
66+
}
67+
68+
main();

0 commit comments

Comments
 (0)