Skip to content

Commit d3df14f

Browse files
Merge pull request #2827 from github/robertbrignull/any/stderr
Avoid use of "as any" during error handling
2 parents 209edf8 + 31e233c commit d3df14f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

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

Lines changed: 4 additions & 7 deletions
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";
@@ -547,9 +548,7 @@ export class CodeQLCliServer implements Disposable {
547548
yield JSON.parse(event) as EventType;
548549
} catch (err) {
549550
throw new Error(
550-
`Parsing output of ${description} failed: ${
551-
(err as any).stderr || getErrorMessage(err)
552-
}`,
551+
`Parsing output of ${description} failed: ${getErrorMessage(err)}`,
553552
);
554553
}
555554
}
@@ -647,9 +646,7 @@ export class CodeQLCliServer implements Disposable {
647646
return JSON.parse(result) as OutputType;
648647
} catch (err) {
649648
throw new Error(
650-
`Parsing output of ${description} failed: ${
651-
(err as any).stderr || getErrorMessage(err)
652-
}`,
649+
`Parsing output of ${description} failed: ${getErrorMessage(err)}`,
653650
);
654651
}
655652
}
@@ -1647,7 +1644,7 @@ export async function runCodeQlCliCommand(
16471644
return result.stdout;
16481645
} catch (err) {
16491646
throw new Error(
1650-
`${description} failed: ${(err as any).stderr || getErrorMessage(err)}`,
1647+
`${description} failed: ${getChildProcessErrorMessage(err)}`,
16511648
);
16521649
}
16531650
}

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+
}

extensions/ql-vscode/src/common/sarif-parser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ export async function sarifParser(
5454
});
5555
} catch (e) {
5656
throw new Error(
57-
`Parsing output of interpretation failed: ${
58-
(e as any).stderr || getErrorMessage(e)
59-
}`,
57+
`Parsing output of interpretation failed: ${getErrorMessage(e)}`,
6058
);
6159
}
6260
}

0 commit comments

Comments
 (0)