Skip to content

Commit 555643d

Browse files
author
Cedric Kassen
committed
try catch promise rejection and sendError
1 parent 14b18a4 commit 555643d

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

packages/grpc-js/src/server.ts

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,29 +1183,33 @@ async function handleUnary<RequestType, ResponseType>(
11831183
metadata: Metadata,
11841184
encoding: string
11851185
): Promise<void> {
1186-
const request = await call.receiveUnaryMessage(encoding);
1186+
try {
1187+
const request = await call.receiveUnaryMessage(encoding);
11871188

1188-
if (request === undefined || call.cancelled) {
1189-
return;
1190-
}
1189+
if (request === undefined || call.cancelled) {
1190+
return;
1191+
}
11911192

1192-
const emitter = new ServerUnaryCallImpl<RequestType, ResponseType>(
1193-
call,
1194-
metadata,
1195-
request
1196-
);
1193+
const emitter = new ServerUnaryCallImpl<RequestType, ResponseType>(
1194+
call,
1195+
metadata,
1196+
request
1197+
);
11971198

1198-
handler.func(
1199-
emitter,
1200-
(
1201-
err: ServerErrorResponse | ServerStatusResponse | null,
1202-
value?: ResponseType | null,
1203-
trailer?: Metadata,
1204-
flags?: number
1205-
) => {
1206-
call.sendUnaryMessage(err, value, trailer, flags);
1207-
}
1208-
);
1199+
handler.func(
1200+
emitter,
1201+
(
1202+
err: ServerErrorResponse | ServerStatusResponse | null,
1203+
value?: ResponseType | null,
1204+
trailer?: Metadata,
1205+
flags?: number
1206+
) => {
1207+
call.sendUnaryMessage(err, value, trailer, flags);
1208+
}
1209+
);
1210+
} catch (err) {
1211+
call.sendError(err as ServerErrorResponse)
1212+
}
12091213
}
12101214

12111215
function handleClientStreaming<RequestType, ResponseType>(
@@ -1245,20 +1249,24 @@ async function handleServerStreaming<RequestType, ResponseType>(
12451249
metadata: Metadata,
12461250
encoding: string
12471251
): Promise<void> {
1248-
const request = await call.receiveUnaryMessage(encoding);
1252+
try {
1253+
const request = await call.receiveUnaryMessage(encoding);
12491254

1250-
if (request === undefined || call.cancelled) {
1251-
return;
1252-
}
1255+
if (request === undefined || call.cancelled) {
1256+
return;
1257+
}
12531258

1254-
const stream = new ServerWritableStreamImpl<RequestType, ResponseType>(
1255-
call,
1256-
metadata,
1257-
handler.serialize,
1258-
request
1259-
);
1259+
const stream = new ServerWritableStreamImpl<RequestType, ResponseType>(
1260+
call,
1261+
metadata,
1262+
handler.serialize,
1263+
request
1264+
);
12601265

1261-
handler.func(stream);
1266+
handler.func(stream);
1267+
} catch (err) {
1268+
call.sendError(err as ServerErrorResponse)
1269+
}
12621270
}
12631271

12641272
function handleBidiStreaming<RequestType, ResponseType>(

0 commit comments

Comments
 (0)