diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc index dafe44c7a624f9ffdb2cfb2c67a27720321850e6..18858fa334ad8e7763a4c37b8ed4006f85c48801 100644 --- a/src/node/ext/call.cc +++ b/src/node/ext/call.cc @@ -623,7 +623,7 @@ NAN_METHOD(Call::StartBatch) { call->wrapped_call, &ops[0], nops, new struct tag( callback, op_vector.release(), resources), NULL); if (error != GRPC_CALL_OK) { - return NanThrowError("startBatch failed", error); + return NanThrowError(nanErrorWithCode("startBatch failed", error)); } CompletionQueueAsyncWorker::Next(); NanReturnUndefined(); @@ -637,7 +637,7 @@ NAN_METHOD(Call::Cancel) { Call *call = ObjectWrap::Unwrap<Call>(args.This()); grpc_call_error error = grpc_call_cancel(call->wrapped_call, NULL); if (error != GRPC_CALL_OK) { - return NanThrowError("cancel failed", error); + return NanThrowError(nanErrorWithCode("cancel failed", error)); } NanReturnUndefined(); } diff --git a/src/node/ext/call.h b/src/node/ext/call.h index 6acda76197ffcac36af764ba5c83e5a6c7df510c..ef6e5fcd21083353c7d241ca9fee8e8fc8ed0990 100644 --- a/src/node/ext/call.h +++ b/src/node/ext/call.h @@ -51,6 +51,19 @@ namespace node { using std::unique_ptr; using std::shared_ptr; +/** + * Helper function for throwing errors with a grpc_call_error value. + * Modified from the answer by Gus Goose to + * http://stackoverflow.com/questions/31794200. + */ +inline v8::Local<v8::Value> nanErrorWithCode(const char *msg, + grpc_call_error code) { + NanEscapableScope(); + v8::Local<v8::Object> err = NanError(msg).As<v8::Object>(); + err->Set(NanNew("code"), NanNew<v8::Uint32>(code)); + return NanEscapeScope(err); +} + v8::Handle<v8::Value> ParseMetadata(const grpc_metadata_array *metadata_array); class PersistentHolder { diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index 8e39644846da09226640f39f8f8600bad540eb5a..01217bce797500b30cf9e7905e253a1a79a421fc 100644 --- a/src/node/ext/server.cc +++ b/src/node/ext/server.cc @@ -235,7 +235,7 @@ NAN_METHOD(Server::RequestCall) { new struct tag(new NanCallback(args[0].As<Function>()), ops.release(), shared_ptr<Resources>(nullptr))); if (error != GRPC_CALL_OK) { - return NanThrowError("requestCall failed", error); + return NanThrowError(nanErrorWithCode("requestCall failed", error)); } CompletionQueueAsyncWorker::Next(); NanReturnUndefined();