Merge branch 'timeout' into 'dev'

Timeout handling

See merge request turris/reforis/foris-js!36
merge-requests/160/merge
Bogdan Bodnar 5 years ago
commit 7075592f24
  1. 18
      src/api/hooks.js
  2. 25
      src/api/utils.js
  3. 4
      src/form/components/ForisForm.js

@ -5,11 +5,11 @@
* See /LICENSE for more information.
*/
import { useReducer, useCallback } from "react";
import { useCallback, useReducer } from "react";
import { ForisURLs } from "forisUrls";
import {
API_STATE, API_ACTIONS, API_METHODS, TIMEOUT, HEADERS, getErrorMessage,
API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT,
} from "./utils";
const DATA_METHODS = ["POST", "PATCH", "PUT"];
@ -30,19 +30,25 @@ function createAPIHook(method) {
dispatch({ type: API_ACTIONS.INIT });
try {
const request = API_METHODS[method];
const config = { timeout: TIMEOUT, headers };
const config = {
timeout: TIMEOUT,
headers,
};
let result;
if (DATA_METHODS.includes(method)) {
result = await request(url, data, config);
} else {
result = await request(url, config);
}
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
dispatch({
type: API_ACTIONS.SUCCESS,
payload: result.data,
});
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: getErrorMessage(error),
status: error.response.status,
status: error.response && error.response.status,
payload: getErrorPayload(error),
});
}
}, [url, contentType]);

@ -52,10 +52,27 @@ function getCookie(name) {
return cookieValue;
}
export function getErrorMessage(error) {
let payload = "An unknown error occurred";
export function getErrorPayload(error) {
if (error.response) {
if (error.response.status === 403) {
return _("The session is expired. Please log in again.");
}
return getJSONErrorMessage(error);
}
if (error.code === "ECONNABORTED") {
return _("Timeout error occurred.");
}
if (error.request) {
return _("No response received.");
}
/* eslint no-console: "off" */
console.error(error);
return _("An unknown error occurred. Check the console for more info.");
}
export function getJSONErrorMessage(error) {
if (error.response.headers["content-type"] === "application/json") {
payload = error.response.data;
return error.response.data;
}
return payload;
return _("An unknown API error occurred.");
}

@ -87,9 +87,9 @@ export function ForisForm({
postCallback();
setAlert(_("Settings saved successfully"), ALERT_TYPES.SUCCESS);
} else if (postState.state === API_STATE.ERROR) {
setAlert(_("Cannot save settings"));
setAlert(postState.data);
}
}, [postCallback, postState.state, setAlert]);
}, [postCallback, postState.state, postState.data, setAlert]);
if (forisModuleState.state === API_STATE.ERROR) {
return <ErrorMessage />;

Loading…
Cancel
Save