RayyR
Convex Community2y ago
12 replies
Rayy

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;
  },
});


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,
        }
      );
    },
  });


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.
Was this page helpful?