From 09d308392e02b085695222a9203ea80c3a4a54f6 Mon Sep 17 00:00:00 2001 From: David Straub Date: Sat, 24 Aug 2024 10:32:27 +0200 Subject: [PATCH] Fix error message display --- src/api.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/api.js b/src/api.js index 0bd532e0..cea254bc 100644 --- a/src/api.js +++ b/src/api.js @@ -258,6 +258,12 @@ export async function apiGet(endpoint, isJson = true) { method: 'GET', headers, }) + let resJson + try { + resJson = await resp.json() + } catch (error) { + resJson = {} + } if (resp.status === 401) { const refreshToken = localStorage.getItem('refresh_token') if (refreshToken === null) { @@ -274,11 +280,13 @@ export async function apiGet(endpoint, isJson = true) { throw new Error('Authorization error') } if (resp.status !== 200) { - throw new Error(resp.statusText || `Error ${resp.status}`) + throw new Error( + resJson?.error?.message || resp.statusText || `Error ${resp.status}` + ) } if (isJson) { return { - data: await resp.json(), + data: resJson, total_count: resp.headers.get('X-Total-Count'), etag: resp.headers.get('ETag'), }