Skip to content

Commit

Permalink
fix: api 요청 빈배열 반환 및 에러처리
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHyeon0409 committed Nov 18, 2024
1 parent 111ec9e commit 3c1bd64
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
38 changes: 38 additions & 0 deletions packages/server/common/all-exceptions.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
} from '@nestjs/common';

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();

// HTTP 예외 처리
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;

const message =
exception instanceof HttpException
? exception.getResponse()
: 'Internal server error';

// 일관된 에러 응답 형식
response.status(status).json({
statusCode: status,
message:
typeof message === 'string'
? message
: (message as any)?.message || message,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
2 changes: 2 additions & 0 deletions packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@nestjs/swagger';
import { config } from 'dotenv';
import { setupSshTunnel } from './configs/ssh-tunnel';
import { AllExceptionsFilter } from 'common/all-exceptions.filter';

config();

Expand Down Expand Up @@ -43,6 +44,7 @@ async function bootstrap() {
SwaggerModule.setup('api', app, documentFactory);

app.setGlobalPrefix('api');
app.useGlobalFilters(new AllExceptionsFilter());

await app.listen(process.env.PORT ?? 3000);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/server/src/upbit/coin-list.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ export class CoinListService implements OnModuleInit {
});
}
async getSimpleCoin(coins) {
console.log(coins);
let krwCoinInfo = this.coinDataUpdaterService.getKrwCoinInfo();
while (!krwCoinInfo) {
await new Promise((resolve) => setTimeout(resolve, 100));
krwCoinInfo = this.coinDataUpdaterService.getKrwCoinInfo();
}

if (!coins.length) return [];

return krwCoinInfo
.filter((coin) => coins.includes(coin.market))
.map((coin) => {
Expand All @@ -50,7 +54,7 @@ export class CoinListService implements OnModuleInit {
};
});
}

getCoinNameList() {
return this.coinDataUpdaterService.getCoinCodeList();
}
Expand Down
5 changes: 4 additions & 1 deletion packages/server/src/upbit/upbit.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class UpbitController {

@Sse('price-updates')
priceUpdates(@Query('coins') coins: string[]): Observable<MessageEvent> {
coins = coins || [];
const initData = this.sseService.initPriceStream(
coins,
this.coinListService.convertToMarketCoinDto,
Expand All @@ -26,6 +27,7 @@ export class UpbitController {
}
@Sse('orderbook')
orderbookUpdates(@Query('coins') coins: string[]): Observable<MessageEvent> {
coins = coins || [];
return this.sseService.getOrderbookUpdatesStream(
coins,
this.coinListService.convertToOrderbookDto,
Expand Down Expand Up @@ -55,6 +57,7 @@ export class UpbitController {
}
@Get('market/simplelist/krw')
getSomeKRW(@Query('market') market: string[]) {
return this.coinListService.getSimpleCoin(market);
const marketList = market || [];
return this.coinListService.getSimpleCoin(marketList);
}
}

0 comments on commit 3c1bd64

Please sign in to comment.