From f08f6ca0a9d337efff280d4d1669a41b5d9c31c2 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Thu, 2 Sep 2021 22:28:11 +0200 Subject: finish translations, force maximize --- helpers/fetchJson.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 helpers/fetchJson.js (limited to 'helpers/fetchJson.js') diff --git a/helpers/fetchJson.js b/helpers/fetchJson.js new file mode 100644 index 0000000..5db80b5 --- /dev/null +++ b/helpers/fetchJson.js @@ -0,0 +1,23 @@ +export default async function fetcher(...args) { + try { + const response = await fetch(...args) + + // if the server replies, there's always some data in json + // if there's a network error, it will throw at the previous line + const data = await response.json() + + if (response.ok) { + return data + } + + const error = new Error(response.statusText) + error.response = response + error.data = data + throw error + } catch (error) { + if (!error.data) { + error.data = {message: error.message} + } + throw error + } +} -- cgit v1.2.3