FreightCompanionDavid / SmartAPIHub

to provide logical controls for API calls that are self-improving in nature for any application.
0 stars 0 forks source link

Sweep: **Error Handling and Robustness:** #18

Closed FreightCompanionDavid closed 6 months ago

FreightCompanionDavid commented 6 months ago

Details

Checklist - [X] Create `middleware/customErrors.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/69b6fe6d6915e4ad4911aa348a3b6bae3682e08e [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/middleware/customErrors.js) - [X] Running GitHub Actions for `middleware/customErrors.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/middleware/customErrors.js) - [X] Modify `middleware/errorHandler.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/69d80dc29b9a38440620868f4653efe4ff913e86 [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/middleware/errorHandler.js) - [X] Running GitHub Actions for `middleware/errorHandler.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/middleware/errorHandler.js) - [X] Modify `app.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/0fa8a67575b8c7700b429704b8bfd1731514986e [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/app.js) - [X] Running GitHub Actions for `app.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/app.js) - [X] Modify `logger.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/6a2fc266cdbfb809e8c462b0f53d48bc7145f32a [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/logger.js) - [X] Running GitHub Actions for `logger.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/logger.js) - [X] Modify `openai-api.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/878b93eee2672a2601c56a8c1a5d9c13d8fef023 [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/openai-api.js) - [X] Running GitHub Actions for `openai-api.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/openai-api.js) - [X] Create `handleImageGenerationRequest.js handleImageUnderstandingRequest.js handleDiscussionsRequest.js` ✓ https://github.com/FreightCompanionDavid/SmartAPIHub/commit/e15d1a0bf7dc3d4d71e6431060b47e69557732a7 [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/handleImageGenerationRequest.js handleImageUnderstandingRequest.js handleDiscussionsRequest.js) - [X] Running GitHub Actions for `handleImageGenerationRequest.js handleImageUnderstandingRequest.js handleDiscussionsRequest.js` ✓ [Edit](https://github.com/FreightCompanionDavid/SmartAPIHub/edit/sweep/error_handling_and_robustness/handleImageGenerationRequest.js handleImageUnderstandingRequest.js handleDiscussionsRequest.js)
sweep-ai[bot] commented 6 months ago

🚀 Here's the PR! #25

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: c15865ecf3)

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/middleware/errorHandler.js#L1-L18 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/logger.js#L1-L24 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/app.js#L1-L108 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/openai-api.js#L1-L42 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/handleImageGenerationRequest.js#L1-L21 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/handleImageUnderstandingRequest.js#L1-L32 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/handleDiscussionsRequest.js#L1-L28 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/handleImageUnderstandingRequest.js#L1-L32 https://github.com/FreightCompanionDavid/SmartAPIHub/blob/1bc4a617da9e5acbb8817e6fd7bcb5bcce95b1b3/handleDiscussionsRequest.js#L1-L28

Step 2: ⌨️ Coding

Ran GitHub Actions for 69b6fe6d6915e4ad4911aa348a3b6bae3682e08e:

--- 
+++ 
@@ -1,14 +1,17 @@
 const logger = require('../logger');
+const { ApplicationError, ValidationError, NotFoundError, InternalServerError } = require('./customErrors');

 function errorHandler(err, req, res, next) {
-    logger.error(err);
+    logger.error(`${err.name}: ${err.message} (Status code: ${err.statusCode})`);

-    if (err.isOperational) {
-        res.status(err.statusCode || 400).json({
+    if (err instanceof ApplicationError) {
+        res.status(err.statusCode).json({
             success: false,
             message: err.message,
         });
     } else {
+        // Log detailed error information for non-operational errors
+        logger.error(`Non-operational error: ${err.stack}`);
         res.status(500).json({
             success: false,
             message: 'An unexpected error occurred. Please try again later.',

Ran GitHub Actions for 69d80dc29b9a38440620868f4653efe4ff913e86:

--- 
+++ 
@@ -7,6 +7,7 @@
 const handleEmbeddingRequest = require('./handleEmbeddingRequest');
 const logger = require('./logger'); // Assuming logger.js setup is done
 const { createDiscussion, getDiscussions } = require('./handleDiscussionsRequest');
+const errorHandler = require('./middleware/errorHandler');

 const app = express();
 const port = process.env.PORT || 3000;
@@ -67,10 +68,7 @@
 });

 // Centralized error handler middleware
-app.use((err, req, res, next) => {
-    logger.error(err.message);
-    res.status(500).json({ error: "An unexpected error occurred." });
-});
+app.use(errorHandler);

 app.get('/health', (req, res) => res.send('OK'));

Ran GitHub Actions for 0fa8a67575b8c7700b429704b8bfd1731514986e:

--- 
+++ 
@@ -2,7 +2,10 @@

 const logger = winston.createLogger({
     level: 'info',
-    format: winston.format.json(),
+    format: winston.format.combine(
+        winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
+        winston.format.json()
+    ),
     defaultMeta: { service: 'user-service' },
     transports: [
         //
@@ -18,7 +21,10 @@
 // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
 if (process.env.NODE_ENV !== 'production') {
     logger.add(new winston.transports.Console({
-        format: winston.format.simple(),
+        format: winston.format.combine(
+            winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
+            winston.format.simple()
+        ),
     }));
 }

Ran GitHub Actions for 6a2fc266cdbfb809e8c462b0f53d48bc7145f32a:

--- 
+++ 
@@ -1,4 +1,5 @@
 const axios = require('axios');
+const { InternalServerError } = require('./middleware/customErrors');
 const apiKey = process.env.OPENAI_API_KEY;

 const openai = {
@@ -20,7 +21,7 @@
                 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 Error("A more descriptive error based on the context.");
+                if (attempt === retries - 1) throw new InternalServerError(`Failed API call to ${path}: ${error.response?.data?.error || error.message}`, 500, false);
                 attempt++;
                 await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, attempt)));
             }

Ran GitHub Actions for 878b93eee2672a2601c56a8c1a5d9c13d8fef023:

Ran GitHub Actions for e15d1a0bf7dc3d4d71e6431060b47e69557732a7:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/error_handling_and_robustness.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 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.