diff options
author | Sebastian <sebasjm@gmail.com> | 2024-03-11 14:57:35 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-03-11 14:57:49 -0300 |
commit | 9c16a25883e9dc03768bfbef6da330260149d74f (patch) | |
tree | 12cd49f55f3e8c962ce41c76cee79a77d1ecb6e0 /packages/taler-util/src | |
parent | 6e02a3852590f39cdd414a1caf89506bcd9dc83a (diff) | |
download | wallet-core-9c16a25883e9dc03768bfbef6da330260149d74f.tar.gz wallet-core-9c16a25883e9dc03768bfbef6da330260149d74f.tar.bz2 wallet-core-9c16a25883e9dc03768bfbef6da330260149d74f.zip |
adding id and time to obs event, plus pretty
Diffstat (limited to 'packages/taler-util/src')
-rw-r--r-- | packages/taler-util/src/errors.ts | 7 | ||||
-rw-r--r-- | packages/taler-util/src/http-client/bank-conversion.ts | 10 | ||||
-rw-r--r-- | packages/taler-util/src/http-client/bank-core.ts | 16 | ||||
-rw-r--r-- | packages/taler-util/src/http-client/bank-integration.ts | 5 | ||||
-rw-r--r-- | packages/taler-util/src/http-client/types.ts | 10 | ||||
-rw-r--r-- | packages/taler-util/src/http-client/utils.ts | 4 | ||||
-rw-r--r-- | packages/taler-util/src/notifications.ts | 12 | ||||
-rw-r--r-- | packages/taler-util/src/observability.ts | 46 | ||||
-rw-r--r-- | packages/taler-util/src/operation.ts | 5 |
9 files changed, 92 insertions, 23 deletions
diff --git a/packages/taler-util/src/errors.ts b/packages/taler-util/src/errors.ts index c5110bda8..11f01a3fe 100644 --- a/packages/taler-util/src/errors.ts +++ b/packages/taler-util/src/errors.ts @@ -93,6 +93,11 @@ export interface DetailsMap { requestMethod: string; timeoutMs: number; }; + [TalerErrorCode.GENERIC_TIMEOUT]: { + requestUrl: string; + requestMethod: string; + timeoutMs: number; + }; [TalerErrorCode.WALLET_NETWORK_ERROR]: { requestUrl: string; requestMethod: string; @@ -112,7 +117,7 @@ export interface DetailsMap { operation: string; error: string; detail: TalerErrorDetail | undefined; - } + }; [TalerErrorCode.WALLET_EXCHANGE_COIN_SIGNATURE_INVALID]: empty; [TalerErrorCode.WALLET_WITHDRAWAL_GROUP_INCOMPLETE]: { numErrors: number; diff --git a/packages/taler-util/src/http-client/bank-conversion.ts b/packages/taler-util/src/http-client/bank-conversion.ts index ea247ccfc..3db9df101 100644 --- a/packages/taler-util/src/http-client/bank-conversion.ts +++ b/packages/taler-util/src/http-client/bank-conversion.ts @@ -39,7 +39,11 @@ import { codecForCashoutConversionResponse, codecForConversionBankConfig, } from "./types.js"; -import { CacheEvictor, makeBearerTokenAuthHeader, nullEvictor } from "./utils.js"; +import { + CacheEvictor, + makeBearerTokenAuthHeader, + nullEvictor, +} from "./utils.js"; export type TalerBankConversionResultByMethod< prop extends keyof TalerBankConversionHttpClient, @@ -203,7 +207,9 @@ export class TalerBankConversionHttpClient { }); switch (resp.status) { case HttpStatusCode.NoContent: { - this.cacheEvictor.notifySuccess(TalerBankConversionCacheEviction.UPDATE_RATE); + this.cacheEvictor.notifySuccess( + TalerBankConversionCacheEviction.UPDATE_RATE, + ); return opEmptySuccess(resp); } case HttpStatusCode.Unauthorized: diff --git a/packages/taler-util/src/http-client/bank-core.ts b/packages/taler-util/src/http-client/bank-core.ts index b9fd6da80..c02bf1ec9 100644 --- a/packages/taler-util/src/http-client/bank-core.ts +++ b/packages/taler-util/src/http-client/bank-core.ts @@ -153,7 +153,9 @@ export class TalerCoreBankHttpClient { }); switch (resp.status) { case HttpStatusCode.Ok: { - await this.cacheEvictor.notifySuccess(TalerCoreBankCacheEviction.CREATE_ACCOUNT) + await this.cacheEvictor.notifySuccess( + TalerCoreBankCacheEviction.CREATE_ACCOUNT, + ); return opSuccessFromHttp(resp, codecForRegisterAccountResponse()); } case HttpStatusCode.BadRequest: @@ -440,7 +442,10 @@ export class TalerCoreBankHttpClient { }); switch (resp.status) { case HttpStatusCode.Ok: - return opSuccessFromHttp(resp, codecForBankAccountTransactionsResponse()); + return opSuccessFromHttp( + resp, + codecForBankAccountTransactionsResponse(), + ); case HttpStatusCode.NoContent: return opFixedSuccess({ transactions: [] }); case HttpStatusCode.Unauthorized: @@ -516,6 +521,8 @@ export class TalerCoreBankHttpClient { const body = await resp.json(); const details = codecForTalerErrorDetail().decode(body); switch (details.code) { + case TalerErrorCode.BANK_ADMIN_CREDITOR: + return opKnownTalerFailure(details.code, resp); case TalerErrorCode.BANK_SAME_ACCOUNT: return opKnownTalerFailure(details.code, resp); case TalerErrorCode.BANK_UNKNOWN_CREDITOR: @@ -553,7 +560,10 @@ export class TalerCoreBankHttpClient { }); switch (resp.status) { case HttpStatusCode.Ok: - return opSuccessFromHttp(resp, codecForBankAccountCreateWithdrawalResponse()); + return opSuccessFromHttp( + resp, + codecForBankAccountCreateWithdrawalResponse(), + ); case HttpStatusCode.NotFound: return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.Conflict: diff --git a/packages/taler-util/src/http-client/bank-integration.ts b/packages/taler-util/src/http-client/bank-integration.ts index a224c1f09..f63fa4445 100644 --- a/packages/taler-util/src/http-client/bank-integration.ts +++ b/packages/taler-util/src/http-client/bank-integration.ts @@ -129,7 +129,10 @@ export class TalerBankIntegrationHttpClient { }); switch (resp.status) { case HttpStatusCode.Ok: - return opSuccessFromHttp(resp, codecForBankWithdrawalOperationPostResponse()); + return opSuccessFromHttp( + resp, + codecForBankWithdrawalOperationPostResponse(), + ); case HttpStatusCode.NotFound: return opKnownHttpFailure(resp.status, resp); case HttpStatusCode.Conflict: { diff --git a/packages/taler-util/src/http-client/types.ts b/packages/taler-util/src/http-client/types.ts index 22272d12f..132ca867d 100644 --- a/packages/taler-util/src/http-client/types.ts +++ b/packages/taler-util/src/http-client/types.ts @@ -17,7 +17,11 @@ import { import { PaytoString, codecForPaytoString } from "../payto.js"; import { AmountString } from "../taler-types.js"; import { TalerActionString, codecForTalerActionString } from "../taleruri.js"; -import { TalerProtocolDuration, TalerProtocolTimestamp, codecForTimestamp } from "../time.js"; +import { + TalerProtocolDuration, + TalerProtocolTimestamp, + codecForTimestamp, +} from "../time.js"; export type UserAndPassword = { username: string; @@ -163,9 +167,9 @@ type ImageDataUrl = string; type WadId = string; -type Timestamp = TalerProtocolTimestamp +type Timestamp = TalerProtocolTimestamp; -type RelativeTime = TalerProtocolDuration +type RelativeTime = TalerProtocolDuration; export interface LoginToken { token: AccessToken; diff --git a/packages/taler-util/src/http-client/utils.ts b/packages/taler-util/src/http-client/utils.ts index 2b8920b66..f925a5610 100644 --- a/packages/taler-util/src/http-client/utils.ts +++ b/packages/taler-util/src/http-client/utils.ts @@ -71,5 +71,5 @@ export interface CacheEvictor<T> { } export const nullEvictor: CacheEvictor<unknown> = { - notifySuccess: () => Promise.resolve() -} + notifySuccess: () => Promise.resolve(), +}; diff --git a/packages/taler-util/src/notifications.ts b/packages/taler-util/src/notifications.ts index ab28cc2ee..f439b4a6f 100644 --- a/packages/taler-util/src/notifications.ts +++ b/packages/taler-util/src/notifications.ts @@ -22,6 +22,8 @@ /** * Imports. */ +import { CancellationToken } from "./CancellationToken.js"; +import { AbsoluteTime } from "./time.js"; import { TransactionState } from "./transactions-types.js"; import { ExchangeEntryState, TalerErrorDetail } from "./wallet-types.js"; @@ -132,15 +134,21 @@ export enum ObservabilityEventType { export type ObservabilityEvent = | { + id: string; + when: AbsoluteTime; type: ObservabilityEventType.HttpFetchStart; url: string; } | { + id: string; + when: AbsoluteTime; type: ObservabilityEventType.HttpFetchFinishSuccess; url: string; status: number; } | { + id: string; + when: AbsoluteTime; type: ObservabilityEventType.HttpFetchFinishError; url: string; error: TalerErrorDetail; @@ -207,9 +215,9 @@ export interface BackupOperationErrorNotification { error: TalerErrorDetail; } /** - * This notification is required to signal UI that + * This notification is required to signal UI that * the withdrawal operation changed the state. - * + * * https://bugs.gnunet.org/view.php?id=8099 */ export interface WithdrawalOperationTransitionNotification { diff --git a/packages/taler-util/src/observability.ts b/packages/taler-util/src/observability.ts index 198dcbe6e..f40967c52 100644 --- a/packages/taler-util/src/observability.ts +++ b/packages/taler-util/src/observability.ts @@ -14,9 +14,17 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { ObservabilityEvent } from "./index.js"; -import { HttpRequestLibrary, HttpRequestOptions, HttpResponse } from "./http-common.js"; -import { ObservabilityEventType } from "./notifications.js" +import { + AbsoluteTime, + CancellationToken, + ObservabilityEvent, +} from "./index.js"; +import { + HttpRequestLibrary, + HttpRequestOptions, + HttpResponse, +} from "./http-common.js"; +import { ObservabilityEventType } from "./notifications.js"; import { getErrorDetailFromException } from "./errors.js"; /** @@ -27,22 +35,48 @@ export interface ObservabilityContext { observe(evt: ObservabilityEvent): void; } +let seqId = 1000; + export class ObservableHttpClientLibrary implements HttpRequestLibrary { + private readonly currentRequest = new Map<string, CancellationToken.Source>(); constructor( private impl: HttpRequestLibrary, private oc: ObservabilityContext, - ) { } + ) {} + + public cancelRequest(id: string) { + const cancelator = this.currentRequest.get(id); + if (!cancelator) return; + cancelator.cancel(); + } + async fetch( url: string, opt?: HttpRequestOptions | undefined, ): Promise<HttpResponse> { + const id = `req-${seqId}`; + seqId = seqId + 1; + + const cancelator = CancellationToken.create(); + if (opt?.cancellationToken) { + opt.cancellationToken.onCancelled(cancelator.cancel); + } + this.currentRequest.set(id, cancelator); + this.oc.observe({ + id, + when: AbsoluteTime.now(), type: ObservabilityEventType.HttpFetchStart, url: url, }); + + const optsWithCancel = opt ?? {}; + optsWithCancel.cancellationToken = cancelator.token; try { - const res = await this.impl.fetch(url, opt); + const res = await this.impl.fetch(url, optsWithCancel); this.oc.observe({ + id, + when: AbsoluteTime.now(), type: ObservabilityEventType.HttpFetchFinishSuccess, url, status: res.status, @@ -50,6 +84,8 @@ export class ObservableHttpClientLibrary implements HttpRequestLibrary { return res; } catch (e) { this.oc.observe({ + id, + when: AbsoluteTime.now(), type: ObservabilityEventType.HttpFetchFinishError, url, error: getErrorDetailFromException(e), diff --git a/packages/taler-util/src/operation.ts b/packages/taler-util/src/operation.ts index ecf4a020a..0debd09fd 100644 --- a/packages/taler-util/src/operation.ts +++ b/packages/taler-util/src/operation.ts @@ -174,10 +174,7 @@ export function narrowOpSuccessOrThrow<Body, ErrorEnum>( { operation: opName, error: String(opRes.case), - detail: - "detail" in opRes - ? opRes.detail - : undefined, + detail: "detail" in opRes ? opRes.detail : undefined, }, `Operation ${opName} failed: ${String(opRes.case)}`, ); |