Skip to content

Commit

Permalink
Fix etherpad delete
Browse files Browse the repository at this point in the history
  • Loading branch information
bischofmax committed Jun 5, 2024
1 parent e290030 commit df5da72
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<AxiosResponse<InlineResponse2001>>({
data: {
code: EtherpadResponseCode.BAD_REQUEST,
code: EtherpadResponseCode.INTERNAL_ERROR,
data: {},
},
});
Expand All @@ -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<AxiosResponse<InlineResponse2001>>({
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';
Expand Down
30 changes: 28 additions & 2 deletions apps/server/src/infra/etherpad-client/etherpad-client.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import { TypeGuard } from '@shared/common';
import { EntityId } from '@shared/domain/types';
import { AxiosResponse } from 'axios';
import {
AuthorApi,
GroupApi,
InlineResponse200,
InlineResponse2001,
InlineResponse20013,
InlineResponse2002,
InlineResponse2003,
InlineResponse2004,
InlineResponse2006,
PadApi,
SessionApi,
} from './etherpad-api-client';
import { AuthorApi, GroupApi, PadApi, SessionApi } from './etherpad-api-client/api';
import {
AuthorId,
EtherpadErrorType,
Expand All @@ -23,6 +26,7 @@ import {
Session,
SessionId,
} from './interface';
import { EtherpadErrorLoggableException } from './loggable';
import { EtherpadResponseMapper } from './mappers';

@Injectable()
Expand Down Expand Up @@ -225,7 +229,29 @@ export class EtherpadClientAdapter {

public async deleteGroup(groupId: GroupId): Promise<void> {
const response = await this.tryDeleteGroup(groupId);
this.handleEtherpadResponse<InlineResponse2001>(response, { groupId });

try {
this.handleEtherpadResponse<InlineResponse2001>(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<AxiosResponse<InlineResponse2001>> {
Expand Down

0 comments on commit df5da72

Please sign in to comment.