From ab418a47be939ec10c20dc70a4600ece3efee77d Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Wed, 15 Jul 2026 05:10:00 +0200 Subject: [PATCH] fix(frontend): surface backend error text from failed requests HttpUtil.get/post read the thrown HttpError body as response.data.message, but the backend error envelope (entity.Msg) serializes its text as msg. On any non-2xx JSON response the real reason was therefore dropped and the operator saw only the generic "Request failed with status N" toast. Read response.data.msg first (keeping message and the native error text as fallbacks). The sibling test had pinned the wrong body shape ({ message }); correct it to the real backend shape ({ success:false, msg }) so it exercises the actual envelope. --- frontend/src/test/httpUtil.test.ts | 4 ++-- frontend/src/utils/index.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/src/test/httpUtil.test.ts b/frontend/src/test/httpUtil.test.ts index d5e7b1643..87ceace7e 100644 --- a/frontend/src/test/httpUtil.test.ts +++ b/frontend/src/test/httpUtil.test.ts @@ -73,8 +73,8 @@ describe('HttpUtil', () => { expect(toast.error).not.toHaveBeenCalled(); }); - it('maps a thrown HttpError to a failure Msg via response.data.message', async () => { - mockRequest.mockRejectedValue(new HttpError(400, 'Bad Request', { message: 'bad input' })); + it('surfaces the backend error text from a thrown HttpError body (msg field)', async () => { + mockRequest.mockRejectedValue(new HttpError(400, 'Bad Request', { success: false, msg: 'bad input' })); const msg = await HttpUtil.post('/x', undefined, { silent: true }); diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 845c3e109..566c169f9 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -76,8 +76,9 @@ export class HttpUtil { return msg; } catch (error) { console.error('GET request failed:', error); - const err = error as { response?: { data?: { message?: string } }; message?: string }; - const errorMsg = new Msg(false, err.response?.data?.message || err.message || 'Request failed'); + const err = error as { response?: { data?: { msg?: string; message?: string } }; message?: string }; + const data = err.response?.data; + const errorMsg = new Msg(false, data?.msg || data?.message || err.message || 'Request failed'); if (!silent) this._handleMsg(errorMsg); return errorMsg; } @@ -92,8 +93,9 @@ export class HttpUtil { return msg; } catch (error) { console.error('POST request failed:', error); - const err = error as { response?: { data?: { message?: string } }; message?: string }; - const errorMsg = new Msg(false, err.response?.data?.message || err.message || 'Request failed'); + const err = error as { response?: { data?: { msg?: string; message?: string } }; message?: string }; + const data = err.response?.data; + const errorMsg = new Msg(false, data?.msg || data?.message || err.message || 'Request failed'); if (!silent) this._handleMsg(errorMsg); return errorMsg; }