Skip to content

Commit

Permalink
Merge pull request #2827 from github/robertbrignull/any/stderr
Browse files Browse the repository at this point in the history
Avoid use of "as any" during error handling
  • Loading branch information
robertbrignull authored Sep 19, 2023
2 parents 209edf8 + 31e233c commit d3df14f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
11 changes: 4 additions & 7 deletions extensions/ql-vscode/src/codeql-cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "./distribution";
import {
assertNever,
getChildProcessErrorMessage,
getErrorMessage,
getErrorStack,
} from "../common/helpers-pure";
Expand Down Expand Up @@ -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)}`,
);
}
}
Expand Down Expand Up @@ -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)}`,
);
}
}
Expand Down Expand Up @@ -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)}`,
);
}
}
Expand Down
23 changes: 23 additions & 0 deletions extensions/ql-vscode/src/common/helpers-pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
4 changes: 1 addition & 3 deletions extensions/ql-vscode/src/common/sarif-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
);
}
}

0 comments on commit d3df14f

Please sign in to comment.