Skip to content

Commit

Permalink
Add retry in the frontend to ignore event loop closed error due to se…
Browse files Browse the repository at this point in the history
…rverless platform issue
  • Loading branch information
lazyhope committed Dec 17, 2024
1 parent 36e41dd commit 522248a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions frontend/src/utils/apiClient.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ const apiClient = axios.create({
baseURL: API_ENDPOINT,
});

export const defineSchema = async ({ messages, model, temperature, max_tokens, max_attempts, apiKey }) => {
const handleRequest = async (requestFn) => {
try {
return await requestFn();
} catch (error) {
if (error.response?.status === 500 &&
error.response?.data?.detail?.includes('Event loop is closed')) {
try {
return await requestFn();
} catch (retryError) {
throw retryError.response ? retryError.response.data : retryError.message;
}
}
throw error.response ? error.response.data : error.message;
}
};

export const defineSchema = async ({ messages, model, temperature, max_tokens, max_attempts, apiKey }) => {
return handleRequest(async () => {
const response = await apiClient.post('/define', {
messages,
model,
Expand All @@ -19,13 +35,11 @@ export const defineSchema = async ({ messages, model, temperature, max_tokens, m
},
});
return response.data;
} catch (error) {
throw error.response ? error.response.data : error.message;
}
});
};

export const parseData = async ({ messages, schema, model, temperature, max_tokens, max_attempts, apiKey }) => {
try {
return handleRequest(async () => {
const response = await apiClient.post('/parse', {
messages,
schema,
Expand All @@ -39,7 +53,5 @@ export const parseData = async ({ messages, schema, model, temperature, max_toke
},
});
return response.data;
} catch (error) {
throw error.response ? error.response.data : error.message;
}
});
};

0 comments on commit 522248a

Please sign in to comment.