From b4db089a6b79ae60dec9fcd0ba2d788a494c8d8f Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 24 Feb 2020 21:13:10 +0200 Subject: add error handling to fetchArtifactReleaseInfo(), throw Error when no artifact found --- .../installation/fetch_artifact_release_info.ts | 56 ++++++++++++++-------- 1 file changed, 37 insertions(+), 19 deletions(-) (limited to 'editors/code/src/installation') 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"; const GITHUB_API_ENDPOINT_URL = "https://api.github.com"; - /** - * Fetches the release with `releaseTag` (or just latest release when not specified) - * from GitHub `repo` and returns metadata about `artifactFileName` shipped with - * this release or `null` if no such artifact was published. + * Fetches the release with `releaseTag` from GitHub `repo` and + * returns metadata about `artifactFileName` shipped with + * this release. + * + * @throws Error upon network failure or if no such repository, release, or artifact exists. */ export async function fetchArtifactReleaseInfo( - repo: GithubRepo, artifactFileName: string, releaseTag?: string -): Promise { + repo: GithubRepo, + artifactFileName: string, + releaseTag: string +): Promise { const repoOwner = encodeURIComponent(repo.owner); const repoName = encodeURIComponent(repo.name); - const apiEndpointPath = releaseTag - ? `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}` - : `/repos/${repoOwner}/${repoName}/releases/latest`; + const apiEndpointPath = `/repos/${repoOwner}/${repoName}/releases/tags/${releaseTag}`; const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath; - // We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`) - log.debug("Issuing request for released artifacts metadata to", requestUrl); - // FIXME: handle non-ok response - const response: GithubRelease = await fetch(requestUrl, { - headers: { Accept: "application/vnd.github.v3+json" } - }) - .then(res => res.json()); + const response = await fetch(requestUrl, { headers: { Accept: "application/vnd.github.v3+json" } }); + + if (!response.ok) { + log.error("Error fetching artifact release info", { + requestUrl, + releaseTag, + artifactFileName, + response: { + headers: response.headers, + status: response.status, + body: await response.text(), + } + }); + + throw new Error( + `Got response ${response.status} when trying to fetch ` + + `"${artifactFileName}" artifact release info for ${releaseTag} release` + ); + } + + // We skip runtime type checks for simplicity (here we cast from `any` to `GithubRelease`) + const release: GithubRelease = await response.json(); - const artifact = response.assets.find(artifact => artifact.name === artifactFileName); + const artifact = release.assets.find(artifact => artifact.name === artifactFileName); - if (!artifact) return null; + if (!artifact) throw new Error( + `Artifact ${artifactFileName} was not found in ${release.name} release!` + ); return { - releaseName: response.name, + releaseName: release.name, downloadUrl: artifact.browser_download_url }; -- cgit v1.2.3 From af57251c311220c2f93a75a6105e6472ffc8ebaa Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 24 Feb 2020 21:23:48 +0200 Subject: vscode: remove type assertion --- editors/code/src/installation/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/installation') diff --git a/editors/code/src/installation/server.ts b/editors/code/src/installation/server.ts index 9de257dd5..cb5e56844 100644 --- a/editors/code/src/installation/server.ts +++ b/editors/code/src/installation/server.ts @@ -63,7 +63,7 @@ export async function ensureServerBinary(source: null | BinarySource): Promise { try { - const releaseInfo = (await fetchArtifactReleaseInfo(source.repo, source.file, source.version))!; + const releaseInfo = await fetchArtifactReleaseInfo(source.repo, source.file, source.version); await downloadArtifact(releaseInfo, source.file, source.dir, "language server"); await setServerVersion(source.storage, releaseInfo.releaseName); -- cgit v1.2.3 From 6ec4a7db42be5980f7a4b20f349cb10709dbf71b Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 01:58:43 +0200 Subject: vscode: wrap non-single-line if body with curlies as per matklad --- editors/code/src/installation/fetch_artifact_release_info.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'editors/code/src/installation') diff --git a/editors/code/src/installation/fetch_artifact_release_info.ts b/editors/code/src/installation/fetch_artifact_release_info.ts index 5e201afb0..b1b5a3485 100644 --- a/editors/code/src/installation/fetch_artifact_release_info.ts +++ b/editors/code/src/installation/fetch_artifact_release_info.ts @@ -51,9 +51,11 @@ export async function fetchArtifactReleaseInfo( const artifact = release.assets.find(artifact => artifact.name === artifactFileName); - if (!artifact) throw new Error( - `Artifact ${artifactFileName} was not found in ${release.name} release!` - ); + if (!artifact) { + throw new Error( + `Artifact ${artifactFileName} was not found in ${release.name} release!` + ); + } return { releaseName: release.name, -- cgit v1.2.3