Skip to content

Commit dfcc2f2

Browse files
committed
Added error status details example
1 parent dac1da4 commit dfcc2f2

3 files changed

Lines changed: 146 additions & 0 deletions

File tree

examples/error_details/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Error Details
2+
3+
This example demonstrates the use of status details in grpc errors.
4+
5+
## Start the server
6+
7+
Run the server, which sends a rich error if the name field is empty.
8+
9+
```
10+
node server.js
11+
```
12+
13+
## Run the client
14+
15+
Run the client in another terminal. It will make two calls: first, a successful call with a valid name, and second, a failing call with an empty name.
16+
17+
```
18+
node client.js
19+
```
20+
21+
## EWxpected Output
22+
```
23+
Greeting: Hello World
24+
25+
--- Standard gRPC Error Received ---
26+
Code: 3
27+
Status: INVALID_ARGUMENT
28+
Message: 3 INVALID_ARGUMENT: Simple Error: The name field was empty.
29+
```

examples/error_details/client.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
*
3+
* Copyright 2025 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+
{
26+
keepCase: true,
27+
longs: String,
28+
enums: String,
29+
defaults: true,
30+
oneofs: true
31+
});
32+
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
33+
34+
var client = new hello_proto.Greeter('localhost:50051', grpc.credentials.createInsecure());
35+
36+
function main() {
37+
client.sayHello({ name: 'World' }, function (err, response) {
38+
if (err) {
39+
console.error('Success call failed:', err);
40+
return;
41+
}
42+
console.log('Greeting:', response.message);
43+
44+
client.sayHello({ name: '' }, function (err, response) {
45+
if (err) {
46+
console.log('--- Standard gRPC Error Received ---');
47+
console.log(`Code: ${err.code}`);
48+
console.log(`Status: ${grpc.status[err.code]}`);
49+
console.log(`Message: ${err.message}`);
50+
} else {
51+
console.log('Failing call unexpectedly succeeded:', response.message);
52+
}
53+
});
54+
});
55+
}
56+
57+
main();

examples/error_details/server.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
*
3+
* Copyright 2025 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+
{
26+
keepCase: true,
27+
longs: String,
28+
enums: String,
29+
defaults: true,
30+
oneofs: true
31+
});
32+
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
33+
34+
/**
35+
* Implements the SayHello RPC method.
36+
*/
37+
function sayHello(call, callback) {
38+
if (call.request.name === '') {
39+
callback({
40+
code: grpc.status.INVALID_ARGUMENT,
41+
details: 'Simple Error: The name field was empty.'
42+
});
43+
return;
44+
}
45+
46+
callback(null, { message: 'Hello ' + call.request.name });
47+
}
48+
49+
/**
50+
* Starts an RPC server.
51+
*/
52+
function main() {
53+
var server = new grpc.Server();
54+
server.addService(hello_proto.Greeter.service, { sayHello: sayHello });
55+
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
56+
console.log('Server running at http://0.0.0.0:50051');
57+
});
58+
}
59+
60+
main();

0 commit comments

Comments
 (0)