(core) Improving error messages on file imports

Summary: Improving error messages that get returned from "Import from URL" plugin.

Test Plan: browser tests

Reviewers: alexmojaki

Reviewed By: alexmojaki

Subscribers: dsagal, alexmojaki

Differential Revision: https://phab.getgrist.com/D2946
This commit is contained in:
Jarosław Sadziński 2021-08-02 09:27:16 +02:00
parent 580cd0e22e
commit 521bbd9ac1
2 changed files with 99 additions and 54 deletions

View File

@ -101,6 +101,10 @@ export class DocPluginManager {
}
}
if (path.extname(fileName) === '.grist') {
throw new Error(`To import a grist document use the "Import document" menu option on your home screen`);
}
const matchingFileParsers: FileParserElement[] = FileParserElement.getMatching(this._pluginInstances, fileName);
if (!this._tmpDir) {
@ -128,8 +132,18 @@ export class DocPluginManager {
continue;
}
}
const details = messages.length ? ": " + messages.join("; ") : "";
throw new Error(`Cannot parse this data${details}`);
if (messages.length) {
const extToType: Record<string, string> = {
'.xlsx' : 'Excel',
'.xls' : 'Excel',
'.json' : 'JSON',
'.csv' : 'CSV',
};
const fileType = extToType[path.extname(fileName)] || path.extname(fileName);
throw new Error(`Failed to parse ${fileType} file. Error: ${messages.join("; ")}`);
}
throw new Error(`File format is not supported.`);
}
/**

View File

@ -337,6 +337,7 @@ export async function fetchURL(url: string, accessId: string|null): Promise<Uplo
*/
async function _fetchURL(url: string, accessId: string|null, fileName: string,
headers?: {[key: string]: string}): Promise<UploadResult> {
try {
const response: FetchResponse = await Deps.fetch(url, {
redirect: 'follow',
follow: 10,
@ -367,6 +368,14 @@ async function _fetchURL(url: string, accessId: string|null, fileName: string,
log.debug(`done fetching url: ${url} to ${destPath}`);
const uploadId = globalUploadSet.registerUpload([uploadedFile], tmpDir, cleanupCallback, accessId);
return {uploadId, files: [pick(uploadedFile, ['origName', 'size', 'ext'])]};
} catch(err) {
if (err?.code === "EPROTO" || // https vs http error
err?.code === "ECONNREFUSED" || // server does not listen
err?.code === "ENOTFOUND") { // could not resolve domain
throw new ApiError(`Can't connect to the server. The URL seems to be invalid. Error code ${err.code}`, 400);
}
throw err;
}
}
/**
@ -392,10 +401,32 @@ async function fetchDoc(homeUrl: string, docId: string, req: Request, accessId:
// Re-issue failures as exceptions.
async function _checkForError(response: FetchResponse) {
if (response.ok) { return; }
if (response.status === 403) {
throw new ApiError("Access to this resource was denied.", response.status);
}
if (response.ok) {
const contentType = response.headers.get("content-type");
if (contentType?.startsWith("text/html")) {
// Probably we hit some login page
if (response.url.startsWith("https://accounts.google.com")) {
throw new ApiError("Importing directly from a Google Drive URL is not supported yet. " +
'Use the "Import from Google Drive" menu option instead.', 403);
} else {
throw new ApiError("Could not import the requested file, check if you have all required permissions.", 403);
}
}
return;
}
const body = await response.json().catch(() => ({}));
if (response.status === 404) {
throw new ApiError("File can't be found at the requested URL.", 404);
} else if (response.status >= 500 && response.status < 600) {
throw new ApiError(`Remote server returned an error (${body.error || response.statusText})`,
response.status, body.details);
} else {
throw new ApiError(body.error || response.statusText, response.status, body.details);
}
}
/**
* Create an access identifier, combining the userId supplied with the host of the