Skip to content

Commit

Permalink
feat: unify files errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Mar 1, 2024
1 parent 7785431 commit 2664638
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 133 deletions.
23 changes: 9 additions & 14 deletions src/apps/fuse/callbacks/RenameOrMoveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,18 @@ export class RenameOrMoveFile {
);

return right(RenameOrMoveFile.SUCCESS);
} catch (throwed: unknown) {
const current = path.basename(src);
const desired = path.basename(dest);

} catch (trowed: unknown) {
const cause: SyncErrorCause =
throwed instanceof DriveDesktopError
? throwed.syncErrorCause
: 'UNKNOWN';
trowed instanceof DriveDesktopError ? trowed.syncErrorCause : 'UNKNOWN';

await this.container.syncFileMessenger.errorWhileRenaming(
current,
desired,
cause
);
await this.container.syncFileMessenger.error({
error: 'RENAME_ERROR',
cause,
name: path.basename(src),
});

if (throwed instanceof FuseError) {
return left(throwed);
if (trowed instanceof FuseError) {
return left(trowed);
}

return left(new FuseUnknownError());
Expand Down
4 changes: 2 additions & 2 deletions src/apps/main/analytics/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
VirtualDriveError,
isVirtualDriveFolderError,
} from '../../../shared/issues/VirtualDriveError';
import { VirtualDriveFolderIssue } from '../../../shared/issues/VirtualDriveIssue';
import { VirtualDriveIssue } from '../../../shared/issues/VirtualDriveIssue';

const virtualDriveErrorToTrackedActionsMap = new Map<
VirtualDriveError,
Expand Down Expand Up @@ -243,7 +243,7 @@ export function trackError(
client.track(payload);
}

export function trackVirtualDriveError(error: VirtualDriveFolderIssue) {
export function trackVirtualDriveError(error: VirtualDriveIssue) {
const event = virtualDriveErrorToTrackedActionsMap.get(error.error);

if (!event) {
Expand Down
46 changes: 26 additions & 20 deletions src/apps/shared/IPC/events/virtualDrive/backgroundEvents/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,59 @@ type FileUpdatePayload = {
processInfo: ProcessInfo;
};

export type FilesEvents = {
FILE_UPLOADING: (payload: FileUpdatePayload) => void;
FILE_UPLOADED: (payload: FileUpdatePayload) => void;
FILE_CREATED: (payload: {
export type FileErrorEvents = {
FILE_DOWNLOAD_ERROR: (payload: {
name: string;
extension: string;
nameWithExtension: string;
cause: SyncErrorCause;
}) => void;
FILE_DOWNLOAD_ERROR: (payload: {

FILE_UPLOAD_ERROR: (payload: {
name: string;
extension: string;
nameWithExtension: string;
cause: SyncErrorCause;
}) => void;

FILE_DOWNLOADING: (payload: FileUpdatePayload) => void;
FILE_DOWNLOADED: (payload: FileUpdatePayload) => void;
FILE_UPLOAD_ERROR: (payload: {
FILE_DELETION_ERROR: (payload: {
name: string;
extension: string;
nameWithExtension: string;
cause: SyncErrorCause;
}) => void;

FILE_DELETING: (payload: {
FILE_RENAME_ERROR: (payload: {
name: string;
extension: string;
nameWithExtension: string;
size: number;
cause: SyncErrorCause;
}) => void;
FILE_DELETED: (payload: {
};

export type FilesEvents = {
FILE_UPLOADING: (payload: FileUpdatePayload) => void;
FILE_UPLOADED: (payload: FileUpdatePayload) => void;
FILE_CREATED: (payload: {
name: string;
extension: string;
nameWithExtension: string;
}) => void;

FILE_DOWNLOADING: (payload: FileUpdatePayload) => void;
FILE_DOWNLOADED: (payload: FileUpdatePayload) => void;

FILE_DELETING: (payload: {
name: string;
extension: string;
nameWithExtension: string;
size: number;
}) => void;
FILE_DELETION_ERROR: (payload: {
FILE_DELETED: (payload: {
name: string;
extension: string;
nameWithExtension: string;
cause: SyncErrorCause;
size: number;
}) => void;

FILE_RENAMING: (payload: {
Expand All @@ -68,14 +80,8 @@ export type FilesEvents = {
nameWithExtension: string;
folderName: string;
}) => void;
FILE_RENAME_ERROR: (payload: {
name: string;
extension: string;
nameWithExtension: string;
cause: SyncErrorCause;
}) => void;

FILE_OVERWRITTEN: (payload: { nameWithExtension: string }) => void;

FILE_CLONED: (payload: FileUpdatePayload) => void;
};
} & FileErrorEvents;
10 changes: 5 additions & 5 deletions src/context/virtual-drive/files/application/FileCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ export class FileCreator {
const cause =
error instanceof DriveDesktopError ? error.syncErrorCause : 'UNKNOWN';

await this.notifier.errorWhileCreating(
filePath.name(),
filePath.extension(),
cause
);
await this.notifier.error({
error: 'UPLOAD_ERROR',
cause,
name: filePath.nameWithExtension(),
});

throw error;
}
Expand Down
6 changes: 5 additions & 1 deletion src/context/virtual-drive/files/application/FileDeleter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export class FileDeleter {
const cause =
error instanceof DriveDesktopError ? error.syncErrorCause : 'UNKNOWN';

this.notifier.errorWhileTrashing(file.name, file.type, cause);
await this.notifier.error({
error: 'DELETE_ERROR',
cause,
name: file.nameWithExtension,
});

// TODO: add an event and an event handler to recreate placeholders if needed
this.local.createPlaceHolder(file);
Expand Down
18 changes: 2 additions & 16 deletions src/context/virtual-drive/files/domain/SyncFileMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { SyncErrorCause } from '../../../../shared/issues/SyncErrorCause';
import { VirtualDriveFileIssue } from '../../../../shared/issues/VirtualDriveIssue';

export interface SyncFileMessenger {
created(name: string, extension: string): Promise<void>;
errorWhileCreating(
name: string,
extension: string,
cause: SyncErrorCause
): Promise<void>;
trashing(name: string, extension: string, size: number): Promise<void>;
trashed(name: string, extension: string, size: number): Promise<void>;
errorWhileTrashing(
name: string,
extension: string,
cause: SyncErrorCause
): Promise<void>;
renaming(current: string, desired: string): Promise<void>;
renamed(current: string, desired: string): Promise<void>;
errorWhileRenaming(
current: string,
desired: string,
cause: SyncErrorCause
): Promise<void>;
error(error: VirtualDriveFileIssue): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@ import path from 'path';
import { SyncEngineIpc } from '../../../../../apps/sync-engine/SyncEngineIpc';
import { SyncMessenger } from '../../../../shared/domain/SyncMessenger';
import { SyncFileMessenger } from '../../domain/SyncFileMessenger';
import { SyncErrorCause } from '../../../../../shared/issues/SyncErrorCause';
import { VirtualDriveFileIssue } from '../../../../../shared/issues/VirtualDriveIssue';
import { VirtualDriveFileError } from '../../../../../shared/issues/VirtualDriveError';
import { FileErrorEvents } from '../../../../../apps/shared/IPC/events/virtualDrive/backgroundEvents/files';

const virtualDriveFileErrorToFileErrorKeyMap: Record<
VirtualDriveFileError,
keyof FileErrorEvents
> = {
UPLOAD_ERROR: 'FILE_UPLOAD_ERROR',
DOWNLOAD_ERROR: 'FILE_DOWNLOAD_ERROR',
RENAME_ERROR: 'FILE_RENAME_ERROR',
DELETE_ERROR: 'FILE_DELETION_ERROR',
METADATA_READ_ERROR: 'FILE_UPLOAD_ERROR',
GENERATE_TREE: 'FILE_DOWNLOAD_ERROR',
};

export class BackgroundProcessSyncFileMessenger
extends SyncMessenger
Expand All @@ -19,19 +33,6 @@ export class BackgroundProcessSyncFileMessenger
});
}

async errorWhileCreating(
name: string,
extension: string,
cause: SyncErrorCause
): Promise<void> {
this.ipc.send('FILE_UPLOAD_ERROR', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
cause,
});
}

async trashing(name: string, extension: string, size: number): Promise<void> {
this.ipc.send('FILE_DELETING', {
name,
Expand All @@ -50,19 +51,6 @@ export class BackgroundProcessSyncFileMessenger
});
}

async errorWhileTrashing(
name: string,
extension: string,
cause: SyncErrorCause
): Promise<void> {
this.ipc.send('FILE_DELETION_ERROR', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
cause,
});
}

async renaming(current: string, desired: string): Promise<void> {
this.ipc.send('FILE_RENAMING', {
oldName: current,
Expand All @@ -77,20 +65,14 @@ export class BackgroundProcessSyncFileMessenger
});
}

async errorWhileRenaming(
current: string,
_desired: string,
cause: SyncErrorCause
): Promise<void> {
const extension = path.extname(current);

const name = current.replace(`.${extension}`, '');
async error(issue: VirtualDriveFileIssue): Promise<void> {
const event = virtualDriveFileErrorToFileErrorKeyMap[issue.error];

this.ipc.send('FILE_RENAME_ERROR', {
name,
extension,
nameWithExtension: current,
cause,
this.ipc.send(event, {
name: path.basename(issue.name, path.extname(issue.name)),
extension: path.extname(issue.name),
nameWithExtension: issue.name,
cause: issue.cause,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {
trackError,
trackEvent,
trackVirtualDriveError,
} from '../../../../../apps/main/analytics/service';
import { addVirtualDriveIssue } from '../../../../../apps/main/issues/virtual-drive';
import { setTrayStatus } from '../../../../../apps/main/tray/tray';
import { broadcastToWindows } from '../../../../../apps/main/windows';
import { VirtualDriveIssue } from '../../../../../shared/issues/VirtualDriveIssue';
import {
VirtualDriveFileIssue,
VirtualDriveIssue,
} from '../../../../../shared/issues/VirtualDriveIssue';
import { SyncMessenger } from '../../../../shared/domain/SyncMessenger';
import { SyncFileMessenger } from '../../domain/SyncFileMessenger';

Expand Down Expand Up @@ -111,16 +115,11 @@ export class MainProcessSyncFileMessenger
setTrayStatus('IDLE');
}

async errorWhileRenaming(
current: string,
_desired: string,
_message: string
): Promise<void> {
broadcastToWindows('sync-info-update', {
action: 'FOLDER_RENAME_ERROR',
name: current,
});

async error(error: VirtualDriveFileIssue): Promise<void> {
setTrayStatus('ALERT');

trackVirtualDriveError(error);

addVirtualDriveIssue(error);
}
}
6 changes: 6 additions & 0 deletions src/shared/issues/VirtualDriveIssue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SyncErrorCause } from './SyncErrorCause';
import {
VirtualDriveError,
VirtualDriveFileError,
VirtualDriveFolderError,
} from './VirtualDriveError';

Expand All @@ -9,6 +10,11 @@ export type VirtualDriveFolderIssue = {
cause: SyncErrorCause;
name: string;
};
export type VirtualDriveFileIssue = {
error: VirtualDriveFileError;
cause: SyncErrorCause;
name: string;
};

export type VirtualDriveIssue = {
error: VirtualDriveError;
Expand Down
Loading

0 comments on commit 2664638

Please sign in to comment.