diff --git a/src/httpClient.ts b/src/httpClient.ts index 174c623..429a957 100644 --- a/src/httpClient.ts +++ b/src/httpClient.ts @@ -50,8 +50,12 @@ export class HttpClient { } return responseMessage; } catch (error) { - console.error('Error decoding response message:', error); - console.error('Response Message Content:\n', responseText.split("'").join('\n')); + // The response carries account data (balances, bookings, names), so it is only + // printed when debugging was asked for + if (this.debug) { + console.error('Error decoding response message:', error); + console.error('Response Message Content:\n', responseText.split("'").join('\n')); + } throw error; } } else { diff --git a/src/tests/httpClientErrorLog.test.ts b/src/tests/httpClientErrorLog.test.ts new file mode 100644 index 0000000..0f2fd3c --- /dev/null +++ b/src/tests/httpClientErrorLog.test.ts @@ -0,0 +1,41 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { HttpClient } from '../httpClient.js'; +import { type CustomerMessage, Message } from '../message.js'; + +const message = { + encode: () => "HNHBK:1:3'", + toString: () => 'HNHBK', +} as unknown as CustomerMessage; + +describe('HttpClient with an undecodable response', () => { + afterEach(() => { + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + }); + + it('does not print the response (it holds account data) unless debugging', async () => { + const secret = "HIRMG:3:2+0010::Saldo 1.234,56 Max Mustermann DE99'"; + vi.stubGlobal( + 'fetch', + vi + .fn() + .mockImplementation( + async () => new Response(Buffer.from(`garbage${secret}`, 'latin1').toString('base64')), + ), + ); + vi.spyOn(Message, 'decode').mockImplementation(() => { + throw new Error('cannot decode'); + }); + const errors = vi.spyOn(console, 'error').mockImplementation(() => {}); + + await expect(new HttpClient('http://bank.example').sendMessage(message)).rejects.toThrow( + 'cannot decode', + ); + expect(errors).not.toHaveBeenCalled(); + + await expect(new HttpClient('http://bank.example', true).sendMessage(message)).rejects.toThrow( + 'cannot decode', + ); + expect(errors).toHaveBeenCalled(); + }); +});