From b831d664a93059f5c3915605cc742e559e61f889 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Tue, 12 Nov 2019 15:26:54 +0100 Subject: [PATCH 1/3] Add timout handling. --- src/api/hooks.js | 2 +- src/api/utils.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index a30bd8d..aa19988 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.js @@ -42,7 +42,7 @@ function createAPIHook(method) { dispatch({ type: API_ACTIONS.FAILURE, payload: getErrorMessage(error), - status: error.response.status, + status: error.response && error.response.status, }); } }, [url, contentType]); diff --git a/src/api/utils.js b/src/api/utils.js index 18a76e3..d727c80 100644 --- a/src/api/utils.js +++ b/src/api/utils.js @@ -53,9 +53,12 @@ function getCookie(name) { } export function getErrorMessage(error) { - let payload = "An unknown error occurred"; - if (error.response.headers["content-type"] === "application/json") { + let payload = _("An unknown error occurred."); + if (error.response && error.response.headers["content-type"] === "application/json") { payload = error.response.data; } + if (error.code === "ECONNABORTED") { + payload = _("Timeout error occurred."); + } return payload; } From 7e6e6f8c87cfacf071c7bea0a867fe7ec40a778f Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Tue, 12 Nov 2019 15:27:40 +0100 Subject: [PATCH 2/3] Use generic error message in the ForisForm. --- src/form/components/ForisForm.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/form/components/ForisForm.js b/src/form/components/ForisForm.js index f739f5b..86b552b 100644 --- a/src/form/components/ForisForm.js +++ b/src/form/components/ForisForm.js @@ -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 ; From 23029470b9abb101f39b0442b5e5d1057179f272 Mon Sep 17 00:00:00 2001 From: Bogdan Bodnar Date: Wed, 13 Nov 2019 12:59:38 +0100 Subject: [PATCH 3/3] Improve error handling + small refactoring. --- src/api/hooks.js | 16 +++++++++++----- src/api/utils.js | 26 ++++++++++++++++++++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/api/hooks.js b/src/api/hooks.js index aa19988..da236d0 100644 --- a/src/api/hooks.js +++ b/src/api/hooks.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 && error.response.status, + payload: getErrorPayload(error), }); } }, [url, contentType]); diff --git a/src/api/utils.js b/src/api/utils.js index d727c80..8e6c23b 100644 --- a/src/api/utils.js +++ b/src/api/utils.js @@ -52,13 +52,27 @@ function getCookie(name) { return cookieValue; } -export function getErrorMessage(error) { - let payload = _("An unknown error occurred."); - if (error.response && error.response.headers["content-type"] === "application/json") { - payload = error.response.data; +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") { - payload = _("Timeout error occurred."); + return _("Timeout error occurred."); } - return payload; + 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") { + return error.response.data; + } + return _("An unknown API error occurred."); }