Introduce asString to call toString() safely on process stdout/stderr
This commit is contained in:
@@ -19,6 +19,7 @@ import type { CliConfig } from "../config";
|
||||
import type { DistributionProvider } from "./distribution";
|
||||
import { FindDistributionResultKind } from "./distribution";
|
||||
import {
|
||||
asString,
|
||||
assertNever,
|
||||
getErrorMessage,
|
||||
getErrorStack,
|
||||
@@ -1605,8 +1606,8 @@ export function spawnServer(
|
||||
command: string[],
|
||||
commandArgs: string[],
|
||||
logger: Logger,
|
||||
stderrListener: (data: any) => void,
|
||||
stdoutListener?: (data: any) => void,
|
||||
stderrListener: (data: unknown) => void,
|
||||
stdoutListener?: (data: unknown) => void,
|
||||
progressReporter?: ProgressReporter,
|
||||
): ChildProcessWithoutNullStreams {
|
||||
// Enable verbose logging.
|
||||
@@ -1626,7 +1627,7 @@ export function spawnServer(
|
||||
);
|
||||
}
|
||||
|
||||
let lastStdout: any = undefined;
|
||||
let lastStdout: unknown = undefined;
|
||||
child.stdout!.on("data", (data) => {
|
||||
lastStdout = data;
|
||||
});
|
||||
@@ -1643,7 +1644,7 @@ export function spawnServer(
|
||||
// If the process exited abnormally, log the last stdout message,
|
||||
// It may be from the jvm.
|
||||
if (code !== 0 && lastStdout !== undefined) {
|
||||
void logger.log(`Last stdout was "${lastStdout.toString()}"`);
|
||||
void logger.log(`Last stdout was "${asString(lastStdout)}"`);
|
||||
}
|
||||
});
|
||||
child.stderr!.on("data", stderrListener);
|
||||
|
||||
@@ -68,6 +68,20 @@ export function asError(e: unknown): Error {
|
||||
return e instanceof Error ? e : new Error(String(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a unknown value to a string, using `toString` if possible,
|
||||
* or returning the human-readable strings "null" or "undefined".
|
||||
*/
|
||||
export function asString(x: unknown): string {
|
||||
if (x === null) {
|
||||
return "null";
|
||||
} else if (x === undefined) {
|
||||
return "undefined";
|
||||
} else {
|
||||
return x.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get error message when the error may have come from a method from the `child_process` module.
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ import { LanguageClient, NotificationType } from "vscode-languageclient/node";
|
||||
import { shouldDebugLanguageServer, spawnServer } from "../codeql-cli/cli";
|
||||
import type { QueryServerConfig } from "../config";
|
||||
import { languageServerLogger } from "../common/logging/vscode";
|
||||
import { asString } from "../common/helpers-pure";
|
||||
|
||||
/**
|
||||
* Managing the language client and corresponding server process for CodeQL.
|
||||
@@ -74,9 +75,9 @@ async function spawnLanguageServer(
|
||||
args,
|
||||
languageServerLogger,
|
||||
(data) =>
|
||||
languageServerLogger.log(data.toString(), { trailingNewline: false }),
|
||||
languageServerLogger.log(asString(data), { trailingNewline: false }),
|
||||
(data) =>
|
||||
languageServerLogger.log(data.toString(), { trailingNewline: false }),
|
||||
languageServerLogger.log(asString(data), { trailingNewline: false }),
|
||||
progressReporter,
|
||||
);
|
||||
return { writer: child.stdin!, reader: child.stdout! };
|
||||
|
||||
@@ -18,6 +18,7 @@ import type { ProgressCallback, ProgressTask } from "../common/vscode/progress";
|
||||
import { withProgress } from "../common/vscode/progress";
|
||||
import { ServerProcess } from "./server-process";
|
||||
import type { App } from "../common/app";
|
||||
import { asString } from "../common/helpers-pure";
|
||||
|
||||
type ServerOpts = {
|
||||
logger: Logger;
|
||||
@@ -214,7 +215,7 @@ export class QueryServerClient extends DisposableObject {
|
||||
args,
|
||||
this.logger,
|
||||
(data) =>
|
||||
this.activeQueryLogger.log(data.toString(), {
|
||||
this.activeQueryLogger.log(asString(data), {
|
||||
trailingNewline: false,
|
||||
}),
|
||||
undefined, // no listener for stdout
|
||||
|
||||
Reference in New Issue
Block a user