Skip to content

Commit e80d9cf

Browse files
authored
Merge pull request #2262 from murgatroid99/grpc-js_typescript_upgrade_2
grpc-js: Update to newest typescript compiler
2 parents a5ff2fe + 8f33dc7 commit e80d9cf

File tree

7 files changed

+78
-27
lines changed

7 files changed

+78
-27
lines changed

packages/grpc-js/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
"devDependencies": {
1818
"@types/gulp": "^4.0.6",
1919
"@types/gulp-mocha": "0.0.32",
20-
"@types/lodash": "4.14.186",
20+
"@types/lodash": "^4.14.186",
2121
"@types/mocha": "^5.2.6",
2222
"@types/ncp": "^2.0.1",
2323
"@types/pify": "^3.0.2",
2424
"@types/semver": "^7.3.9",
2525
"clang-format": "^1.0.55",
2626
"execa": "^2.0.3",
27-
"gts": "^2.0.0",
27+
"gts": "^3.1.1",
2828
"gulp": "^4.0.2",
2929
"gulp-mocha": "^6.0.0",
3030
"lodash": "^4.17.4",
@@ -35,7 +35,7 @@
3535
"rimraf": "^3.0.2",
3636
"semver": "^7.3.5",
3737
"ts-node": "^8.3.0",
38-
"typescript": "^3.7.2"
38+
"typescript": "^4.8.4"
3939
},
4040
"contributors": [
4141
{

packages/grpc-js/src/call-credentials.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ export abstract class CallCredentials {
115115
reject(err);
116116
return;
117117
}
118+
if (!headers) {
119+
reject(new Error('Headers not set by metadata plugin'));
120+
return;
121+
}
118122
resolve(headers);
119123
}
120124
);

packages/grpc-js/src/client-interceptors.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Channel } from './channel';
3434
import { CallOptions } from './client';
3535
import { CallCredentials } from './call-credentials';
3636
import { ClientMethodDefinition } from './make-client';
37+
import { getErrorMessage } from './error';
3738

3839
/**
3940
* Error class associated with passing both interceptors and interceptor
@@ -374,7 +375,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
374375
} catch (e) {
375376
this.call.cancelWithStatus(
376377
Status.INTERNAL,
377-
`Request message serialization failure: ${e.message}`
378+
`Request message serialization failure: ${getErrorMessage(e)}`
378379
);
379380
return;
380381
}
@@ -401,7 +402,7 @@ class BaseInterceptingCall implements InterceptingCallInterface {
401402
} catch (e) {
402403
readError = {
403404
code: Status.INTERNAL,
404-
details: `Response message parsing error: ${e.message}`,
405+
details: `Response message parsing error: ${getErrorMessage(e)}`,
405406
metadata: new Metadata(),
406407
};
407408
this.call.cancelWithStatus(readError.code, readError.details);

packages/grpc-js/src/error.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2022 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
export function getErrorMessage(error: unknown): string {
19+
if (error instanceof Error) {
20+
return error.message;
21+
} else {
22+
return String(error);
23+
}
24+
}
25+
26+
export function getErrorCode(error: unknown): number | null {
27+
if (
28+
typeof error === 'object' &&
29+
error !== null &&
30+
'code' in error &&
31+
typeof (error as Record<string, unknown>).code === 'number'
32+
) {
33+
return (error as Record<string, number>).code;
34+
} else {
35+
return null;
36+
}
37+
}

packages/grpc-js/src/metadata.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import * as http2 from 'http2';
1919
import { log } from './logging';
2020
import { LogVerbosity } from './constants';
21+
import { getErrorMessage } from './error';
2122
const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
2223
const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
2324

@@ -285,7 +286,7 @@ export class Metadata {
285286
}
286287
}
287288
} catch (error) {
288-
const message = `Failed to add metadata entry ${key}: ${values}. ${error.message}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
289+
const message = `Failed to add metadata entry ${key}: ${values}. ${getErrorMessage(error)}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
289290
log(LogVerbosity.ERROR, message);
290291
}
291292
}

packages/grpc-js/src/server-call.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { ChannelOptions } from './channel-options';
3535
import * as logging from './logging';
3636
import { StatusObject, PartialStatusObject } from './call-interface';
3737
import { Deadline } from './deadline';
38+
import { getErrorCode, getErrorMessage } from './error';
3839

3940
const TRACER_NAME = 'server_call';
4041
const unzip = promisify(zlib.unzip);
@@ -232,8 +233,10 @@ export class ServerWritableStreamImpl<RequestType, ResponseType>
232233
return;
233234
}
234235
} catch (err) {
235-
err.code = Status.INTERNAL;
236-
this.emit('error', err);
236+
this.emit('error', {
237+
details: getErrorMessage(err),
238+
code: Status.INTERNAL
239+
});
237240
}
238241

239242
callback();
@@ -630,8 +633,10 @@ export class Http2ServerCallStream<
630633
try {
631634
next(null, this.deserializeMessage(buffer));
632635
} catch (err) {
633-
err.code = Status.INTERNAL;
634-
next(err);
636+
next({
637+
details: getErrorMessage(err),
638+
code: Status.INTERNAL
639+
});
635640
}
636641
}
637642

@@ -679,8 +684,10 @@ export class Http2ServerCallStream<
679684
this.write(response);
680685
this.sendStatus({ code: Status.OK, details: 'OK', metadata });
681686
} catch (err) {
682-
err.code = Status.INTERNAL;
683-
this.sendError(err);
687+
this.sendError({
688+
details: getErrorMessage(err),
689+
code: Status.INTERNAL
690+
});
684691
}
685692
}
686693

@@ -909,21 +916,15 @@ export class Http2ServerCallStream<
909916
} catch (error) {
910917
// Ignore any remaining messages when errors occur.
911918
this.bufferedMessages.length = 0;
912-
913-
if (
914-
!(
915-
'code' in error &&
916-
typeof error.code === 'number' &&
917-
Number.isInteger(error.code) &&
918-
error.code >= Status.OK &&
919-
error.code <= Status.UNAUTHENTICATED
920-
)
921-
) {
922-
// The error code is not a valid gRPC code so its being overwritten.
923-
error.code = Status.INTERNAL;
919+
let code = getErrorCode(error);
920+
if (code === null || code < Status.OK || code > Status.UNAUTHENTICATED) {
921+
code = Status.INTERNAL
924922
}
925923

926-
readable.emit('error', error);
924+
readable.emit('error', {
925+
details: getErrorMessage(error),
926+
code: code
927+
});
927928
}
928929

929930
this.isPushPending = false;

packages/grpc-js/src/server.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
import { parseUri } from './uri-parser';
6262
import { ChannelzCallTracker, ChannelzChildrenTracker, ChannelzTrace, registerChannelzServer, registerChannelzSocket, ServerInfo, ServerRef, SocketInfo, SocketRef, TlsInfo, unregisterChannelzRef } from './channelz';
6363
import { CipherNameAndProtocol, TLSSocket } from 'tls';
64+
import { getErrorCode, getErrorMessage } from './error';
6465

6566
const {
6667
HTTP2_HEADER_PATH
@@ -814,7 +815,10 @@ export class Server {
814815
try {
815816
handler = this._retrieveHandler(headers)
816817
} catch (err) {
817-
this._respondWithError(err, stream, channelzSessionInfo)
818+
this._respondWithError({
819+
details: getErrorMessage(err),
820+
code: getErrorCode(err) ?? undefined
821+
}, stream, channelzSessionInfo)
818822
return
819823
}
820824

@@ -866,7 +870,10 @@ export class Server {
866870
try {
867871
handler = this._retrieveHandler(headers)
868872
} catch (err) {
869-
this._respondWithError(err, stream, null)
873+
this._respondWithError({
874+
details: getErrorMessage(err),
875+
code: getErrorCode(err) ?? undefined
876+
}, stream, null)
870877
return
871878
}
872879

0 commit comments

Comments
 (0)