Skip to content

Commit fb0f4f8

Browse files
Improve error handling and optimize character lookup (#112)
1 parent 69c2d8f commit fb0f4f8

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

app/app.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ app.use(express.static(path.join(__dirname, 'public')));
2828
app.locals.delimiters = '{{ }}';
2929

3030
function getCharacterByName(name) {
31-
for(let i=0; i< json.length; i++) {
32-
if(json[i].name == name) {
33-
return json[i];
34-
35-
}
36-
}
37-
return null;
31+
return json.find(character => character.name === name) || null;
3832
}
3933

4034
// Route to send the prompt
@@ -74,8 +68,8 @@ app.post('/send', async (req, res) => {
7468
answer: completion.choices[0]?.message?.content
7569
});
7670
} catch (error) {
77-
console.log(`Error: ${error}`);
78-
res.status(500).json({ error: error });
71+
console.error(`Error: ${error.message}`); // Log the error message for debugging
72+
res.status(500).json({ message: 'An unexpected error occurred. Please try again later.' }); // Send a generic error message
7973
}
8074
});
8175

0 commit comments

Comments
 (0)