Skip to content

Commit

Permalink
fix: Gracefully handle degradation cancelation errors (#14931)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc authored Mar 30, 2023
1 parent d65f86a commit 5d7b83e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 25 deletions.
19 changes: 4 additions & 15 deletions src/script/conversation/MessageRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,19 +483,15 @@ export class MessageRepository {
return;
}
if (state === MessageSendingState.CANCELED) {
throw new ConversationError(
ConversationError.TYPE.DEGRADED_CONVERSATION_CANCELLATION,
ConversationError.MESSAGE.DEGRADED_CONVERSATION_CANCELLATION,
);
// The user has canceled the upload, no need to do anything else
return;
}
try {
await this.sendAssetRemotedata(conversation, file, id, asImage);
const uploadDuration = (Date.now() - uploadStarted) / TIME_IN_MILLIS.SECOND;
this.logger.info(`Finished to upload asset for conversation'${conversation.id} in ${uploadDuration}`);
} catch (error) {
if (this.isUserCancellationError(error)) {
throw error;
} else if (error instanceof RequestCancellationError) {
if (error instanceof RequestCancellationError) {
return;
}
this.logger.error(
Expand Down Expand Up @@ -640,14 +636,6 @@ export class MessageRepository {
return this.sendAndInjectMessage(payload, conversation);
}

private isUserCancellationError(error: any): boolean {
const errorTypes: string[] = [
ConversationError.TYPE.DEGRADED_CONVERSATION_CANCELLATION,
ConversationError.TYPE.LEGAL_HOLD_CONVERSATION_CANCELLATION,
];
return errorTypes.includes(error.type);
}

/**
* Will request user permission before sending a message in case the conversation is in a degraded state
*
Expand Down Expand Up @@ -806,6 +794,7 @@ export class MessageRepository {

const shouldProceedSending = await injectOptimisticEvent();
if (shouldProceedSending === false) {
this.logger.log('User has canceled sending a message to a degraded conversation.');
return {id: payload.messageId, sentAt: new Date().toISOString(), state: MessageSendingState.CANCELED};
}

Expand Down
3 changes: 0 additions & 3 deletions src/script/error/ConversationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {BaseError, BASE_ERROR_TYPE} from './BaseError';

enum CONVERSATION_ERROR_TYPE {
CONVERSATION_NOT_FOUND = 'CONVERSATION_NOT_FOUND',
DEGRADED_CONVERSATION_CANCELLATION = 'DEGRADED_CONVERSATION_CANCELLATION',
INVALID_PARAMETER = 'INVALID_PARAMETER',
LEGAL_HOLD_CONVERSATION_CANCELLATION = 'LEGAL_HOLD_CONVERSATION_CANCELLATION',
MESSAGE_NOT_FOUND = 'MESSAGE_NOT_FOUND',
Expand All @@ -47,7 +46,6 @@ export class ConversationError extends BaseError {
static get MESSAGE(): Record<CONVERSATION_ERROR_TYPE | string, string> {
return {
CONVERSATION_NOT_FOUND: 'Conversation not found',
DEGRADED_CONVERSATION_CANCELLATION: 'Sending to degraded conversation was canceled by user',
INVALID_PARAMETER: 'Invalid parameter passed',
LEGAL_HOLD_CONVERSATION_CANCELLATION: 'Sending to legal hold conversation was canceled by user',
MESSAGE_NOT_FOUND: 'Message not found in conversation',
Expand All @@ -66,7 +64,6 @@ export class ConversationError extends BaseError {
static get TYPE(): Record<CONVERSATION_ERROR_TYPE, CONVERSATION_ERROR_TYPE> {
return {
CONVERSATION_NOT_FOUND: CONVERSATION_ERROR_TYPE.CONVERSATION_NOT_FOUND,
DEGRADED_CONVERSATION_CANCELLATION: CONVERSATION_ERROR_TYPE.DEGRADED_CONVERSATION_CANCELLATION,
INVALID_PARAMETER: CONVERSATION_ERROR_TYPE.INVALID_PARAMETER,
LEGAL_HOLD_CONVERSATION_CANCELLATION: CONVERSATION_ERROR_TYPE.LEGAL_HOLD_CONVERSATION_CANCELLATION,
MESSAGE_NOT_FOUND: CONVERSATION_ERROR_TYPE.MESSAGE_NOT_FOUND,
Expand Down
7 changes: 0 additions & 7 deletions src/script/ui/WindowHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ export class WindowHandler {
window.addEventListener('unhandledrejection', (promiseRejectionEvent: any): void | false => {
const error = promiseRejectionEvent.reason || {};

const isDegraded = error.type === ConversationError.TYPE.DEGRADED_CONVERSATION_CANCELLATION;
if (isDegraded) {
this.logger.log('User has canceled sending a message to a degraded conversation.');
promiseRejectionEvent.preventDefault();
promiseRejectionEvent.stopPropagation();
return false;
}
const isLegalHoldReject = error.type === ConversationError.TYPE.LEGAL_HOLD_CONVERSATION_CANCELLATION;
if (isLegalHoldReject) {
this.logger.log('User has canceled sending a message to a conversation under legal hold.');
Expand Down

0 comments on commit 5d7b83e

Please sign in to comment.