show reducer network errors
This commit is contained in:
parent
773e025b6c
commit
90f4a4e655
@ -1,3 +1,4 @@
|
||||
import { TalerErrorCode } from "@gnu-taler/taler-util";
|
||||
import { useState } from "preact/hooks";
|
||||
|
||||
export type ReducerState =
|
||||
@ -138,13 +139,44 @@ export enum RecoveryStates {
|
||||
const reducerBaseUrl = "http://localhost:5000/";
|
||||
|
||||
async function getBackupStartState(): Promise<ReducerState> {
|
||||
const resp = await fetch(new URL("start-backup", reducerBaseUrl).href);
|
||||
let resp: Response;
|
||||
|
||||
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> {
|
||||
const resp = await fetch(new URL("start-recovery", reducerBaseUrl).href);
|
||||
let resp: Response;
|
||||
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(
|
||||
@ -152,7 +184,9 @@ async function reduceState(
|
||||
action: string,
|
||||
args: any,
|
||||
): Promise<ReducerState> {
|
||||
const resp = await fetch(new URL("action", reducerBaseUrl).href, {
|
||||
let resp: Response;
|
||||
try {
|
||||
resp = await fetch(new URL("action", reducerBaseUrl).href, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
@ -164,7 +198,20 @@ async function reduceState(
|
||||
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 {
|
||||
@ -244,19 +291,33 @@ export function useAnastasisReducer(): AnastasisReducerApi {
|
||||
currentError: anastasisState.currentError,
|
||||
async startBackup() {
|
||||
const s = await getBackupStartState();
|
||||
if (s.code !== undefined) {
|
||||
setAnastasisState({
|
||||
...anastasisState,
|
||||
currentError: s,
|
||||
});
|
||||
} else {
|
||||
setAnastasisState({
|
||||
...anastasisState,
|
||||
currentError: undefined,
|
||||
reducerState: s,
|
||||
});
|
||||
}
|
||||
},
|
||||
async startRecover() {
|
||||
const s = await getRecoveryStartState();
|
||||
if (s.code !== undefined) {
|
||||
setAnastasisState({
|
||||
...anastasisState,
|
||||
currentError: s,
|
||||
});
|
||||
} else {
|
||||
setAnastasisState({
|
||||
...anastasisState,
|
||||
currentError: undefined,
|
||||
reducerState: s,
|
||||
});
|
||||
}
|
||||
},
|
||||
transition(action: string, args: any) {
|
||||
doTransition(action, args);
|
||||
|
Loading…
Reference in New Issue
Block a user