From 24f1963dca8e383e04f2e1253ee8f8a534bad4c7 Mon Sep 17 00:00:00 2001 From: Tim Zallmann <tzallmann@gitlab.com> Date: Fri, 21 Jul 2023 16:50:21 +0000 Subject: [PATCH] Improves Duo Chat Error messages --- .../ai/tanuki_bot/store/actions.js | 21 +++++++------ .../ai/tanuki_bot/store/mutations.js | 4 +-- ee/spec/frontend/ai/tanuki_bot/mock_data.js | 14 +++++---- .../ai/tanuki_bot/store/actions_spec.js | 31 +++++++++++++++++-- .../ai/tanuki_bot/store/mutations_spec.js | 9 ++++-- 5 files changed, 56 insertions(+), 23 deletions(-) diff --git a/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js b/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js index 6c53f9b4b3269..9fc6bbe1f3c1d 100644 --- a/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js +++ b/ee/app/assets/javascripts/ai/tanuki_bot/store/actions.js @@ -20,30 +20,31 @@ export const receiveMutationResponse = ({ commit, dispatch }, { data, message }) export const receiveTanukiBotMessage = async ({ commit, dispatch }, data) => { const { errors = [], responseBody } = data.aiCompletionResponse || {}; + let parsedResponse; + try { + parsedResponse = JSON.parse(responseBody); + } catch { + parsedResponse = { content: responseBody }; + } + if (errors?.length) { - dispatch('tanukiBotMessageError'); + dispatch('tanukiBotMessageError', parsedResponse); } else if (responseBody) { commit(types.SET_LOADING, false); - let parsedResponse; - try { - parsedResponse = JSON.parse(responseBody); - } catch { - parsedResponse = { content: responseBody }; - } commit(types.ADD_TANUKI_MESSAGE, parsedResponse); } }; -export const tanukiBotMessageError = ({ commit }) => { +export const tanukiBotMessageError = ({ commit }, data) => { commit(types.SET_LOADING, false); - commit(types.ADD_ERROR_MESSAGE); + commit(types.ADD_ERROR_MESSAGE, data); }; export const setMessages = ({ commit, dispatch }, messages) => { messages.forEach((msg) => { if (msg.errors?.length) { - dispatch('tanukiBotMessageError'); + dispatch('tanukiBotMessageError', msg); } else { switch (msg.role.toLowerCase()) { case MESSAGE_TYPES.USER: diff --git a/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js b/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js index 0146370a17385..f3f9f1ca4be2f 100644 --- a/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js +++ b/ee/app/assets/javascripts/ai/tanuki_bot/store/mutations.js @@ -30,11 +30,11 @@ export default { }); } }, - [types.ADD_ERROR_MESSAGE](state) { + [types.ADD_ERROR_MESSAGE](state, msg) { state.messages.push({ id: state.messages.length, role: MESSAGE_TYPES.TANUKI, - content: '', + content: msg ? msg.content || msg.errors.join(' ') : '', errors: [ERROR_MESSAGE], }); }, diff --git a/ee/spec/frontend/ai/tanuki_bot/mock_data.js b/ee/spec/frontend/ai/tanuki_bot/mock_data.js index fed7ee89cf512..fab2e1cfc19fb 100644 --- a/ee/spec/frontend/ai/tanuki_bot/mock_data.js +++ b/ee/spec/frontend/ai/tanuki_bot/mock_data.js @@ -46,13 +46,15 @@ export const GENERATE_MOCK_TANUKI_RES = (body = JSON.stringify(MOCK_TANUKI_MESSA export const MOCK_TANUKI_SUCCESS_RES = GENERATE_MOCK_TANUKI_RES(); -export const MOCK_TANUKI_ERROR_RES = { - data: { - aiCompletionResponse: { - responseBody: null, - errors: ['error'], +export const MOCK_TANUKI_ERROR_RES = (body = JSON.stringify(MOCK_TANUKI_MESSAGE)) => { + return { + data: { + aiCompletionResponse: { + responseBody: body, + errors: ['error'], + }, }, - }, + }; }; export const MOCK_CHAT_CACHED_MESSAGES_RES = { diff --git a/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js b/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js index 7476492db5499..fa9d142d376c6 100644 --- a/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js +++ b/ee/spec/frontend/ai/tanuki_bot/store/actions_spec.js @@ -81,11 +81,21 @@ describe('TanukiBot Store Actions', () => { describe('with error', () => { it(`should dispatch the correct actions`, () => { + const contentObject = { + content: '', + source: 'foo-bar', + }; + const stringifiedContent = JSON.stringify(contentObject); return testAction({ action: actions.receiveTanukiBotMessage, - payload: MOCK_TANUKI_ERROR_RES.data, + payload: MOCK_TANUKI_ERROR_RES(stringifiedContent).data, state, - expectedActions: [{ type: 'tanukiBotMessageError' }], + expectedActions: [ + { + payload: contentObject, + type: 'tanukiBotMessageError', + }, + ], }); }); }); @@ -172,7 +182,22 @@ describe('TanukiBot Store Actions', () => { }, ], state, - expectedActions: [{ type: 'tanukiBotMessageError' }, { type: 'tanukiBotMessageError' }], + expectedActions: [ + { + type: 'tanukiBotMessageError', + payload: { + ...MOCK_USER_MESSAGE, + errors: ['foo'], + }, + }, + { + type: 'tanukiBotMessageError', + payload: { + ...MOCK_TANUKI_MESSAGE, + errors: ['foo'], + }, + }, + ], }); }); }); diff --git a/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js b/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js index 5dd006aadde89..57ac3061e0257 100644 --- a/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js +++ b/ee/spec/frontend/ai/tanuki_bot/store/mutations_spec.js @@ -51,10 +51,15 @@ describe('TanukiBot Store Mutations', () => { describe('ADD_ERROR_MESSAGE', () => { it('pushes an error message to the messages array with type: Tanuki', () => { - mutations[types.ADD_ERROR_MESSAGE](state); + mutations[types.ADD_ERROR_MESSAGE](state, { ...MOCK_TANUKI_MESSAGE, msg: 'test' }); expect(state.messages).toStrictEqual([ - { id: 0, role: MESSAGE_TYPES.TANUKI, errors: [ERROR_MESSAGE], content: '' }, + { + id: 0, + role: MESSAGE_TYPES.TANUKI, + errors: [ERROR_MESSAGE], + content: 'Tanuki Bot message', + }, ]); }); }); -- GitLab