Add better error handling
* ensure error appears when an invalid URL is entered * ensure error messages are understandable by users
This commit is contained in:
committed by
Andrew Eisenberg
parent
1c1dbc95c7
commit
16e09b7ae9
@@ -84,29 +84,32 @@ export async function promptImportLgtmDatabase(
|
|||||||
});
|
});
|
||||||
if (looksLikeLgtmUrl(lgtmUrl)) {
|
if (looksLikeLgtmUrl(lgtmUrl)) {
|
||||||
const databaseUrl = await convertToDatabaseUrl(lgtmUrl);
|
const databaseUrl = await convertToDatabaseUrl(lgtmUrl);
|
||||||
if (!databaseUrl) {
|
if (databaseUrl) {
|
||||||
return item;
|
const progressOptions: ProgressOptions = {
|
||||||
|
location: ProgressLocation.Notification,
|
||||||
|
title: "Adding database from LGTM",
|
||||||
|
cancellable: false,
|
||||||
|
};
|
||||||
|
await withProgress(
|
||||||
|
progressOptions,
|
||||||
|
async (progress) =>
|
||||||
|
(item = await databaseArchiveFetcher(
|
||||||
|
databaseUrl,
|
||||||
|
databasesManager,
|
||||||
|
storagePath,
|
||||||
|
progress
|
||||||
|
))
|
||||||
|
);
|
||||||
|
commands.executeCommand("codeQLDatabases.focus");
|
||||||
}
|
}
|
||||||
const progressOptions: ProgressOptions = {
|
} else {
|
||||||
location: ProgressLocation.Notification,
|
throw new Error(`Invalid LGTM URL: ${lgtmUrl}`);
|
||||||
title: "Adding database from LGTM",
|
}
|
||||||
cancellable: false,
|
if (item) {
|
||||||
};
|
showAndLogInformationMessage(
|
||||||
await withProgress(
|
"Database downloaded and imported successfully."
|
||||||
progressOptions,
|
);
|
||||||
async (progress) =>
|
|
||||||
(item = await databaseArchiveFetcher(
|
|
||||||
databaseUrl,
|
|
||||||
databasesManager,
|
|
||||||
storagePath,
|
|
||||||
progress
|
|
||||||
))
|
|
||||||
);
|
|
||||||
commands.executeCommand("codeQLDatabases.focus");
|
|
||||||
}
|
}
|
||||||
showAndLogInformationMessage(
|
|
||||||
"Database downloaded and imported successfully."
|
|
||||||
);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
showAndLogErrorMessage(e.message);
|
showAndLogErrorMessage(e.message);
|
||||||
}
|
}
|
||||||
@@ -145,9 +148,11 @@ export async function importArchiveDatabase(
|
|||||||
);
|
);
|
||||||
commands.executeCommand("codeQLDatabases.focus");
|
commands.executeCommand("codeQLDatabases.focus");
|
||||||
|
|
||||||
showAndLogInformationMessage(
|
if (item) {
|
||||||
"Database unzipped and imported successfully."
|
showAndLogInformationMessage(
|
||||||
);
|
"Database unzipped and imported successfully."
|
||||||
|
);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
showAndLogErrorMessage(e.message);
|
showAndLogErrorMessage(e.message);
|
||||||
}
|
}
|
||||||
@@ -323,39 +328,51 @@ function looksLikeLgtmUrl(lgtmUrl: string | undefined): lgtmUrl is string {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uri = Uri.parse(lgtmUrl, true);
|
try {
|
||||||
if (uri.scheme !== "https") {
|
const uri = Uri.parse(lgtmUrl, true);
|
||||||
|
if (uri.scheme !== "https") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (uri.authority !== "lgtm.com" && uri.authority !== "www.lgtm.com") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const paths = uri.path.split("/").filter((segment) => segment);
|
||||||
|
return paths.length === 4 && paths[0] === "projects" && paths[1] === "g";
|
||||||
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uri.authority !== "lgtm.com" && uri.authority !== "www.lgtm.com") {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const paths = uri.path.split("/").filter((segment) => segment);
|
|
||||||
return paths.length === 4 && paths[0] === "projects" && paths[1] === "g";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function convertToDatabaseUrl(lgtmUrl: string) {
|
async function convertToDatabaseUrl(lgtmUrl: string) {
|
||||||
const uri = Uri.parse(lgtmUrl, true);
|
try {
|
||||||
const paths = ["api", "v1.0"].concat(
|
const uri = Uri.parse(lgtmUrl, true);
|
||||||
uri.path.split("/").filter((segment) => segment)
|
const paths = ["api", "v1.0"].concat(
|
||||||
);
|
uri.path.split("/").filter((segment) => segment)
|
||||||
const projectUrl = `https://lgtm.com/${paths.join("/")}`;
|
);
|
||||||
const projectResponse = await fetch(projectUrl);
|
const projectUrl = `https://lgtm.com/${paths.join("/")}`;
|
||||||
const projectJson = await projectResponse.json();
|
const projectResponse = await fetch(projectUrl);
|
||||||
|
const projectJson = await projectResponse.json();
|
||||||
|
|
||||||
const language = await promptForLanguage(projectJson);
|
if (projectJson.code === 404) {
|
||||||
if (!language) {
|
throw new Error();
|
||||||
return;
|
}
|
||||||
|
|
||||||
|
const language = await promptForLanguage(projectJson);
|
||||||
|
if (!language) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return `https://lgtm.com/${[
|
||||||
|
"api",
|
||||||
|
"v1.0",
|
||||||
|
"snapshots",
|
||||||
|
projectJson.id,
|
||||||
|
language,
|
||||||
|
].join("/")}`;
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(`Invalid LGTM URL: ${lgtmUrl}`);
|
||||||
}
|
}
|
||||||
return `https://lgtm.com/${[
|
|
||||||
"api",
|
|
||||||
"v1.0",
|
|
||||||
"snapshots",
|
|
||||||
projectJson.id,
|
|
||||||
language,
|
|
||||||
].join("/")}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function promptForLanguage(
|
async function promptForLanguage(
|
||||||
|
|||||||
Reference in New Issue
Block a user