Skip to content

Commit 071544a

Browse files
committed
Move HTTPError out of util.ts
1 parent 8bdea82 commit 071544a

4 files changed

Lines changed: 56 additions & 49 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/errors/index.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
import * as json from "../json";
2+
13
/**
24
* An Error class that indicates an error that occurred due to
35
* a misconfiguration of the action or the CodeQL CLI.
46
*/
57
export class ConfigurationError extends Error {}
8+
9+
/** Minimal representation of errors resulting from HTTP requests. */
10+
export class HTTPError extends Error {
11+
public status: number;
12+
13+
constructor(message: string, status: number) {
14+
super(message);
15+
this.status = status;
16+
}
17+
}
18+
19+
/**
20+
* Tries to convert {@link arg} to a {@link HTTPError}, but returns `undefined` if
21+
* {@link arg} is not like a {@link HTTPError}.
22+
*/
23+
export function asHTTPError(arg: any): HTTPError | undefined {
24+
if (!json.isObject<any>(arg) || !json.isString(arg.message)) {
25+
return undefined;
26+
}
27+
if (Number.isInteger(arg.status)) {
28+
return new HTTPError(arg.message, arg.status as number);
29+
}
30+
// See https://github.com/actions/toolkit/blob/acb230b99a46ed33a3f04a758cd68b47b9a82908/packages/tool-cache/src/tool-cache.ts#L19
31+
if (Number.isInteger(arg.httpStatusCode)) {
32+
return new HTTPError(arg.message, arg.httpStatusCode as number);
33+
}
34+
return undefined;
35+
}

‎src/tools-download.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { https } from "follow-redirects";
1010
import nock from "nock";
1111
import * as sinon from "sinon";
1212

13+
import * as errors from "./errors";
1314
import { getRunnerLogger } from "./logging";
1415
import * as tar from "./tar";
1516
import { setupTests } from "./testing-utils";
@@ -164,7 +165,7 @@ test.serial(
164165
test.serial(
165166
"downloadAndExtract handles an unknown status as a non-HTTP error",
166167
async (t) => {
167-
const asHTTPError = sinon.spy(util, "asHTTPError");
168+
const asHTTPError = sinon.spy(errors, "asHTTPError");
168169
await util.withTmpDir(async (tmpDir) => {
169170
sinon.stub(process, "platform").value("linux");
170171
const archivePath = path.join(tmpDir, "codeql-bundle.tar.zst");

‎src/util.ts‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ import {
2424
type Env,
2525
} from "./environment";
2626
import { ConfigurationError } from "./errors";
27-
import * as json from "./json";
2827
import { Language } from "./languages";
2928
import { Logger } from "./logging";
3029

3130
// Re-export for backwards compatibility to avoid updating a lot of imports elsewhere.
32-
export { ConfigurationError } from "./errors";
31+
export { ConfigurationError, HTTPError, asHTTPError } from "./errors";
3332
export {
3433
getRequiredEnvParam,
3534
getOptionalEnvVar,
@@ -566,29 +565,6 @@ export function initializeEnvironment(version: string, env: Env = getEnv()) {
566565
env.export(EnvVar.VERSION, version);
567566
}
568567

569-
export class HTTPError extends Error {
570-
public status: number;
571-
572-
constructor(message: string, status: number) {
573-
super(message);
574-
this.status = status;
575-
}
576-
}
577-
578-
export function asHTTPError(arg: any): HTTPError | undefined {
579-
if (!json.isObject<any>(arg) || !json.isString(arg.message)) {
580-
return undefined;
581-
}
582-
if (Number.isInteger(arg.status)) {
583-
return new HTTPError(arg.message, arg.status as number);
584-
}
585-
// See https://github.com/actions/toolkit/blob/acb230b99a46ed33a3f04a758cd68b47b9a82908/packages/tool-cache/src/tool-cache.ts#L19
586-
if (Number.isInteger(arg.httpStatusCode)) {
587-
return new HTTPError(arg.message, arg.httpStatusCode as number);
588-
}
589-
return undefined;
590-
}
591-
592568
export async function codeQlVersionAtLeast(
593569
codeql: CodeQL,
594570
requiredVersion: string,

0 commit comments

Comments
 (0)