From 96eed02d32ae488bb258aee98d96fd7e1a7a19f2 Mon Sep 17 00:00:00 2001 From: Maciej Lenartowicz Date: Tue, 1 Oct 2019 09:38:18 +0000 Subject: [PATCH] Filter non-JSON payload --- Makefile | 2 +- package-lock.json | 2 +- package.json | 2 +- src/api/delete.js | 4 ++-- src/api/patch.js | 4 ++-- src/api/post.js | 8 ++------ src/api/utils.js | 8 ++++++++ 7 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index d85a714..08ed1ff 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ install-js: package.json npm install --save-dev watch-js: - npm run watch + npm run build:watch build-js: npm run build diff --git a/package-lock.json b/package-lock.json index cbf57b5..dad8c2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "0.1.0-beta.6", + "version": "0.1.0-beta.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 008b82f..0646b86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "foris", - "version": "0.1.0-beta.6", + "version": "0.1.0-beta.7", "description": "Set of components and utils for Foris and its plugins.", "author": "CZ.NIC, z.s.p.o.", "repository": { diff --git a/src/api/delete.js b/src/api/delete.js index c04114d..dc4574f 100644 --- a/src/api/delete.js +++ b/src/api/delete.js @@ -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, }); } diff --git a/src/api/patch.js b/src/api/patch.js index 19c7fa4..ba3ed9e 100644 --- a/src/api/patch.js +++ b/src/api/patch.js @@ -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, }); } diff --git a/src/api/post.js b/src/api/post.js index dc97796..705356e 100644 --- a/src/api/post.js +++ b/src/api/post.js @@ -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, }); } }; diff --git a/src/api/utils.js b/src/api/utils.js index 20bc045..fd85c8c 100644 --- a/src/api/utils.js +++ b/src/api/utils.js @@ -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; +}