Skip to content

Commit 38670d7

Browse files
authored
[Job Launcher] Remove unnecessary data from logs (#3682)
1 parent 96ffffd commit 38670d7

File tree

8 files changed

+130
-89
lines changed

8 files changed

+130
-89
lines changed

packages/apps/dashboard/server/src/modules/stats/stats.service.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -392,47 +392,66 @@ export class StatsService implements OnModuleInit {
392392
return cachedHmtPrice;
393393
}
394394

395-
const headers = this.envConfigService.hmtPriceSourceApiKey
396-
? { 'x-cg-demo-api-key': this.envConfigService.hmtPriceSourceApiKey }
397-
: {};
395+
try {
396+
const headers = this.envConfigService.hmtPriceSourceApiKey
397+
? { 'x-cg-demo-api-key': this.envConfigService.hmtPriceSourceApiKey }
398+
: {};
398399

399-
const { data } = await lastValueFrom(
400-
this.httpService.get(this.envConfigService.hmtPriceSource, { headers }),
401-
);
400+
const { data } = await lastValueFrom(
401+
this.httpService.get(this.envConfigService.hmtPriceSource, { headers }),
402+
);
402403

403-
let hmtPrice: number;
404-
405-
if (this.envConfigService.hmtPriceSource.includes('coingecko')) {
406-
if (
407-
!data ||
408-
!data[this.envConfigService.hmtPriceFromKey] ||
409-
!data[this.envConfigService.hmtPriceFromKey][
410-
this.envConfigService.hmtPriceToKey
411-
]
412-
) {
413-
throw new Error('Failed to fetch HMT price from CoinGecko API');
404+
let hmtPrice: number;
405+
406+
if (this.envConfigService.hmtPriceSource.includes('coingecko')) {
407+
if (
408+
!data ||
409+
!data[this.envConfigService.hmtPriceFromKey] ||
410+
!data[this.envConfigService.hmtPriceFromKey][
411+
this.envConfigService.hmtPriceToKey
412+
]
413+
) {
414+
throw new Error('Failed to fetch HMT price from CoinGecko API');
415+
}
416+
hmtPrice = parseFloat(
417+
data[this.envConfigService.hmtPriceFromKey][
418+
this.envConfigService.hmtPriceToKey
419+
],
420+
);
421+
} else if (this.envConfigService.hmtPriceSource.includes('coinlore')) {
422+
if (
423+
!data ||
424+
!data[0] ||
425+
!data[0].price_usd ||
426+
data[0].symbol !== 'HMT'
427+
) {
428+
throw new Error('Failed to fetch HMT price from Coinlore API');
429+
}
430+
hmtPrice = parseFloat(data[0].price_usd);
431+
} else {
432+
throw new Error('Unsupported HMT price source');
414433
}
415-
hmtPrice = parseFloat(
416-
data[this.envConfigService.hmtPriceFromKey][
417-
this.envConfigService.hmtPriceToKey
418-
],
434+
435+
await this.cacheManager.set(
436+
this.redisConfigService.hmtPriceCacheKey,
437+
hmtPrice,
438+
this.redisConfigService.cacheHmtPriceTTL,
419439
);
420-
} else if (this.envConfigService.hmtPriceSource.includes('coinlore')) {
421-
if (!data || !data[0] || !data[0].price_usd || data[0].symbol !== 'HMT') {
422-
throw new Error('Failed to fetch HMT price from Coinlore API');
440+
441+
return hmtPrice;
442+
} catch (error) {
443+
let formattedError = error;
444+
if (error instanceof AxiosError) {
445+
formattedError = httpUtils.formatAxiosError(error);
423446
}
424-
hmtPrice = parseFloat(data[0].price_usd);
425-
} else {
426-
throw new Error('Unsupported HMT price source');
427-
}
428447

429-
await this.cacheManager.set(
430-
this.redisConfigService.hmtPriceCacheKey,
431-
hmtPrice,
432-
this.redisConfigService.cacheHmtPriceTTL,
433-
);
448+
this.logger.error('Failed to fetch HMT price', {
449+
source: this.envConfigService.hmtPriceSource,
450+
error: formattedError,
451+
});
434452

435-
return hmtPrice;
453+
throw error;
454+
}
436455
}
437456

438457
async hCaptchaStats(from: string, to: string): Promise<HcaptchaDailyStats[]> {

packages/apps/job-launcher/server/src/common/utils/hcaptcha.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
import { formatAxiosError } from './http';
23

34
export async function verifyToken(
45
url: string,
@@ -17,11 +18,18 @@ export async function verifyToken(
1718
queryParams.remoteip = ip;
1819
}
1920

20-
const { data } = await axios.post(
21-
`${url}/siteverify`,
22-
{},
23-
{ params: queryParams },
24-
);
21+
try {
22+
const { data } = await axios.post(
23+
`${url}/siteverify`,
24+
{},
25+
{ params: queryParams },
26+
);
2527

26-
return data;
28+
return data;
29+
} catch (error) {
30+
return {
31+
success: false,
32+
error: formatAxiosError(error),
33+
};
34+
}
2735
}

packages/apps/job-launcher/server/src/common/utils/slack.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios from 'axios';
22

33
import logger from '../../logger';
4+
import { formatAxiosError } from './http';
45

56
const slackLogger = logger.child({ context: 'sendSlackNotification' });
67

@@ -21,7 +22,9 @@ export async function sendSlackNotification(
2122
slackLogger.debug('Slack notification sent', payload);
2223
return true;
2324
} catch (error) {
24-
slackLogger.error('Error sending Slack notification', error);
25+
slackLogger.error('Error sending Slack notification', {
26+
error: formatAxiosError(error),
27+
});
2528
return false;
2629
}
2730
}

packages/apps/job-launcher/server/src/common/utils/storage.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HttpStatus } from '@nestjs/common';
2-
import axios from 'axios';
2+
import axios, { AxiosError } from 'axios';
33
import { parseString } from 'xml2js';
44
import { StorageDataDto } from '../../modules/job/job.dto';
55
import { ErrorBucket } from '../constants/errors';
@@ -10,6 +10,7 @@ import {
1010
GCS_HTTP_REGEX_PATH_BASED,
1111
GCS_HTTP_REGEX_SUBDOMAIN,
1212
} from './gcstorage';
13+
import { formatAxiosError } from './http';
1314

1415
export function generateBucketUrl(
1516
storageData: StorageDataDto,
@@ -140,7 +141,11 @@ export async function listObjectsInBucket(url: URL): Promise<string[]> {
140141
} while (nextContinuationToken);
141142
resolve(objects);
142143
} catch (err) {
143-
reject(err);
144+
let formatted = err;
145+
if (err instanceof AxiosError) {
146+
formatted = formatAxiosError(err);
147+
}
148+
reject(formatted);
144149
}
145150
});
146151
}

packages/apps/job-launcher/server/src/modules/auth/auth.service.ts

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
import { Injectable } from '@nestjs/common';
2+
import { Injectable, Logger } from '@nestjs/common';
33
import { JwtService } from '@nestjs/jwt';
44

55
import { ErrorAuth, ErrorUser } from '../../common/constants/errors';
@@ -39,6 +39,8 @@ import { ApiKeyRepository } from './apikey.repository';
3939

4040
@Injectable()
4141
export class AuthService {
42+
private readonly logger = new Logger(AuthService.name);
43+
4244
constructor(
4345
private readonly jwtService: JwtService,
4446
private readonly userService: UserService,
@@ -80,19 +82,7 @@ export class AuthService {
8082
}
8183

8284
public async signup(data: UserCreateDto, ip?: string): Promise<UserEntity> {
83-
if (
84-
!(
85-
await verifyToken(
86-
this.authConfigService.hcaptchaProtectionUrl,
87-
this.authConfigService.hCaptchaSiteKey,
88-
this.authConfigService.hCaptchaSecret,
89-
data.hCaptchaToken,
90-
ip,
91-
)
92-
).success
93-
) {
94-
throw new ForbiddenError(ErrorAuth.InvalidCaptchaToken);
95-
}
85+
await this.ensureCaptchaValid(data.hCaptchaToken, ip);
9686
const storedUser = await this.userRepository.findByEmail(data.email);
9787
if (storedUser) {
9888
throw new ConflictError(ErrorUser.DuplicatedEmail);
@@ -185,19 +175,7 @@ export class AuthService {
185175
data: ForgotPasswordDto,
186176
ip?: string,
187177
): Promise<void> {
188-
if (
189-
!(
190-
await verifyToken(
191-
this.authConfigService.hcaptchaProtectionUrl,
192-
this.authConfigService.hCaptchaSiteKey,
193-
this.authConfigService.hCaptchaSecret,
194-
data.hCaptchaToken,
195-
ip,
196-
)
197-
).success
198-
) {
199-
throw new ForbiddenError(ErrorAuth.InvalidCaptchaToken);
200-
}
178+
await this.ensureCaptchaValid(data.hCaptchaToken, ip);
201179
const userEntity = await this.userRepository.findByEmail(data.email);
202180

203181
if (!userEntity) {
@@ -245,19 +223,7 @@ export class AuthService {
245223
data: RestorePasswordDto,
246224
ip?: string,
247225
): Promise<void> {
248-
if (
249-
!(
250-
await verifyToken(
251-
this.authConfigService.hcaptchaProtectionUrl,
252-
this.authConfigService.hCaptchaSiteKey,
253-
this.authConfigService.hCaptchaSecret,
254-
data.hCaptchaToken,
255-
ip,
256-
)
257-
).success
258-
) {
259-
throw new ForbiddenError(ErrorAuth.InvalidCaptchaToken);
260-
}
226+
await this.ensureCaptchaValid(data.hCaptchaToken, ip);
261227

262228
const tokenEntity = await this.tokenRepository.findOneByUuidAndType(
263229
data.token,
@@ -426,4 +392,24 @@ export class AuthService {
426392

427393
return null;
428394
}
395+
396+
private async ensureCaptchaValid(token: string, ip?: string): Promise<void> {
397+
const verification = await verifyToken(
398+
this.authConfigService.hcaptchaProtectionUrl,
399+
this.authConfigService.hCaptchaSiteKey,
400+
this.authConfigService.hCaptchaSecret,
401+
token,
402+
ip,
403+
);
404+
405+
if (verification.success) {
406+
return;
407+
}
408+
409+
this.logger.error(ErrorAuth.InvalidCaptchaToken, {
410+
error: verification.error,
411+
});
412+
413+
throw new ForbiddenError(ErrorAuth.InvalidCaptchaToken);
414+
}
429415
}

packages/apps/job-launcher/server/src/modules/qualification/qualification.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { firstValueFrom } from 'rxjs';
55
import { Web3ConfigService } from '../../common/config/web3-config.service';
66
import { ErrorQualification, ErrorWeb3 } from '../../common/constants/errors';
77
import { ServerError } from '../../common/errors';
8+
import { formatAxiosError } from '../../common/utils/http';
9+
import logger from '../../logger';
810
import { Web3Service } from '../web3/web3.service';
911
import { QualificationDto } from './qualification.dto';
10-
import logger from '../../logger';
1112

1213
@Injectable()
1314
export class QualificationService {
@@ -48,9 +49,10 @@ export class QualificationService {
4849

4950
return data;
5051
} catch (error) {
52+
const formattedError = formatAxiosError(error);
5153
this.logger.error(
5254
'Error fetching qualifications from reputation oracle',
53-
error,
55+
{ error: formattedError },
5456
);
5557
throw new ServerError(ErrorQualification.FailedToFetchQualifications);
5658
}

packages/apps/job-launcher/server/src/modules/rate/rate.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ErrorCurrency } from '../../common/constants/errors';
77
import { CoingeckoTokenId } from '../../common/constants/payment';
88
import { EscrowFundToken } from '../../common/enums/job';
99
import { NotFoundError } from '../../common/errors';
10+
import { formatAxiosError } from '../../common/utils/http';
1011
import logger from '../../logger';
1112

1213
@Injectable()
@@ -77,10 +78,11 @@ export class RateService {
7778

7879
return finalRate;
7980
} catch (error) {
81+
const formattedError = formatAxiosError(error);
8082
this.logger.error('Error while getting rate', {
8183
from,
8284
to,
83-
error,
85+
error: formattedError,
8486
});
8587
throw new NotFoundError(ErrorCurrency.PairNotFound);
8688
}

packages/apps/reputation-oracle/server/src/integrations/hcaptcha/hcaptcha.service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { HttpService } from '@nestjs/axios';
22
import { Injectable } from '@nestjs/common';
3+
import { AxiosError } from 'axios';
34
import { ethers } from 'ethers';
45
import { firstValueFrom } from 'rxjs';
56

67
import { HCaptchaConfigService } from '@/config';
78
import logger from '@/logger';
9+
import { formatAxiosError } from '@/utils/http';
810

911
import {
1012
GetLabelerQueryParams,
@@ -59,7 +61,13 @@ export class HCaptchaService {
5961
});
6062
}
6163
} catch (error) {
62-
this.logger.error('Error occurred during token verification', error);
64+
let formattedError = error;
65+
if (error instanceof AxiosError) {
66+
formattedError = formatAxiosError(error);
67+
}
68+
this.logger.error('Error occurred during token verification', {
69+
error: formattedError,
70+
});
6371
}
6472

6573
return false;
@@ -105,8 +113,12 @@ export class HCaptchaService {
105113
});
106114
}
107115
} catch (error) {
116+
let formattedError = error;
117+
if (error instanceof AxiosError) {
118+
formattedError = formatAxiosError(error);
119+
}
108120
this.logger.error('Error occurred during labeling registration', {
109-
error,
121+
error: formattedError,
110122
...data,
111123
});
112124
}
@@ -134,8 +146,12 @@ export class HCaptchaService {
134146
return response.data;
135147
}
136148
} catch (error) {
149+
let formattedError = error;
150+
if (error instanceof AxiosError) {
151+
formattedError = formatAxiosError(error);
152+
}
137153
this.logger.error(`Error occurred while retrieving labeler data`, {
138-
error,
154+
error: formattedError,
139155
email,
140156
});
141157
}

0 commit comments

Comments
 (0)