mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
fix(wanderer): log why an answer from wanderer was not understood
A failed wanderer call kept its reason in WandererError.detail (the field that did not match, or what came instead of JSON), and the client comment said it stays in the log, but nothing wrote it anywhere. The log only had the generic 'wanderer answered with something TREK does not understand', so neither an admin nor we could tell what wanderer had sent. Every refusal now logs its code, message and detail.
This commit is contained in:
@@ -2,6 +2,7 @@ import { WandererConnections } from '../../db/entities/WandererConnections.entit
|
||||
import type { WandererConnectionsRepository } from '../../db/repositories/WandererConnections.repository';
|
||||
import { decodePolyline, thinLine } from '../../utils/polyline';
|
||||
import { checkSsrf } from '../../utils/ssrfGuard';
|
||||
import { logWarn } from '../audit/audit-log.logger';
|
||||
import { AuditService } from '../audit/audit.service';
|
||||
import { decrypt_api_key, maybe_encrypt_api_key } from '../common/crypto/apiKeyCrypto';
|
||||
import { DomainError } from '../common/domain-error';
|
||||
@@ -183,9 +184,14 @@ export class WandererService {
|
||||
await this.connections.recordCheck(userId, ok, code);
|
||||
}
|
||||
|
||||
/** A failed call, with a rejected token noted on the connection so its card says so. */
|
||||
/**
|
||||
* A failed call, with a rejected token noted on the connection so its card
|
||||
* says so. The log gets the reason the client is not shown: which field of
|
||||
* wanderer's answer did not match, or what came instead of JSON.
|
||||
*/
|
||||
private async failed(userId: number, err: unknown): Promise<never> {
|
||||
if (!(err instanceof WandererError)) throw err;
|
||||
logWarn(`[wanderer] ${err.code}: ${err.message}${err.detail ? ` (${err.detail})` : ''}`);
|
||||
if (err.code === 'unauthorized') await this.noteCheck(userId, false, err.code);
|
||||
throw refusalOf(err);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vites
|
||||
|
||||
const { checkSsrf } = vi.hoisted(() => ({ checkSsrf: vi.fn() }));
|
||||
vi.mock('../../../src/utils/ssrfGuard', () => ({ checkSsrf, safeFetch: vi.fn() }));
|
||||
const { logWarn } = vi.hoisted(() => ({ logWarn: vi.fn() }));
|
||||
vi.mock('../../../src/nest/audit/audit-log.logger', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('../../../src/nest/audit/audit-log.logger')>()),
|
||||
logWarn,
|
||||
}));
|
||||
|
||||
const PUBLIC = { allowed: true, isPrivate: false };
|
||||
const PRIVATE = { allowed: false, isPrivate: true, resolvedIp: '192.168.1.5', error: 'private' };
|
||||
@@ -376,6 +381,23 @@ describe('WandererService reads for an import', () => {
|
||||
expect((await refusal(failing.service.findTrail(1, 'trail1'))).getStatus()).toBe(404);
|
||||
});
|
||||
|
||||
it('WAND-SVC-018b: the log gets the reason an answer was not understood, which the client is not shown', async () => {
|
||||
const { service, client } = setup(savedRow());
|
||||
logWarn.mockClear();
|
||||
client.ownActors.mockRejectedValueOnce(
|
||||
new WandererError(
|
||||
'invalid_response',
|
||||
'wanderer answered with something TREK does not understand',
|
||||
'hits.0.difficulty: expected number, received string',
|
||||
),
|
||||
);
|
||||
expect((await refusal(service.search(1, { q: '', page: 1 }))).getStatus()).toBe(502);
|
||||
expect(logWarn).toHaveBeenCalledWith(
|
||||
'[wanderer] invalid_response: wanderer answered with something TREK does not understand' +
|
||||
' (hits.0.difficulty: expected number, received string)',
|
||||
);
|
||||
});
|
||||
|
||||
it('WAND-SVC-019: a failure that is not wanderer answering is rethrown as it is', async () => {
|
||||
const { service, client } = setup(savedRow());
|
||||
client.trail.mockRejectedValueOnce(new TypeError('bug'));
|
||||
|
||||
Reference in New Issue
Block a user