Rayy
Rayy•10mo ago

How to handle errors in scheduled functions and display that error to the frontend?

Suppose I have a mutation which is called from the client which inserts data to a table.
export const createChatbook = mutation({
args: {
//Pass arguments
},
handler: async (ctx, args) => {
const chatId = await ctx.db.insert("chatbook", {
//Insert data
});
await ctx.scheduler.runAfter(
0,
internal.chatbook.chunks.youtube.createChunks,
{
chatId,
url,
}
);

return chatId;
},
});
export const createChatbook = mutation({
args: {
//Pass arguments
},
handler: async (ctx, args) => {
const chatId = await ctx.db.insert("chatbook", {
//Insert data
});
await ctx.scheduler.runAfter(
0,
internal.chatbook.chunks.youtube.createChunks,
{
chatId,
url,
}
);

return chatId;
},
});
It then calls a scheduled function which creates chunks from the youtube url.
export const createChunks = internalAction({
args: {
//Pass args
},
handler: async (ctx, args) => {
const chunks = await extractAudioAndSplitChunks(args.url);

//Add chunks to a table
await ctx.runMutation(internal.helper.chunks.addChunks, {
chatId: args.chatId,
chunks: chunks.chunkArr,
});

await ctx.scheduler.runAfter(
0,
internal.chatbook.embedding.generateEmbeddings,
{
chatId: args.chatId,
title: chunks.title,
}
);
},
});
export const createChunks = internalAction({
args: {
//Pass args
},
handler: async (ctx, args) => {
const chunks = await extractAudioAndSplitChunks(args.url);

//Add chunks to a table
await ctx.runMutation(internal.helper.chunks.addChunks, {
chatId: args.chatId,
chunks: chunks.chunkArr,
});

await ctx.scheduler.runAfter(
0,
internal.chatbook.embedding.generateEmbeddings,
{
chatId: args.chatId,
title: chunks.title,
}
);
},
});
After creating the chunks, another scheduled function is called which generates the embeddings from the chunks and then adds it to the vector database and updates the "chatbook" table saying that the embeddings has been generated. Till the embeddings is being generating, a loading spinner is displayed in the frontend. And as soon as the table updates that the embedding has been generated, the chatbot is shown. So, the problem I am facing is if there is an error in the createChunks action even then the loading spinner is being displayed in the frontend. The error is being caught but I cannot figure out how can I show that error to the frontend. What would be the efficient way to handle and display errors in such cases? If I am unable to explain my situation, then please feel free to further ask any questions regarding the same.
8 Replies
erquhart
erquhart•10mo ago
You would need to store any error information that you want to display - similar to how you're handling the actual data you're displaying. Since errors are ephemeral, I'd expect you maybe have a cron job to delete error messages after a given time has passed. If each chatbook is specific to a given request, you could maybe store errors on the chatbook document, and mark them viewed or delete them once presented to the user, assuming you don't want them to be more long-lived than that. If an error should be followed up by a retry, maybe you store the error as a status, and display it along with controls to retry. Not sure how much of this applies, inferring some things about your application here.
lee
lee•10mo ago
this may not fit your use-case, but note the _scheduled_functions system table has a state: Failed which holds error messages for a short time
lee
lee•10mo ago
Rayy
RayyOP•10mo ago
Was considering a similar solution, where I would patch the error message to the "chatbook" table. Since each chatbook corresponds to a specific request, I could catch the error within the action and then update the database with the error message, which would be displayed on the frontend. While this approach seems feasible, I am curious if there are any alternative methods that might be more effective, or if this approach is good enough?
erquhart
erquhart•10mo ago
Check Lee's response first, that may be much better and easier for you
Rayy
RayyOP•10mo ago
About that, I'd need to get the id of the scheduled function and then query the status of the scheduled function to get the error message, right?
erquhart
erquhart•10mo ago
Exactly The schedule call returns the id, and that you would probably want to store, probably on the chatbook But once you have it you can get the state of the request as needed, build UI around it, etc. As long as you don't need it to be there longer than 7 days.
Rayy
RayyOP•10mo ago
This will work just fine, I guess. Will try it. Thanks guys. 🙂

Did you find this page helpful?