diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc index 705c80ffc1d..1e76ea7194d 100644 --- a/src/node/ext/call.cc +++ b/src/node/ext/call.cc @@ -619,7 +619,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(); @@ -633,7 +633,7 @@ NAN_METHOD(Call::Cancel) { Call *call = ObjectWrap::Unwrap(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 6acda76197f..ef6e5fcd210 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 nanErrorWithCode(const char *msg, + grpc_call_error code) { + NanEscapableScope(); + v8::Local err = NanError(msg).As(); + err->Set(NanNew("code"), NanNew(code)); + return NanEscapeScope(err); +} + v8::Handle ParseMetadata(const grpc_metadata_array *metadata_array); class PersistentHolder { diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index 8e39644846d..01217bce797 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()), ops.release(), shared_ptr(nullptr))); if (error != GRPC_CALL_OK) { - return NanThrowError("requestCall failed", error); + return NanThrowError(nanErrorWithCode("requestCall failed", error)); } CompletionQueueAsyncWorker::Next(); NanReturnUndefined();