Closed FreightCompanionDavid closed 7 months ago
c183728816
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
openai-api.js
✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/8afa6267776abbe6104790fc97fa035030f06917 Edit
Modify openai-api.js with contents:
• Import the `ApplicationError` class from `middleware/customErrors.js` at the top of the `openai-api.js` file. Add the line: `const { ApplicationError } = require('./middleware/customErrors');`
• Import the `logger` from `logger.js` at the top of the `openai-api.js` file. Add the line: `const logger = require('./logger');`
• Within the `apiCall` method in `openai-api.js`, locate the error handling block inside the catch statement where it checks if `attempt === retries - 1` and throws a new `Error`.
• Replace the existing throw statement with the enhanced error handling logic as described in the issue. Specifically: - Log the detailed error information using `logger.error`. The log message should be 'API call failed:', and the log details should include `path`, `data`, and the error message. Use the line: `logger.error('API call failed:', { path, data, error: error.response?.data?.error || error.message });` - Throw a new `ApplicationError` instead of a generic `Error`. The error message should be 'API call failed. Check logs for details.', and the status code should be 500. Use the line: `throw new ApplicationError('API call failed. Check logs for details.', 500, false);`
• These changes will ensure that when the maximum number of retries is reached, and the API call fails, detailed error information is logged, and a custom `ApplicationError` is thrown with a more informative message.
--- +++ @@ -1,4 +1,6 @@ const axios = require('axios'); +const { ApplicationError } = require('./middleware/customErrors'); +const logger = require('./logger'); const { InternalServerError } = require('./middleware/customErrors'); const apiKey = process.env.OPENAI_API_KEY; @@ -22,7 +24,10 @@ return response.data; } catch (error) { console.error(`Error calling API: ${error.response?.data?.error || error.message}`, { path, data, attempt }); - if (attempt === retries - 1) throw new InternalServerError(`Failed API call to ${path}: ${error.response?.data?.error || error.message}`, 500, false); + if (attempt === retries - 1) { + logger.error('API call failed:', { path, data, error: error.response?.data?.error || error.message }); + throw new ApplicationError('API call failed. Check logs for details.', 500, false); + } attempt++; await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, attempt))); }
openai-api.js
✓ Edit
Check openai-api.js with contents:
Ran GitHub Actions for 8afa6267776abbe6104790fc97fa035030f06917:
I have finished reviewing the code for completeness. I did not find errors for sweep/_specific_code_change_examples_for_smart
.
💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Details
Here are some specific code change examples based on the proposed enhancements and reusability strategies:
1. API Call Strategies (openai-api.js):
Current: async apiCall(path, data, retries = 3) { // ... (existing code) if (attempt === retries - 1) throw new Error("A more descriptive error based on the context."); // ... (existing code) } Use code with caution. `
Enhancement: async apiCall(path, data, retries = 3) { // ... (existing code) if (attempt === retries - 1) { const errorDetails = { path, data, error: error.response?.data?.error || error.message }; logger.error('API call failed:', errorDetails); // Log detailed error information throw new ApplicationError('API call failed. Check logs for details.', 500); // Throw a custom error with details } // ... (existing code) } Use code with caution. `
This example demonstrates improved error handling by logging detailed error information and throwing a custom
ApplicationError
with a more informative message.Branch
No response
Checklist
- [X] Modify `openai-api.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/8afa6267776abbe6104790fc97fa035030f06917 [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/_specific_code_change_examples_for_smart/openai-api.js) - [X] Running GitHub Actions for `openai-api.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/_specific_code_change_examples_for_smart/openai-api.js)