Handle cloud project opening errors better (#5202)

Don't show in changelog
This commit is contained in:
AlexandreS
2023-04-05 10:28:25 +02:00
committed by GitHub
parent 95dfb391c4
commit bc87caf640
3 changed files with 30 additions and 14 deletions

View File

@@ -855,7 +855,6 @@ const MainFrame = (props: Props) => {
const openFromFileMetadata = React.useCallback(
async (fileMetadata: FileMetadata): Promise<?State> => {
const storageProviderOperations = getStorageProviderOperations();
const storageProviderInternalName = getStorageProvider().internalName;
const {
hasAutoSave,
@@ -916,6 +915,7 @@ const MainFrame = (props: Props) => {
await delay(150);
const autoSaveFileMetadata = await checkForAutosave();
let content;
let openingError: Error | null = null;
try {
const result = await onOpen(
autoSaveFileMetadata,
@@ -923,6 +923,7 @@ const MainFrame = (props: Props) => {
);
content = result.content;
} catch (error) {
openingError = error;
// onOpen failed, try to find again an autosave.
const autoSaveAfterFailureFileMetadata = await checkForAutosaveAfterFailure();
if (autoSaveAfterFailureFileMetadata) {
@@ -931,9 +932,10 @@ const MainFrame = (props: Props) => {
}
}
if (!content) {
throw new Error(
'The project file content could not be read. It might be corrupted/malformed.'
);
throw openingError ||
new Error(
'The project file content could not be read. It might be corrupted/malformed.'
);
}
if (!verifyProjectContent(i18n, content)) {
// The content is not recognized and the user was warned. Abort the opening.
@@ -957,7 +959,7 @@ const MainFrame = (props: Props) => {
serializedProject.delete();
}
} catch (error) {
if (storageProviderInternalName === 'Cloud') {
if (error.name === 'CloudProjectReadingError') {
setIsLoadingProject(false);
setLoaderModalProgress(null, null);
setCloudProjectFileMetadataToRecover(fileMetadata);
@@ -979,7 +981,6 @@ const MainFrame = (props: Props) => {
[
i18n,
getStorageProviderOperations,
getStorageProvider,
loadFromSerializedProject,
showConfirmation,
showAlert,

View File

@@ -11,6 +11,18 @@ import { type AuthenticatedUser } from '../../Profile/AuthenticatedUserContext';
import { type FileMetadata } from '..';
import { unzipFirstEntryOfBlob } from '../../Utils/Zip.js/Utils';
class CloudProjectReadingError extends Error {
constructor() {
super();
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CloudProjectReadingError);
}
this.name = 'CloudProjectReadingError';
}
}
export const generateOnOpen = (authenticatedUser: AuthenticatedUser) => async (
fileMetadata: FileMetadata,
onProgress?: (progress: number, message: MessageDescriptor) => void
@@ -32,13 +44,16 @@ export const generateOnOpen = (authenticatedUser: AuthenticatedUser) => async (
);
onProgress && onProgress((4 / 4) * 100, t`Opening portal`);
// Reading only the first entry since the zip should only contain the project json file
const serializedProject = await unzipFirstEntryOfBlob(
zippedSerializedProject
);
return {
content: JSON.parse(serializedProject),
};
try {
const serializedProject = await unzipFirstEntryOfBlob(
zippedSerializedProject
);
return {
content: JSON.parse(serializedProject),
};
} catch (error) {
throw new CloudProjectReadingError();
}
};
export const generateOnEnsureCanAccessResources = (

View File

@@ -106,8 +106,8 @@ export const isCloudProjectVersionSane = async (
{ responseType: 'blob' }
)
);
const projectFile = await unzipFirstEntryOfBlob(response.data);
try {
const projectFile = await unzipFirstEntryOfBlob(response.data);
JSON.parse(projectFile);
return true;
} catch (error) {