Log analysis job failures

This commit is contained in:
dahyeon committed 2026-07-02 22:21:14 -07:00
1 parent f447e0c6ab
commit 9d43cc266c
4 files changed
+105 -2

No files matched your search

@@ -1,7 +1,8 @@
import { Logger } from '@nestjs/common';
import { FEATURE_SCHEMA_VERSION } from '@persona-lenz/shared'; import { FEATURE_SCHEMA_VERSION } from '@persona-lenz/shared';
import type { ConnectionService } from '../global/database/connection.service'; import type { ConnectionService } from '../global/database/connection.service';
import type { Account } from '../global/database/schemas'; import type { Account } from '../global/database/schemas';
import type { RiotApiClient } from '../riot/riot-api.client'; import { RiotNotFoundError, type RiotApiClient } from '../riot/riot-api.client';
import type { UserAnalysisJobData } from './analysis.types'; import type { UserAnalysisJobData } from './analysis.types';
import { FeatureExtractorService } from './feature-extractor.service'; import { FeatureExtractorService } from './feature-extractor.service';
import { AnalysisPipelineService } from './analysis-pipeline.service'; import { AnalysisPipelineService } from './analysis-pipeline.service';
@@ -114,6 +115,12 @@ function createStoredAccount(overrides: Partial<Account> = {}): Account {
}; };
} }
function suppressLoggerError() {
return jest
.spyOn(Logger.prototype, 'error')
.mockImplementation(() => undefined);
}
interface PipelineHarnessOptions { interface PipelineHarnessOptions {
accountError?: Error; accountError?: Error;
existingMatchRaw?: Record<string, unknown> | null; existingMatchRaw?: Record<string, unknown> | null;
@@ -334,6 +341,7 @@ describe('AnalysisPipelineService', () => {
}); });
it('marks the job failed with a public error on the final BullMQ attempt', async () => { it('marks the job failed with a public error on the final BullMQ attempt', async () => {
const loggerErrorSpy = suppressLoggerError();
const { service, updateAnalysisJob } = createPipelineHarness({ const { service, updateAnalysisJob } = createPipelineHarness({
accountError: new Error('internal host redis.local refused connection'), accountError: new Error('internal host redis.local refused connection'),
}); });
@@ -351,5 +359,51 @@ describe('AnalysisPipelineService', () => {
status: 'failed', status: 'failed',
error: 'Analysis failed during processing.', error: 'Analysis failed during processing.',
}); });
loggerErrorSpy.mockRestore();
});
it('marks Riot account lookup misses with a public not-found error', async () => {
const loggerErrorSpy = suppressLoggerError();
const { service, updateAnalysisJob } = createPipelineHarness({
accountError: new RiotNotFoundError(404),
});
await expect(
service.processUserAnalysisJob(createJobData(), 'analysis-job-1', {
isFinalAttempt: true,
}),
).rejects.toThrow(RiotNotFoundError);
const failedJobUpdate = updateAnalysisJob.mock.calls.find(
([, update]) => asRecord(asRecord(update).$set).status === 'failed',
);
expect(asRecord(asRecord(failedJobUpdate?.[1]).$set)).toMatchObject({
status: 'failed',
error: 'Riot account was not found for the selected region.',
});
loggerErrorSpy.mockRestore();
});
it('logs final pipeline failures with job context', async () => {
const loggerErrorSpy = suppressLoggerError();
const { service } = createPipelineHarness({
accountError: new Error('internal host redis.local refused connection'),
});
await expect(
service.processUserAnalysisJob(createJobData(), 'analysis-job-1', {
isFinalAttempt: true,
}),
).rejects.toThrow('internal host redis.local refused connection');
expect(loggerErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('Analysis job failed jobId="analysis-job-1"'),
expect.any(String),
);
expect(loggerErrorSpy.mock.calls[0][0]).toContain(
'publicError="Analysis failed during processing."',
);
loggerErrorSpy.mockRestore();
}); });
}); });
@@ -8,6 +8,7 @@ import type {
} from '../global/database/schemas'; } from '../global/database/schemas';
import { import {
RiotApiClient, RiotApiClient,
RiotNotFoundError,
type RiotLeagueEntryDto, type RiotLeagueEntryDto,
} from '../riot/riot-api.client'; } from '../riot/riot-api.client';
import { import {
@@ -144,9 +145,11 @@ export class AnalysisPipelineService {
await this.completeJob(jobId); await this.completeJob(jobId);
} catch (error) { } catch (error) {
if (options.isFinalAttempt === true) { if (options.isFinalAttempt === true) {
const publicError = this.publicErrorMessage(error);
this.logFinalFailure(normalizedData, jobId, publicError, error);
await this.updateJob(jobId, { await this.updateJob(jobId, {
status: 'failed', status: 'failed',
error: this.publicErrorMessage(error), error: publicError,
}); });
} }
throw error; throw error;
@@ -447,9 +450,31 @@ export class AnalysisPipelineService {
) { ) {
return 'Riot API key is invalid or expired.'; return 'Riot API key is invalid or expired.';
} }
if (error instanceof RiotNotFoundError) {
return 'Riot account was not found for the selected region.';
}
return 'Analysis failed during processing.'; return 'Analysis failed during processing.';
} }
private logFinalFailure(
data: UserAnalysisJobData,
jobId: string,
publicError: string,
error: unknown,
): void {
this.logger.error(
[
`Analysis job failed jobId="${this.safeLogText(jobId)}"`,
`gameName="${this.safeLogText(data.gameName)}"`,
`tagLine="${this.safeLogText(data.tagLine)}"`,
`platformRoute="${data.platformRoute}"`,
`publicError="${this.safeLogText(publicError)}"`,
`cause="${this.safeLogText(errorMessage(error))}"`,
].join(' '),
error instanceof Error ? error.stack : undefined,
);
}
private safeLogText(value: string): string { private safeLogText(value: string): string {
return value.replace(/[\r\n\t]/gu, ' ').slice(0, 300); return value.replace(/[\r\n\t]/gu, ' ').slice(0, 300);
} }
@@ -33,6 +33,29 @@ describe('AnalysisReadService', () => {
expect(findOne).toHaveBeenCalledWith({ jobId: 'analysis-job-1' }); expect(findOne).toHaveBeenCalledWith({ jobId: 'analysis-job-1' });
}); });
it('preserves public Riot account not-found job errors', async () => {
const findOne = jest.fn(() =>
createLeanExec({
error: 'Riot account was not found for the selected region.',
jobId: 'analysis-job-1',
progress: { fetchedMatches: 0, totalMatches: 0 },
stage: 'resolve-account',
status: 'failed',
}),
);
const service = new AnalysisReadService({
getModel: jest.fn().mockResolvedValue({ findOne }),
} as unknown as ConnectionService);
await expect(service.getJobStatus('analysis-job-1')).resolves.toEqual({
error: 'Riot account was not found for the selected region.',
jobId: 'analysis-job-1',
progress: { fetchedMatches: 0, totalMatches: 0 },
stage: 'resolve-account',
status: 'failed',
});
});
it('distinguishes analyzed accounts with no ranked games from missing accounts', async () => { it('distinguishes analyzed accounts with no ranked games from missing accounts', async () => {
const accountFindOne = jest.fn(() => const accountFindOne = jest.fn(() =>
createLeanExec({ createLeanExec({
@@ -169,6 +169,7 @@ export class AnalysisReadService {
if ( if (
error === 'Analysis failed during processing.' || error === 'Analysis failed during processing.' ||
error === 'Riot API key is invalid or expired.' || error === 'Riot API key is invalid or expired.' ||
error === 'Riot account was not found for the selected region.' ||
error === 'Analysis could not be queued.' error === 'Analysis could not be queued.'
) { ) {
return error; return error;