From d5e5e0d2283b30e04480fc4abd35ca541fe223ba Mon Sep 17 00:00:00 2001 From: codewithdhruba01 Date: Sat, 26 Apr 2025 22:55:40 +0530 Subject: [PATCH] Improve error handling and optimize character lookup --- app/app.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/app/app.js b/app/app.js index bf1dc3a6..3925bbd4 100644 --- a/app/app.js +++ b/app/app.js @@ -28,13 +28,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.locals.delimiters = '{{ }}'; function getCharacterByName(name) { - for(let i=0; i< json.length; i++) { - if(json[i].name == name) { - return json[i]; - - } - } - return null; + return json.find(character => character.name === name) || null; } // Route to send the prompt @@ -74,8 +68,8 @@ app.post('/send', async (req, res) => { answer: completion.choices[0]?.message?.content }); } catch (error) { - console.log(`Error: ${error}`); - res.status(500).json({ error: error }); + console.error(`Error: ${error.message}`); // Log the error message for debugging + res.status(500).json({ message: 'An unexpected error occurred. Please try again later.' }); // Send a generic error message } });