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

Filter non-JSON payload

This commit is contained in:
Maciej Lenartowicz
2019-10-01 09:38:18 +00:00
parent 8479518fab
commit 96eed02d32
7 changed files with 17 additions and 13 deletions

View File

@ -9,7 +9,7 @@ import { useReducer, useCallback } from "react";
import axios from "axios";
import {
API_ACTIONS, TIMEOUT, HEADERS, APIReducer,
API_ACTIONS, TIMEOUT, HEADERS, APIReducer, getErrorMessage,
} from "./utils";
export function useAPIDelete(url) {
@ -31,7 +31,7 @@ export function useAPIDelete(url) {
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
payload: getErrorMessage(error),
status: error.response.status,
});
}

View File

@ -9,7 +9,7 @@ import { useReducer } from "react";
import axios from "axios";
import {
API_ACTIONS, TIMEOUT, HEADERS, APIReducer,
API_ACTIONS, TIMEOUT, HEADERS, APIReducer, getErrorMessage,
} from "./utils";
export function useAPIPatch(url) {
@ -31,7 +31,7 @@ export function useAPIPatch(url) {
} catch (error) {
dispatch({
type: API_ACTIONS.FAILURE,
payload: error.response.data,
payload: getErrorMessage(error),
status: error.response.status,
});
}

View File

@ -9,7 +9,7 @@ import { useReducer } from "react";
import axios from "axios";
import {
API_ACTIONS, TIMEOUT, HEADERS, APIReducer,
API_ACTIONS, TIMEOUT, HEADERS, APIReducer, getErrorMessage,
} from "./utils";
export function useAPIPost(url) {
@ -29,14 +29,10 @@ export function useAPIPost(url) {
});
dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data });
} catch (error) {
let payload = "An unknown error occurred";
if (error.response.headers["content-type"] === "application/json") {
payload = error.response.data;
}
dispatch({
type: API_ACTIONS.FAILURE,
payload: getErrorMessage(error),
status: error.response.status,
payload,
});
}
};

View File

@ -67,3 +67,11 @@ export function APIReducer(state, action) {
throw new Error();
}
}
export function getErrorMessage(error) {
let payload = "An unknown error occurred";
if (error.response.headers["content-type"] === "application/json") {
payload = error.response.data;
}
return payload;
}