mirror of
				https://gitlab.nic.cz/turris/reforis/foris-js.git
				synced 2025-10-30 22:20:31 +01:00 
			
		
		
		
	Improve error handling + small refactoring.
This commit is contained in:
		| @@ -5,11 +5,11 @@ | |||||||
|  * See /LICENSE for more information. |  * See /LICENSE for more information. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| import { useReducer, useCallback } from "react"; | import { useCallback, useReducer } from "react"; | ||||||
|  |  | ||||||
| import { ForisURLs } from "forisUrls"; | import { ForisURLs } from "forisUrls"; | ||||||
| import { | import { | ||||||
|     API_STATE, API_ACTIONS, API_METHODS, TIMEOUT, HEADERS, getErrorMessage, |     API_ACTIONS, API_METHODS, API_STATE, getErrorPayload, HEADERS, TIMEOUT, | ||||||
| } from "./utils"; | } from "./utils"; | ||||||
|  |  | ||||||
| const DATA_METHODS = ["POST", "PATCH", "PUT"]; | const DATA_METHODS = ["POST", "PATCH", "PUT"]; | ||||||
| @@ -30,19 +30,25 @@ function createAPIHook(method) { | |||||||
|             dispatch({ type: API_ACTIONS.INIT }); |             dispatch({ type: API_ACTIONS.INIT }); | ||||||
|             try { |             try { | ||||||
|                 const request = API_METHODS[method]; |                 const request = API_METHODS[method]; | ||||||
|                 const config = { timeout: TIMEOUT, headers }; |                 const config = { | ||||||
|  |                     timeout: TIMEOUT, | ||||||
|  |                     headers, | ||||||
|  |                 }; | ||||||
|                 let result; |                 let result; | ||||||
|                 if (DATA_METHODS.includes(method)) { |                 if (DATA_METHODS.includes(method)) { | ||||||
|                     result = await request(url, data, config); |                     result = await request(url, data, config); | ||||||
|                 } else { |                 } else { | ||||||
|                     result = await request(url, config); |                     result = await request(url, config); | ||||||
|                 } |                 } | ||||||
|                 dispatch({ type: API_ACTIONS.SUCCESS, payload: result.data }); |                 dispatch({ | ||||||
|  |                     type: API_ACTIONS.SUCCESS, | ||||||
|  |                     payload: result.data, | ||||||
|  |                 }); | ||||||
|             } catch (error) { |             } catch (error) { | ||||||
|                 dispatch({ |                 dispatch({ | ||||||
|                     type: API_ACTIONS.FAILURE, |                     type: API_ACTIONS.FAILURE, | ||||||
|                     payload: getErrorMessage(error), |  | ||||||
|                     status: error.response && error.response.status, |                     status: error.response && error.response.status, | ||||||
|  |                     payload: getErrorPayload(error), | ||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
|         }, [url, contentType]); |         }, [url, contentType]); | ||||||
|   | |||||||
| @@ -52,13 +52,27 @@ function getCookie(name) { | |||||||
|     return cookieValue; |     return cookieValue; | ||||||
| } | } | ||||||
|  |  | ||||||
| export function getErrorMessage(error) { | export function getErrorPayload(error) { | ||||||
|     let payload = _("An unknown error occurred."); |     if (error.response) { | ||||||
|     if (error.response && error.response.headers["content-type"] === "application/json") { |         if (error.response.status === 403) { | ||||||
|         payload = error.response.data; |             return _("The session is expired. Please log in again."); | ||||||
|  |         } | ||||||
|  |         return getJSONErrorMessage(error); | ||||||
|     } |     } | ||||||
|     if (error.code === "ECONNABORTED") { |     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."); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user