Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(wanderer): log why an answer from wanderer was not understood

Maurice217a576e16bc
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<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);
}
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<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'));