Add getChildProcessErrorMessage

This commit is contained in:
Robert
2023-09-18 16:34:20 +01:00
parent 23641a01d9
commit 31e233cc59
2 changed files with 25 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ import {
} from "./distribution";
import {
assertNever,
getChildProcessErrorMessage,
getErrorMessage,
getErrorStack,
} from "../common/helpers-pure";
@@ -1643,7 +1644,7 @@ export async function runCodeQlCliCommand(
return result.stdout;
} catch (err) {
throw new Error(
`${description} failed: ${(err as any).stderr || getErrorMessage(err)}`,
`${description} failed: ${getChildProcessErrorMessage(err)}`,
);
}
}

View File

@@ -67,3 +67,26 @@ export function asError(e: unknown): Error {
return e instanceof Error ? e : new Error(String(e));
}
/**
* Get error message when the error may have come from a method from the `child_process` module.
*/
export function getChildProcessErrorMessage(e: unknown): string {
return isChildProcessError(e) ? e.stderr : getErrorMessage(e);
}
/**
* Error thrown from methods from the `child_process` module.
*/
interface ChildProcessError {
readonly stderr: string;
}
function isChildProcessError(e: unknown): e is ChildProcessError {
return (
typeof e === "object" &&
e !== null &&
"stderr" in e &&
typeof e.stderr === "string"
);
}