Skip to content

Commit

Permalink
fix: Added response context to error message (#114)
Browse files Browse the repository at this point in the history
* fix: Added response context to message

* fix: Added response error context test
  • Loading branch information
josebui authored Oct 9, 2023
1 parent b675b25 commit 2d67b61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/terrasoApi/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ test('Terraso API: mutation errors', async () => {
]),
},
],
extraData: 'test',
},
},
}),
Expand Down Expand Up @@ -115,6 +116,9 @@ test('Terraso API: mutation errors', async () => {
body: {
variables: {},
},
response: {
extraData: 'test',
},
code: 'unique',
context: '',
field: 'name',
Expand Down
5 changes: 3 additions & 2 deletions src/terrasoApi/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
},
};
});
Expand Down Expand Up @@ -85,7 +86,7 @@ const handleApiErrors = async <T extends Errors>(
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);
Expand Down

0 comments on commit 2d67b61

Please sign in to comment.