Skip to content

Commit 40feac7

Browse files
authored
Merge pull request #2232 from murgatroid99/grpc-js_optional_caller_stack
grpc-js: Defer evaluating caller stack until an error
2 parents e581aad + 5b42e99 commit 40feac7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

packages/grpc-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grpc/grpc-js",
3-
"version": "1.7.0",
3+
"version": "1.7.1",
44
"description": "gRPC Library for Node - pure JS implementation",
55
"homepage": "https://grpc.io/",
66
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",

packages/grpc-js/src/client.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ export type ClientOptions = Partial<ChannelOptions> & {
108108
callInvocationTransformer?: CallInvocationTransformer;
109109
};
110110

111+
function getErrorStackString(error: Error): string {
112+
return error.stack!.split('\n').slice(1).join('\n');
113+
}
114+
111115
/**
112116
* A generic gRPC client. Primarily useful as a base class for all generated
113117
* clients.
@@ -321,7 +325,7 @@ export class Client {
321325
}
322326
let responseMessage: ResponseType | null = null;
323327
let receivedStatus = false;
324-
const callerStack = (new Error().stack!).split('\n').slice(1).join('\n');
328+
const callerStackError = new Error();
325329
call.start(callProperties.metadata, {
326330
onReceiveMetadata: (metadata) => {
327331
emitter.emit('metadata', metadata);
@@ -340,6 +344,7 @@ export class Client {
340344
receivedStatus = true;
341345
if (status.code === Status.OK) {
342346
if (responseMessage === null) {
347+
const callerStack = getErrorStackString(callerStackError);
343348
callProperties.callback!(callErrorFromStatus({
344349
code: Status.INTERNAL,
345350
details: 'No message received',
@@ -349,6 +354,7 @@ export class Client {
349354
callProperties.callback!(null, responseMessage);
350355
}
351356
} else {
357+
const callerStack = getErrorStackString(callerStackError);
352358
callProperties.callback!(callErrorFromStatus(status, callerStack));
353359
}
354360
emitter.emit('status', status);
@@ -447,7 +453,7 @@ export class Client {
447453
}
448454
let responseMessage: ResponseType | null = null;
449455
let receivedStatus = false;
450-
const callerStack = (new Error().stack!).split('\n').slice(1).join('\n');
456+
const callerStackError = new Error();
451457
call.start(callProperties.metadata, {
452458
onReceiveMetadata: (metadata) => {
453459
emitter.emit('metadata', metadata);
@@ -466,6 +472,7 @@ export class Client {
466472
receivedStatus = true;
467473
if (status.code === Status.OK) {
468474
if (responseMessage === null) {
475+
const callerStack = getErrorStackString(callerStackError);
469476
callProperties.callback!(callErrorFromStatus({
470477
code: Status.INTERNAL,
471478
details: 'No message received',
@@ -475,6 +482,7 @@ export class Client {
475482
callProperties.callback!(null, responseMessage);
476483
}
477484
} else {
485+
const callerStack = getErrorStackString(callerStackError);
478486
callProperties.callback!(callErrorFromStatus(status, callerStack));
479487
}
480488
emitter.emit('status', status);
@@ -577,7 +585,7 @@ export class Client {
577585
call.setCredentials(callProperties.callOptions.credentials);
578586
}
579587
let receivedStatus = false;
580-
const callerStack = (new Error().stack!).split('\n').slice(1).join('\n');
588+
const callerStackError = new Error();
581589
call.start(callProperties.metadata, {
582590
onReceiveMetadata(metadata: Metadata) {
583591
stream.emit('metadata', metadata);
@@ -593,6 +601,7 @@ export class Client {
593601
receivedStatus = true;
594602
stream.push(null);
595603
if (status.code !== Status.OK) {
604+
const callerStack = getErrorStackString(callerStackError);
596605
stream.emit('error', callErrorFromStatus(status, callerStack));
597606
}
598607
stream.emit('status', status);
@@ -675,7 +684,7 @@ export class Client {
675684
call.setCredentials(callProperties.callOptions.credentials);
676685
}
677686
let receivedStatus = false;
678-
const callerStack = (new Error().stack!).split('\n').slice(1).join('\n');
687+
const callerStackError = new Error();
679688
call.start(callProperties.metadata, {
680689
onReceiveMetadata(metadata: Metadata) {
681690
stream.emit('metadata', metadata);
@@ -690,6 +699,7 @@ export class Client {
690699
receivedStatus = true;
691700
stream.push(null);
692701
if (status.code !== Status.OK) {
702+
const callerStack = getErrorStackString(callerStackError);
693703
stream.emit('error', callErrorFromStatus(status, callerStack));
694704
}
695705
stream.emit('status', status);

0 commit comments

Comments
 (0)