|
| 1 | +import { asError, getErrorMessage } from "../common/helpers-pure"; |
| 2 | + |
| 3 | +// https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/exit-codes |
| 4 | +const EXIT_CODE_USER_ERROR = 2; |
| 5 | +const EXIT_CODE_CANCELLED = 98; |
| 6 | + |
| 7 | +export class ExitCodeError extends Error { |
| 8 | + constructor(public readonly exitCode: number | null) { |
| 9 | + super(`Process exited with code ${exitCode}`); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +export class CliError extends Error { |
| 14 | + constructor( |
| 15 | + message: string, |
| 16 | + public readonly stderr: string | undefined, |
| 17 | + public readonly cause: Error, |
| 18 | + public readonly commandDescription: string, |
| 19 | + public readonly commandArgs: string[], |
| 20 | + ) { |
| 21 | + super(message); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export function getCliError( |
| 26 | + e: unknown, |
| 27 | + stderr: string | undefined, |
| 28 | + commandDescription: string, |
| 29 | + commandArgs: string[], |
| 30 | +): CliError { |
| 31 | + const error = asError(e); |
| 32 | + |
| 33 | + if (!(error instanceof ExitCodeError) || !stderr) { |
| 34 | + return formatCliErrorFallback( |
| 35 | + error, |
| 36 | + stderr, |
| 37 | + commandDescription, |
| 38 | + commandArgs, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + switch (error.exitCode) { |
| 43 | + case EXIT_CODE_USER_ERROR: { |
| 44 | + // This is an error that we should try to format nicely |
| 45 | + const fatalErrorIndex = stderr.lastIndexOf("A fatal error occurred: "); |
| 46 | + if (fatalErrorIndex !== -1) { |
| 47 | + return new CliError( |
| 48 | + stderr.slice(fatalErrorIndex), |
| 49 | + stderr, |
| 50 | + error, |
| 51 | + commandDescription, |
| 52 | + commandArgs, |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + break; |
| 57 | + } |
| 58 | + case EXIT_CODE_CANCELLED: { |
| 59 | + const cancellationIndex = stderr.lastIndexOf( |
| 60 | + "Computation was cancelled: ", |
| 61 | + ); |
| 62 | + if (cancellationIndex !== -1) { |
| 63 | + return new CliError( |
| 64 | + stderr.slice(cancellationIndex), |
| 65 | + stderr, |
| 66 | + error, |
| 67 | + commandDescription, |
| 68 | + commandArgs, |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return formatCliErrorFallback(error, stderr, commandDescription, commandArgs); |
| 77 | +} |
| 78 | + |
| 79 | +function formatCliErrorFallback( |
| 80 | + error: Error, |
| 81 | + stderr: string | undefined, |
| 82 | + commandDescription: string, |
| 83 | + commandArgs: string[], |
| 84 | +): CliError { |
| 85 | + if (stderr) { |
| 86 | + return new CliError( |
| 87 | + stderr, |
| 88 | + undefined, |
| 89 | + error, |
| 90 | + commandDescription, |
| 91 | + commandArgs, |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return new CliError( |
| 96 | + getErrorMessage(error), |
| 97 | + undefined, |
| 98 | + error, |
| 99 | + commandDescription, |
| 100 | + commandArgs, |
| 101 | + ); |
| 102 | +} |
0 commit comments