taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 1c269d94a9e4af3a899363d17fd1363e2fa36881
parent f350c5022477ece9c7257cf5a85ce028d6e5e099
Author: Florian Dold <dold@taler.net>
Date:   Tue, 21 Jul 2026 02:54:11 +0200

util: test that unexpected-response errors carry an error code

Diffstat:
Apackages/taler-util/src/operation.test.ts | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+), 0 deletions(-)

diff --git a/packages/taler-util/src/operation.test.ts b/packages/taler-util/src/operation.test.ts @@ -0,0 +1,52 @@ +/* + This file is part of GNU Taler + (C) 2024 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import { test } from "node:test"; +import assert from "node:assert"; +import { TalerError } from "./errors.js"; +import { HttpResponse } from "./http-common.js"; +import { + opKnownFailure, + succeedOrThrow, + throwUnexpectedResponse, +} from "./operation.js"; +import { TalerErrorCode } from "./taler-error-codes.js"; + +function stubResponse(): HttpResponse { + return { + requestUrl: "http://example.com/", + requestMethod: "GET", + status: 404, + headers: new Headers(), + json: async () => ({}), + text: async () => "", + bytes: async () => new Uint8Array(), + }; +} + +const hasUnexpectedCode = (e: unknown): boolean => + e instanceof TalerError && + e.errorDetail.code === TalerErrorCode.GENERIC_UNEXPECTED_REQUEST_ERROR; + +test("succeedOrThrow builds a TalerError with a concrete error code", (t) => { + const fail = opKnownFailure(stubResponse(), 404); + assert.throws(() => succeedOrThrow(fail), hasUnexpectedCode); +}); + +test("throwUnexpectedResponse builds a TalerError with a concrete error code", (t) => { + const fail = opKnownFailure(stubResponse(), 404); + assert.throws(() => throwUnexpectedResponse(fail), hasUnexpectedCode); +});