I think i am handling the case if an exception is thrown just fine but still I keep getting an error Error: No response is returned from route handler ‘C:UsersayanOneDriveDesktopappswebappapifollowupsroute.ts’. Ensure you return a Response
or a NextResponse
in all branches of your handler.
I expected a response from this api after successful execution or just an exception rather than no response is returned error
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { NextApiResponse } from "next";
export async function GET(res: NextApiResponse){
try {
const cohere_api_key = "";
const followups = [
{
ques : "What other creative ways can ChatGPT be integrated into everyday objects?"
},
{
ques: "How can AI be used to enhance education and learning experiences beyond exam assistance?"
},
{
ques: "What are the potential ethical considerations of using AI-powered tools like this eraser during exams?"
}
]
console.log("###########Cohere is called");
const promises = followups.map((item)=>(
fetch('https://api.cohere.ai/v1/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${cohere_api_key}`, // Replace YOUR_API_KEY with your actual API key
},
body: JSON.stringify({
message: `You are a smart blogger who write detailed blogs on a given topic, write a blog on topic: "${item.ques}"`,
connectors: [{ id: 'web-search' }],
}),
})
)
)
console.log("###########resolving promises")
Promise.all(promises)
.then((response)=>{
console.log("all promises resolved", response);
return Promise.all(response.map((res)=> res.json()));
})
.then((res)=>{
console.log("/////response", res);
const result = followups.map((followup , index) => ({
question : followup.ques,
content : res[index].text
}))
console.log("our final result set" ,result)
return Response.json(result);
})
.catch((error) => {
console.log("error",error)
return res.status(500).json({ message: 'Internal server error' })
})
} catch (error) {
return res.status(500).json({ message: 'Internal server error' })
}
}