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.
It then calls a scheduled function which creates chunks from the youtube url.
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
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.
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 timeScheduled Functions | Convex Developer Hub
Convex allows you to schedule functions to run in the future. This allows you to
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?
Check Lee's response first, that may be much better and easier for you
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?
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.
This will work just fine, I guess. Will try it.
Thanks guys. 🙂