Skip to content

Commit eb81ad6

Browse files
committed
Fix axios 404 exceptions
1 parent c3d5f9e commit eb81ad6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

backend/src/models/tightshorts/tightshorts.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import axios from 'axios';
4+
import type { AxiosRequestConfig } from 'axios';
45
import { TightshortsRepository } from './tightshorts.repository';
56
import type { ITightshorts } from './interfaces/tightshorts.interface';
67
import type { ITightshortsChart } from './interfaces/chart.interface';
@@ -20,8 +21,8 @@ export class TightshortsService {
2021
return this.tightshortsRepository.set(stockTicker, data);
2122
}
2223

23-
async fetch(stockTicker: string): Promise<ITightshorts> {
24-
const config = {
24+
async fetch(stockTicker: string): Promise<ITightshorts | null> {
25+
const config: AxiosRequestConfig<any> = {
2526
method: 'get',
2627
url: `${this.configService.get(
2728
'SHORT_API_URL',
@@ -30,9 +31,15 @@ export class TightshortsService {
3031
token: this.configService.get('SHORT_API_KEY'),
3132
'Content-Type': 'application/json',
3233
},
34+
validateStatus: (status) =>
35+
(status >= 200 && status < 300) || status === 404,
3336
};
3437

35-
const api = (await axios(config)).data;
38+
const response = await axios(config);
39+
if (response.status === 404) {
40+
return null;
41+
}
42+
const api = response.data;
3643

3744
const currentShortVolume =
3845
((api.volume[0].shortVolume / api.volume[0].totalVolume) * 100).toFixed(

0 commit comments

Comments
 (0)