I am currently working on a SvelteKit project and have encountered an issue related to generating 404 errors. I would appreciate any guidance or support to resolve this.
When a valid URL is entered, the response is as expected. However, when an invalid URL is entered, I encounter an unexpected error (error code 500)NotFoundError: No documents were returned.
Any help to address this issue would be greatly appreciated.
Thanks for posting this question! The problem is that client.getByUID() throws an error, which stops execution of the load() function, and SvelteKit understands it as a server error.
You can solve this by handling the error from getByUID(), like so:
Thank you for your swift response. I regret to inform you that your solution did not yield the desired results. You can find the code at this GitHub link.
Hi Sam,
Thank you so much for taking time to help me out. I tried to add everything to a single try-and-catch block. It still gives a server error instead of 404.
I think the problem is now that you have nested your queries inside Promise.all(). I might be wrong, but I suspect that if the API queries fails the error won't get caught. Try making sure that all of your queries are immediate children of your try/catch block.
I hope this message finds you well. I wanted to inform you that I have already attempted the initial solution you suggested, but unfortunately, it resulted in an internal server error. Despite this, I have gone ahead and incorporated the modifications you outlined.
When you have a moment, could you kindly review the changes? You can find the updated file at the following location: Updated File
Piyush
PS: If it’s not too much trouble, could you share an example of a 404 page you’ve worked on? I believe it would be quite beneficial for me.
I tried running your code, and it seems to be working fine for me. I got an error arising from your cloudinary.js file (because I didn't have the .env config). But otherwise the website seems to work:
I thank you so much for taking the time to look into my troubles. I must admit, I feel a bit remorseful for imposing on you so frequently.
To illustrate the issue at hand, if one attempts to access a non-existent URL, such as ao.vercel.app/123 or http://localhost:5173/123, the ideal response would be a 404 error. However, it currently results in a 500 internal server error.
I would also like to mention that all valid URLs are functioning as expected without any issues.
Thanks for your patience, and please don't feel remorseful! Are you still having trouble with this? If so, I can spend some time debugging it on Monday.
I forgot to inform you earlier, but I’ve managed to solve the issue. I realized that I was still operating on Sveltekit’s first version, where the syntax for utilizing the error helper class has been modified. I was attempting to use it without the “throw” keyword, a beginner’s error on my part. I’m grateful for your assistance, and I’m pleased to report that everything is functioning as anticipated.