From 2d67b61beb1024d8372e698dc21ad5451728f329 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Mon, 9 Oct 2023 10:15:23 -0500 Subject: [PATCH] fix: Added response context to error message (#114) * fix: Added response context to message * fix: Added response error context test --- src/terrasoApi/api.test.ts | 4 ++++ src/terrasoApi/api.ts | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/terrasoApi/api.test.ts b/src/terrasoApi/api.test.ts index 8d14653b..404bf84c 100644 --- a/src/terrasoApi/api.test.ts +++ b/src/terrasoApi/api.test.ts @@ -86,6 +86,7 @@ test('Terraso API: mutation errors', async () => { ]), }, ], + extraData: 'test', }, }, }), @@ -115,6 +116,9 @@ test('Terraso API: mutation errors', async () => { body: { variables: {}, }, + response: { + extraData: 'test', + }, code: 'unique', context: '', field: 'name', diff --git a/src/terrasoApi/api.ts b/src/terrasoApi/api.ts index d79914a9..43a6c65e 100644 --- a/src/terrasoApi/api.ts +++ b/src/terrasoApi/api.ts @@ -24,7 +24,7 @@ import logger from 'terraso-client-shared/monitoring/logger'; type Error = { message: any }; type Errors = { errors?: Error[] | null }; -const parseMessage = (message: any, body: any) => { +const parseMessage = (message: any, body: any, response: any) => { try { const messages = (function () { // If message can be parsed as JSON, return parsed messages @@ -54,6 +54,7 @@ const parseMessage = (message: any, body: any) => { ..._.omit('extra', message.context), context: _.get('context.extra', message), body: _.omit('query', body), + response: _.omit('errors', response), }, }; }); @@ -85,7 +86,7 @@ const handleApiErrors = async ( body instanceof FormData ? JSON.parse(JSON.stringify(body)) : body; const messages = _.flatMap( - error => parseMessage(error.message, parsedBody), + error => parseMessage(error.message, parsedBody, response), response.errors, ); return Promise.reject(messages);