show reducer network errors

This commit is contained in:
Florian Dold 2021-10-14 17:08:41 +02:00
parent 773e025b6c
commit 90f4a4e655
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -1,3 +1,4 @@
import { TalerErrorCode } from "@gnu-taler/taler-util";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
export type ReducerState = export type ReducerState =
@ -138,13 +139,44 @@ export enum RecoveryStates {
const reducerBaseUrl = "http://localhost:5000/"; const reducerBaseUrl = "http://localhost:5000/";
async function getBackupStartState(): Promise<ReducerState> { async function getBackupStartState(): Promise<ReducerState> {
const resp = await fetch(new URL("start-backup", reducerBaseUrl).href); let resp: Response;
return await resp.json();
try {
resp = await fetch(new URL("start-backup", reducerBaseUrl).href);
} catch (e) {
return {
code: TalerErrorCode.ANASTASIS_REDUCER_NETWORK_FAILED,
message: `Network request to remote reducer ${reducerBaseUrl} failed`,
} as any;
}
try {
return await resp.json();
} catch (e) {
return {
code: TalerErrorCode.ANASTASIS_REDUCER_NETWORK_FAILED,
message: `Could not parse response from reducer`,
} as any;
}
} }
async function getRecoveryStartState(): Promise<ReducerState> { async function getRecoveryStartState(): Promise<ReducerState> {
const resp = await fetch(new URL("start-recovery", reducerBaseUrl).href); let resp: Response;
return await resp.json(); try {
resp = await fetch(new URL("start-recovery", reducerBaseUrl).href);
} catch (e) {
return {
code: TalerErrorCode.ANASTASIS_REDUCER_NETWORK_FAILED,
message: `Network request to remote reducer ${reducerBaseUrl} failed`,
} as any;
}
try {
return await resp.json();
} catch (e) {
return {
code: TalerErrorCode.ANASTASIS_REDUCER_NETWORK_FAILED,
message: `Could not parse response from reducer`,
} as any;
}
} }
async function reduceState( async function reduceState(
@ -152,19 +184,34 @@ async function reduceState(
action: string, action: string,
args: any, args: any,
): Promise<ReducerState> { ): Promise<ReducerState> {
const resp = await fetch(new URL("action", reducerBaseUrl).href, { let resp: Response;
method: "POST", try {
headers: { resp = await fetch(new URL("action", reducerBaseUrl).href, {
Accept: "application/json", method: "POST",
"Content-Type": "application/json", headers: {
}, Accept: "application/json",
body: JSON.stringify({ "Content-Type": "application/json",
state, },
action, body: JSON.stringify({
arguments: args, state,
}), action,
}); arguments: args,
return resp.json(); }),
});
} catch (e) {
return {
code: TalerErrorCode.ANASTASIS_REDUCER_NETWORK_FAILED,
message: `Network request to remote reducer ${reducerBaseUrl} failed`,
} as any;
}
try {
return await resp.json();
} catch (e) {
return {
code: TalerErrorCode.ANASTASIS_REDUCER_NETWORK_FAILED,
message: `Could not parse response from reducer`,
} as any;
}
} }
export interface ReducerTransactionHandle { export interface ReducerTransactionHandle {
@ -244,19 +291,33 @@ export function useAnastasisReducer(): AnastasisReducerApi {
currentError: anastasisState.currentError, currentError: anastasisState.currentError,
async startBackup() { async startBackup() {
const s = await getBackupStartState(); const s = await getBackupStartState();
setAnastasisState({ if (s.code !== undefined) {
...anastasisState, setAnastasisState({
currentError: undefined, ...anastasisState,
reducerState: s, currentError: s,
}); });
} else {
setAnastasisState({
...anastasisState,
currentError: undefined,
reducerState: s,
});
}
}, },
async startRecover() { async startRecover() {
const s = await getRecoveryStartState(); const s = await getRecoveryStartState();
setAnastasisState({ if (s.code !== undefined) {
...anastasisState, setAnastasisState({
currentError: undefined, ...anastasisState,
reducerState: s, currentError: s,
}); });
} else {
setAnastasisState({
...anastasisState,
currentError: undefined,
reducerState: s,
});
}
}, },
transition(action: string, args: any) { transition(action: string, args: any) {
doTransition(action, args); doTransition(action, args);