import { ErrorType, HttpResponse, HttpResponsePaginated, notifyError, useTranslationContext, } from "@gnu-taler/web-util/browser"; import { VNode, h } from "preact"; import { Loading } from "./Loading.js"; import { HttpStatusCode, TranslatedString } from "@gnu-taler/taler-util"; import { AmlExchangeBackend } from "../types.js"; export function handleNotOkResult( i18n: ReturnType["i18n"], ): ( result: HttpResponsePaginated | HttpResponse, ) => VNode { return function handleNotOkResult2( result: HttpResponsePaginated | HttpResponse, ): VNode { if (result.loading) return ; if (!result.ok) { switch (result.type) { case ErrorType.TIMEOUT: { notifyError(i18n.str`Request timeout, try again later.`, undefined); break; } case ErrorType.CLIENT: { if (result.status === HttpStatusCode.Unauthorized) { notifyError(i18n.str`Wrong credentials`, undefined); return
not authorized
; } const errorData = result.payload; notifyError( i18n.str`Could not load due to a client error`, errorData.hint as TranslatedString, JSON.stringify(result), ); break; } case ErrorType.SERVER: { notifyError( i18n.str`Server returned with error`, result.payload.hint as TranslatedString, JSON.stringify(result.payload), ); break; } case ErrorType.UNREADABLE: { notifyError( i18n.str`Unexpected error.`, `Response from ${result.info?.url} is unreadable, http status: ${result.status}` as TranslatedString, JSON.stringify(result), ); break; } case ErrorType.UNEXPECTED: { notifyError( i18n.str`Unexpected error.`, `Diagnostic from ${result.info?.url} is "${result.message}"` as TranslatedString, JSON.stringify(result), ); break; } default: { assertUnreachable(result); } } return
error
; } return
; }; } export function assertUnreachable(x: never): never { throw new Error("Didn't expect to get here"); }