Skip to content

Commit 935cbf9

Browse files
committed
enhance error handling and logging in HCaptcha and stats services
1 parent 33b4c78 commit 935cbf9

File tree

3 files changed

+79
-39
lines changed

3 files changed

+79
-39
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/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/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)