Skip to content

Commit

Permalink
Replace error handling ..but one unstable test exists
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Jun 6, 2024
1 parent e290030 commit d301210
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { TextEncoder } from 'util';
import { INestApplication, NotAcceptableException } from '@nestjs/common';
import { MongoMemoryDatabaseModule } from '@infra/database';
import { createConfigModuleOptions } from '@src/config';
import { Logger } from '@src/core/logger';
import { of, throwError } from 'rxjs';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpService } from '@nestjs/axios';
import { AxiosError, AxiosHeaders, AxiosResponse } from 'axios';
import { axiosResponseFactory } from '@shared/testing';
import { CoreModule } from '@src/core';
import { TldrawRedisFactory, TldrawRedisService } from '../../redis';
import { TldrawDrawing } from '../../entities';
import { TldrawWsService } from '../../service';
Expand All @@ -22,6 +22,7 @@ import { TldrawWs } from '..';
import { WsCloseCode, WsCloseMessage } from '../../types';
import { TldrawConfig } from '../../config';

// This is a unit test, no api test...need to be refactored
describe('WebSocketController (WsAdapter)', () => {
let app: INestApplication;
let gateway: TldrawWs;
Expand All @@ -41,6 +42,7 @@ describe('WebSocketController (WsAdapter)', () => {
imports: [
MongoMemoryDatabaseModule.forRoot({ entities: [TldrawDrawing] }),
ConfigModule.forRoot(createConfigModuleOptions(tldrawTestConfig)),
CoreModule,
],
providers: [
TldrawWs,
Expand All @@ -54,10 +56,6 @@ describe('WebSocketController (WsAdapter)', () => {
provide: TldrawRepo,
useValue: createMock<TldrawRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
{
provide: HttpService,
useValue: createMock<HttpService>(),
Expand All @@ -76,6 +74,7 @@ describe('WebSocketController (WsAdapter)', () => {

afterAll(async () => {
await app.close();
jest.resetAllMocks();
});

afterEach(() => {
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/modules/tldraw/controller/tldraw.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
NotFoundException,
NotAcceptableException,
} from '@nestjs/common';
import { Logger } from '@src/core/logger';
import { isAxiosError } from 'axios';
import { firstValueFrom } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { DomainErrorHandler } from '@src/core';
import { WebsocketInitErrorLoggable } from '../loggable';
import { TldrawConfig, TLDRAW_SOCKET_PORT } from '../config';
import { WsCloseCode, WsCloseMessage } from '../types';
Expand All @@ -27,7 +27,7 @@ export class TldrawWs implements OnGatewayInit, OnGatewayConnection {
private readonly configService: ConfigService<TldrawConfig, true>,
private readonly tldrawWsService: TldrawWsService,
private readonly httpService: HttpService,
private readonly logger: Logger
private readonly domainErrorHandler: DomainErrorHandler
) {}

public async handleConnection(client: WebSocket, request: Request): Promise<void> {
Expand Down Expand Up @@ -106,7 +106,7 @@ export class TldrawWs implements OnGatewayInit, OnGatewayConnection {
err?: unknown
): void {
client.close(code, message);
this.logger.warning(new WebsocketInitErrorLoggable(code, message, docName, err));
this.domainErrorHandler.exec(new WebsocketInitErrorLoggable(code, message, docName, err));
}

private handleError(err: unknown, client: WebSocket, docName: string): void {
Expand Down
Loading

0 comments on commit d301210

Please sign in to comment.