Skip to content
Open
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
29 changes: 29 additions & 0 deletions examples/error_details/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Error Details

This example demonstrates the use of status details in grpc errors.

## Start the server

Run the server, which sends a rich error if the name field is empty.

```
node server.js
```

## Run the client

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.

```
node client.js
```

## EWxpected Output
```
Greeting: Hello World

--- Standard gRPC Error Received ---
Code: 3
Status: INVALID_ARGUMENT
Message: 3 INVALID_ARGUMENT: Simple Error: The name field was empty.
```
57 changes: 57 additions & 0 deletions examples/error_details/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* Copyright 2025 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

var PROTO_PATH = __dirname + '/../protos/helloworld.proto';

var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

var client = new hello_proto.Greeter('localhost:50051', grpc.credentials.createInsecure());

function main() {
client.sayHello({ name: 'World' }, function (err, response) {
if (err) {
console.error('Success call failed:', err);
return;
}
console.log('Greeting:', response.message);

client.sayHello({ name: '' }, function (err, response) {
if (err) {
console.log('--- Standard gRPC Error Received ---');
console.log(`Code: ${err.code}`);
console.log(`Status: ${grpc.status[err.code]}`);
console.log(`Message: ${err.message}`);
} else {
console.log('Failing call unexpectedly succeeded:', response.message);
}
});
});
}

main();
60 changes: 60 additions & 0 deletions examples/error_details/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright 2025 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

var PROTO_PATH = __dirname + '/../protos/helloworld.proto';

var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

/**
* Implements the SayHello RPC method.
*/
function sayHello(call, callback) {
if (call.request.name === '') {
callback({
code: grpc.status.INVALID_ARGUMENT,
details: 'Simple Error: The name field was empty.'
});
return;
}

callback(null, { message: 'Hello ' + call.request.name });
}

/**
* Starts an RPC server.
*/
function main() {
var server = new grpc.Server();
server.addService(hello_proto.Greeter.service, { sayHello: sayHello });
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
console.log('Server running at http://0.0.0.0:50051');
});
}

main();