From c6aa238710b52026b1826bb55f41d503c9d0d3df Mon Sep 17 00:00:00 2001 From: Max Bischof <106820326+bischofmax@users.noreply.github.com> Date: Thu, 6 Jun 2024 08:45:16 +0200 Subject: [PATCH] BC-7432 - Fix etherpad delete (#5049) --- .../etherpad-client.adapter.spec.ts | 31 ++++++++++++++++--- .../etherpad-client.adapter.ts | 30 ++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/apps/server/src/infra/etherpad-client/etherpad-client.adapter.spec.ts b/apps/server/src/infra/etherpad-client/etherpad-client.adapter.spec.ts index 31d9c25ca02..d730c6c26d5 100644 --- a/apps/server/src/infra/etherpad-client/etherpad-client.adapter.spec.ts +++ b/apps/server/src/infra/etherpad-client/etherpad-client.adapter.spec.ts @@ -968,7 +968,7 @@ describe(EtherpadClientAdapter.name, () => { return groupId; }; - it('should call deletePadUsingGET with correct params', async () => { + it('should call deleteGroupUsingGET with correct params', async () => { const groupId = setup(); await service.deleteGroup(groupId); @@ -977,12 +977,12 @@ describe(EtherpadClientAdapter.name, () => { }); }); - describe('when deleteGroupUsingPOST returns etherpad error code', () => { + describe('when deleteGroupUsingPOST returns valid etherpad error', () => { const setup = () => { const groupId = 'groupId'; const response = createMock>({ data: { - code: EtherpadResponseCode.BAD_REQUEST, + code: EtherpadResponseCode.INTERNAL_ERROR, data: {}, }, }); @@ -995,11 +995,34 @@ describe(EtherpadClientAdapter.name, () => { it('should throw EtherpadErrorLoggableException', async () => { const groupId = setup(); - const exception = new EtherpadErrorLoggableException(EtherpadErrorType.BAD_REQUEST, { padId: groupId }, {}); + const exception = new EtherpadErrorLoggableException(EtherpadErrorType.INTERNAL_ERROR, { padId: groupId }, {}); await expect(service.deleteGroup(groupId)).rejects.toThrowError(exception); }); }); + describe('when deleteGroupUsingPOST returns invalid etherpad error', () => { + const setup = () => { + const groupId = 'groupId'; + const response = createMock>({ + data: { + code: EtherpadResponseCode.BAD_REQUEST, + message: 'sessionID does not exist', + data: {}, + }, + }); + + groupApi.deleteGroupUsingPOST.mockResolvedValue(response); + + return groupId; + }; + + it('should return successfull', async () => { + const groupId = setup(); + + await service.deleteGroup(groupId); + }); + }); + describe('when deleteGroupUsingPOST returns error', () => { const setup = () => { const groupId = 'padId'; diff --git a/apps/server/src/infra/etherpad-client/etherpad-client.adapter.ts b/apps/server/src/infra/etherpad-client/etherpad-client.adapter.ts index 404a544a0ae..3b87606ddbc 100644 --- a/apps/server/src/infra/etherpad-client/etherpad-client.adapter.ts +++ b/apps/server/src/infra/etherpad-client/etherpad-client.adapter.ts @@ -3,6 +3,8 @@ import { TypeGuard } from '@shared/common'; import { EntityId } from '@shared/domain/types'; import { AxiosResponse } from 'axios'; import { + AuthorApi, + GroupApi, InlineResponse200, InlineResponse2001, InlineResponse20013, @@ -10,8 +12,9 @@ import { InlineResponse2003, InlineResponse2004, InlineResponse2006, + PadApi, + SessionApi, } from './etherpad-api-client'; -import { AuthorApi, GroupApi, PadApi, SessionApi } from './etherpad-api-client/api'; import { AuthorId, EtherpadErrorType, @@ -23,6 +26,7 @@ import { Session, SessionId, } from './interface'; +import { EtherpadErrorLoggableException } from './loggable'; import { EtherpadResponseMapper } from './mappers'; @Injectable() @@ -225,7 +229,29 @@ export class EtherpadClientAdapter { public async deleteGroup(groupId: GroupId): Promise { const response = await this.tryDeleteGroup(groupId); - this.handleEtherpadResponse(response, { groupId }); + + try { + this.handleEtherpadResponse(response, { groupId }); + } catch (error) { + this.throwIfValidError(error); + } + } + + private throwIfValidError(error: unknown): void { + // If the session does not exist, we can ignore the error. See https://github.com/ether/etherpad-lite/issues/5798 + if (this.isSessionDoesNotExistError(error)) { + return; + } + + throw error; + } + + private isSessionDoesNotExistError(error: unknown): boolean { + return !!( + error instanceof EtherpadErrorLoggableException && + error.getLogMessage().type === EtherpadErrorType.BAD_REQUEST && + error?.cause?.toString().includes('sessionID does not exist') + ); } private async tryDeleteGroup(groupId: string): Promise> {