Merge pull request #2827 from github/robertbrignull/any/stderr

Avoid use of "as any" during error handling
This commit is contained in:
Robert
2023-09-19 09:52:51 +01:00
committed by GitHub
3 changed files with 28 additions and 10 deletions

View File

@@ -19,6 +19,7 @@ import {
} from "./distribution";
import {
assertNever,
getChildProcessErrorMessage,
getErrorMessage,
getErrorStack,
} from "../common/helpers-pure";
@@ -547,9 +548,7 @@ export class CodeQLCliServer implements Disposable {
yield JSON.parse(event) as EventType;
} catch (err) {
throw new Error(
`Parsing output of ${description} failed: ${
(err as any).stderr || getErrorMessage(err)
}`,
`Parsing output of ${description} failed: ${getErrorMessage(err)}`,
);
}
}
@@ -647,9 +646,7 @@ export class CodeQLCliServer implements Disposable {
return JSON.parse(result) as OutputType;
} catch (err) {
throw new Error(
`Parsing output of ${description} failed: ${
(err as any).stderr || getErrorMessage(err)
}`,
`Parsing output of ${description} failed: ${getErrorMessage(err)}`,
);
}
}
@@ -1647,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"
);
}

View File

@@ -54,9 +54,7 @@ export async function sarifParser(
});
} catch (e) {
throw new Error(
`Parsing output of interpretation failed: ${
(e as any).stderr || getErrorMessage(e)
}`,
`Parsing output of interpretation failed: ${getErrorMessage(e)}`,
);
}
}