Skip to content

Commit 17cb68c

Browse files
authored
send error if obtaining of types-registry package failed (microsoft#14573)
1 parent bdb6a8a commit 17cb68c

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

src/server/protocol.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,17 @@ namespace ts.server.protocol {
21272127
payload: any;
21282128
}
21292129

2130+
export type TypesInstallerInitializationFailedEventName = "typesInstallerInitializationFailed";
2131+
2132+
export interface TypesInstallerInitializationFailedEvent extends Event {
2133+
event: TypesInstallerInitializationFailedEventName;
2134+
body: TypesInstallerInitializationFailedEventBody;
2135+
}
2136+
2137+
export interface TypesInstallerInitializationFailedEventBody {
2138+
message: string;
2139+
}
2140+
21302141
export type TypingsInstalledTelemetryEventName = "typingsInstalled";
21312142

21322143
export interface TypingsInstalledTelemetryEventBody extends TelemetryEventBody {

src/server/server.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,23 @@ namespace ts.server {
304304
});
305305
}
306306

307-
private handleMessage(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes) {
307+
private handleMessage(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse) {
308308
if (this.logger.hasLevel(LogLevel.verbose)) {
309309
this.logger.info(`Received response: ${JSON.stringify(response)}`);
310310
}
311311

312+
if (response.kind === EventInitializationFailed) {
313+
if (!this.eventSender) {
314+
return;
315+
}
316+
const body: protocol.TypesInstallerInitializationFailedEventBody = {
317+
message: response.message
318+
}
319+
const eventName: protocol.TypesInstallerInitializationFailedEventName = "typesInstallerInitializationFailed";
320+
this.eventSender.event(body, eventName);
321+
return;
322+
}
323+
312324
if (response.kind === EventBeginInstallTypes) {
313325
if (!this.eventSender) {
314326
return;

src/server/shared.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace ts.server {
55
export const ActionInvalidate: ActionInvalidate = "action::invalidate";
66
export const EventBeginInstallTypes: EventBeginInstallTypes = "event::beginInstallTypes";
77
export const EventEndInstallTypes: EventEndInstallTypes = "event::endInstallTypes";
8+
export const EventInitializationFailed: EventInitializationFailed = "event::initializationFailed";
89

910
export namespace Arguments {
1011
export const GlobalCacheLocation = "--globalTypingsCacheLocation";

src/server/types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ declare namespace ts.server {
4747
export type ActionInvalidate = "action::invalidate";
4848
export type EventBeginInstallTypes = "event::beginInstallTypes";
4949
export type EventEndInstallTypes = "event::endInstallTypes";
50+
export type EventInitializationFailed = "event::initializationFailed";
5051

5152
export interface TypingInstallerResponse {
52-
readonly kind: ActionSet | ActionInvalidate | EventBeginInstallTypes | EventEndInstallTypes;
53+
readonly kind: ActionSet | ActionInvalidate | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed;
54+
}
55+
56+
export interface InitializationFailedResponse extends TypingInstallerResponse {
57+
readonly kind: EventInitializationFailed;
58+
readonly message: string;
5359
}
5460

5561
export interface ProjectResponse extends TypingInstallerResponse {

src/server/typingsInstaller/nodeTypingsInstaller.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ namespace ts.server.typingsInstaller {
7474
private readonly npmPath: string;
7575
readonly typesRegistry: Map<void>;
7676

77+
private delayedInitializationError: InitializationFailedResponse;
78+
7779
constructor(globalTypingsCacheLocation: string, throttleLimit: number, log: Log) {
7880
super(
7981
sys,
@@ -99,13 +101,23 @@ namespace ts.server.typingsInstaller {
99101
if (this.log.isEnabled()) {
100102
this.log.writeLine(`Error updating ${TypesRegistryPackageName} package: ${(<Error>e).message}`);
101103
}
104+
// store error info to report it later when it is known that server is already listening to events from typings installer
105+
this.delayedInitializationError = {
106+
kind: "event::initializationFailed",
107+
message: (<Error>e).message
108+
};
102109
}
103110

104111
this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(globalTypingsCacheLocation), this.installTypingHost, this.log);
105112
}
106113

107114
listen() {
108115
process.on("message", (req: DiscoverTypings | CloseProject) => {
116+
if (this.delayedInitializationError) {
117+
// report initializationFailed error
118+
this.sendResponse(this.delayedInitializationError);
119+
this.delayedInitializationError = undefined;
120+
}
109121
switch (req.kind) {
110122
case "discover":
111123
this.install(req);
@@ -116,7 +128,7 @@ namespace ts.server.typingsInstaller {
116128
});
117129
}
118130

119-
protected sendResponse(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes) {
131+
protected sendResponse(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse) {
120132
if (this.log.isEnabled()) {
121133
this.log.writeLine(`Sending response: ${JSON.stringify(response)}`);
122134
}

0 commit comments

Comments
 (0)