LibreChat/api/app/clients/tools/util/handleOpenAIErrors.js
Danny Avila b35a8b78e2
🔧 refactor: Improve Agent Context & Minor Fixes (#5349)
* refactor: Improve Context for Agents

* 🔧 fix: Safeguard against undefined properties in OpenAIClient response handling

* refactor: log error before re-throwing for original stack trace

* refactor: remove toolResource state from useFileHandling, allow svg files

* refactor: prevent verbose logs from axios errors when using actions

* refactor: add silent method recordTokenUsage in AgentClient

* refactor: streamline token count assignment in BaseClient

* refactor: enhance safety settings handling for Gemini 2.0 model

* fix: capabilities structure in MCPConnection

* refactor: simplify civic integrity threshold handling in GoogleClient and llm

* refactor: update token count retrieval method in BaseClient tests

* ci: fix test for svg
2025-01-17 12:55:48 -05:00

33 lines
1.4 KiB
JavaScript

const OpenAI = require('openai');
const { logger } = require('~/config');
/**
* Handles errors that may occur when making requests to OpenAI's API.
* It checks the instance of the error and prints a specific warning message
* to the console depending on the type of error encountered.
* It then calls an optional error callback function with the error object.
*
* @param {Error} err - The error object thrown by OpenAI API.
* @param {Function} errorCallback - A callback function that is called with the error object.
* @param {string} [context='stream'] - A string providing context where the error occurred, defaults to 'stream'.
*/
async function handleOpenAIErrors(err, errorCallback, context = 'stream') {
if (err instanceof OpenAI.APIError && err?.message?.includes('abort')) {
logger.warn(`[OpenAIClient.chatCompletion][${context}] Aborted Message`);
}
if (err instanceof OpenAI.OpenAIError && err?.message?.includes('missing finish_reason')) {
logger.warn(`[OpenAIClient.chatCompletion][${context}] Missing finish_reason`);
} else if (err instanceof OpenAI.APIError) {
logger.warn(`[OpenAIClient.chatCompletion][${context}] API error`);
} else {
logger.warn(`[OpenAIClient.chatCompletion][${context}] Unhandled error type`);
}
logger.error(err);
if (errorCallback) {
errorCallback(err);
}
}
module.exports = handleOpenAIErrors;