From 217a576e16bcb8c90b72c9eabab20abcc9bb3ae3 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 25 Oct 2026 17:27:33 +0100 Subject: [PATCH] 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. --- server/src/nest/wanderer/wanderer.service.ts | 8 ++++++- .../tests/unit/nest/wanderer.service.test.ts | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/server/src/nest/wanderer/wanderer.service.ts b/server/src/nest/wanderer/wanderer.service.ts index 61cf5ccf2..5cc3de4d7 100644 --- a/server/src/nest/wanderer/wanderer.service.ts +++ b/server/src/nest/wanderer/wanderer.service.ts @@ -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 { 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); } diff --git a/server/tests/unit/nest/wanderer.service.test.ts b/server/tests/unit/nest/wanderer.service.test.ts index e19de592e..147465a64 100644 --- a/server/tests/unit/nest/wanderer.service.test.ts +++ b/server/tests/unit/nest/wanderer.service.test.ts @@ -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()), + 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'));