Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosResponse } from 'axios';
import axios, { AxiosError, AxiosResponse } from 'axios';
import { prepareFormData } from '@/api/utils';
import { APIResponse } from '../types/api';
import { useErrorTracker } from '@/hawk';
Expand Down Expand Up @@ -142,19 +142,12 @@ export async function callOld(
response = (await Promise.all([blockingRequest, promise]))[1];
}

if (!response || !response.data) {
return response;
}
Comment on lines +145 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statement seems unclear for me


if (response.data.errors) {
response.data.errors.forEach(error => {
/**
* Send error to Hawk
*/
track(new Error(error.message), {
'Request': request,
'Error Path': error.path,
'Variables': variables ?? {},
'Response Data': response.data.data,
});

printApiError(error, response.data, request, variables);
});
}
Expand All @@ -178,11 +171,6 @@ export async function callOld(
} catch (error) {
console.error('API Request Error', error);

track(error as Error, {
'Request': request,
'Variables': variables ?? {},
'Response Data': response?.data.data,
});
throw error;
}
}
Expand Down Expand Up @@ -284,6 +272,7 @@ interface ApiModuleHandlers {
* @param {ApiModuleHandlers} eventsHandlers - object with handlers
*/
export function setupApiModuleHandlers(eventsHandlers: ApiModuleHandlers): void {

/**
* Interceptors that handles the error of expired tokens
*/
Expand All @@ -296,6 +285,9 @@ export function setupApiModuleHandlers(eventsHandlers: ApiModuleHandlers): void
*/
async (response: AxiosResponse): Promise<AxiosResponse> => {
const errors = response.data.errors;

trackErrors(response);

const isTokenExpiredError = errors && errors[0].extensions.code === errorCodes.ACCESS_TOKEN_EXPIRED_ERROR;

if (!errors || !isTokenExpiredError) {
Expand Down Expand Up @@ -332,6 +324,35 @@ export function setupApiModuleHandlers(eventsHandlers: ApiModuleHandlers): void

return response;
}
},

(errorResponse: AxiosError) => {

if (errorResponse.response) {
trackErrors(errorResponse.response);
}

return errorResponse;
}
);
}


/**
* Sends errors to Hawk
*
* @param response - Axios response object
*/
function trackErrors(response: AxiosResponse) {
const errors = response.data?.errors || [];

errors.forEach(error => {
track(error.message, {
path: error.path,
payload: response.config.data,
locations: error.locations,
stacktrace: error.extensions.exception.stacktrace,
code: error.extensions.code,
});
});
}
Loading