1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2025-06-15 13:36:35 +02:00

Improve error handling + small refactoring.

This commit is contained in:
Bogdan Bodnar
2019-11-13 12:59:38 +01:00
parent 7e6e6f8c87
commit 23029470b9
2 changed files with 31 additions and 11 deletions

View File

@ -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]);