diff options
author | Veetaha <[email protected]> | 2020-02-24 19:13:10 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-02-24 19:13:10 +0000 |
commit | b4db089a6b79ae60dec9fcd0ba2d788a494c8d8f (patch) | |
tree | b463517f7793852fd90636543fde4a0a158666b4 /editors/code | |
parent | bd4ea87f7442541123e3bbd7e17bfecdfb3c18c6 (diff) |
add error handling to fetchArtifactReleaseInfo(), throw Error when no artifact found
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/installation/fetch_artifact_release_info.ts | 56 |
1 files changed, 37 insertions, 19 deletions
diff --git a/editors/code/src/installation/fetch_artifact_release_info.ts b/editors/code/src/installation/fetch_artifact_release_info.ts index 1b6fc8d48..5e201afb0 100644 --- a/editors/code/src/installation/fetch_artifact_release_info.ts +++ b/editors/code/src/installation/fetch_artifact_release_info.ts | |||
@@ -4,41 +4,59 @@ import { log } from "../util"; | |||
4 | 4 | ||
5 | const GITHUB_API_ENDPOINT_URL = "https://api.github.com"; | 5 | const GITHUB_API_ENDPOINT_URL = "https://api.github.com"; |
6 | 6 | ||
7 | |||
8 | /** | 7 | /** |
9 | * Fetches the release with `releaseTag` (or just latest release when not specified) | 8 | * Fetches the release with `releaseTag` from GitHub `repo` and |
10 | * from GitHub `repo` and returns metadata about `artifactFileName` shipped with | 9 | * returns metadata about `artifactFileName` shipped with |
11 | * this release or `null` if no such artifact was published. | 10 | * this release. |
11 | * | ||
12 | * @throws Error upon network failure or if no such repository, release, or artifact exists. | ||
12 | */ | 13 | */ |
13 | export async function fetchArtifactReleaseInfo( | 14 | export async function fetchArtifactReleaseInfo( |
14 | repo: GithubRepo, artifactFileName: string, releaseTag?: string | 15 | repo: GithubRepo, |
15 | ): Promise<null | ArtifactReleaseInfo> { | 16 | artifactFileName: string, |
17 | releaseTag: string | ||
18 | ): Promise<ArtifactReleaseInfo> { | ||
16 | 19 | ||
17 | const repoOwner = encodeURIComponent(repo.owner); | 20 | const repoOwner = encodeURIComponent(repo.owner); |
18 | const repoName = encodeURIComponent(repo.name); | 21 | const repoName = encodeURIComponent(repo.name); |
19 | 22 | ||
20 | const apiEndpointPath = releaseTag | 23 | const apiEndpointPath = `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}`; |
21 | ? `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}` | ||
22 | : `/repos/${repoOwner}/${repoName}/releases/latest`; | ||
23 | 24 | ||
24 | const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath; | 25 | const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath; |
25 | 26 | ||
26 | // We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`) | ||
27 | |||
28 | log.debug("Issuing request for released artifacts metadata to", requestUrl); | 27 | log.debug("Issuing request for released artifacts metadata to", requestUrl); |
29 | 28 | ||
30 | // FIXME: handle non-ok response | 29 | const response = await fetch(requestUrl, { headers: { Accept: "application/vnd.github.v3+json" } }); |
31 | const response: GithubRelease = await fetch(requestUrl, { | 30 | |
32 | headers: { Accept: "application/vnd.github.v3+json" } | 31 | if (!response.ok) { |
33 | }) | 32 | log.error("Error fetching artifact release info", { |
34 | .then(res => res.json()); | 33 | requestUrl, |
34 | releaseTag, | ||
35 | artifactFileName, | ||
36 | response: { | ||
37 | headers: response.headers, | ||
38 | status: response.status, | ||
39 | body: await response.text(), | ||
40 | } | ||
41 | }); | ||
42 | |||
43 | throw new Error( | ||
44 | `Got response ${response.status} when trying to fetch ` + | ||
45 | `"${artifactFileName}" artifact release info for ${releaseTag} release` | ||
46 | ); | ||
47 | } | ||
48 | |||
49 | // We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`) | ||
50 | const release: GithubRelease = await response.json(); | ||
35 | 51 | ||
36 | const artifact = response.assets.find(artifact => artifact.name === artifactFileName); | 52 | const artifact = release.assets.find(artifact => artifact.name === artifactFileName); |
37 | 53 | ||
38 | if (!artifact) return null; | 54 | if (!artifact) throw new Error( |
55 | `Artifact ${artifactFileName} was not found in ${release.name} release!` | ||
56 | ); | ||
39 | 57 | ||
40 | return { | 58 | return { |
41 | releaseName: response.name, | 59 | releaseName: release.name, |
42 | downloadUrl: artifact.browser_download_url | 60 | downloadUrl: artifact.browser_download_url |
43 | }; | 61 | }; |
44 | 62 | ||