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.
This commit is contained in:
MHSanaei
2026-07-15 05:10:00 +02:00
parent 46cac582ab
commit ab418a47be
2 changed files with 8 additions and 6 deletions
+2 -2
View File
@@ -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 });
+6 -4
View File
@@ -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<T>(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<T>(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<T>(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<T>(false, data?.msg || data?.message || err.message || 'Request failed');
if (!silent) this._handleMsg(errorMsg);
return errorMsg;
}