Skip to content

Commit 31e233c

Browse files
Add getChildProcessErrorMessage
1 parent 23641a0 commit 31e233c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "./distribution";
2020
import {
2121
assertNever,
22+
getChildProcessErrorMessage,
2223
getErrorMessage,
2324
getErrorStack,
2425
} from "../common/helpers-pure";
@@ -1643,7 +1644,7 @@ export async function runCodeQlCliCommand(
16431644
return result.stdout;
16441645
} catch (err) {
16451646
throw new Error(
1646-
`${description} failed: ${(err as any).stderr || getErrorMessage(err)}`,
1647+
`${description} failed: ${getChildProcessErrorMessage(err)}`,
16471648
);
16481649
}
16491650
}

extensions/ql-vscode/src/common/helpers-pure.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,26 @@ export function asError(e: unknown): Error {
6767

6868
return e instanceof Error ? e : new Error(String(e));
6969
}
70+
71+
/**
72+
* Get error message when the error may have come from a method from the `child_process` module.
73+
*/
74+
export function getChildProcessErrorMessage(e: unknown): string {
75+
return isChildProcessError(e) ? e.stderr : getErrorMessage(e);
76+
}
77+
78+
/**
79+
* Error thrown from methods from the `child_process` module.
80+
*/
81+
interface ChildProcessError {
82+
readonly stderr: string;
83+
}
84+
85+
function isChildProcessError(e: unknown): e is ChildProcessError {
86+
return (
87+
typeof e === "object" &&
88+
e !== null &&
89+
"stderr" in e &&
90+
typeof e.stderr === "string"
91+
);
92+
}

0 commit comments

Comments
 (0)