Reputation: 145
I´m new to GRPC and I´d like to learn about error formatting using gRPC and NodeJS.
Whenever we need to build a request handler for a gRPC server, we follow this pattern:
* getFeature request handler. Gets a request with a point, and responds with a
* feature object indicating whether there is a feature at that point.
* @param {EventEmitter} call Call object for the handler to process
* @param {function(Error, feature)} callback Response callback
*/
function getFeature(call, callback) {
callback(null, checkFeature(call.request));
}
It´s clear to me what the call parameter is, and how to use callbacks.
My question is, how is the first field of the callback supossed to look like? I think there is no error standard in nodeJS, and it seems like this should be a gRPC specific format.
I´ve looked around and there is a standard of gRPC status codes. Is that the error field? an int representing the status or a 0 (or nothing) if everything wet ok?
Upvotes: 4
Views: 647
Reputation: 20267
You have a few options there. Like most callback-based asynchronous APIs in JavaScript, that callback accepts a regular JS Error
object. That will translate into an UNKNOWN
status code. You can also set the code
property on the error object to a member of the grpc.status
enum. For example you can do error.code = grpc.status.NOT_FOUND
to send a "Not found" error. In addition, you can set the metadata
property on the error object to an instance of the grpc.Metadata
class to send additional trailing metadata along with the error.
On top of all of that, you can also pass a simple object with a details
string and code
and metadata
properties that are the same as described above. For example:
callback({details: "Entry not found", code: grpc.status.NOT_FOUND});
Note that if everything goes OK, that error parameter should just be null
.
Upvotes: 2