Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions src/tests/httpClientErrorLog.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});