SvelteKit 500 error "NotFoundError: No documents were returned"

Hello,

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.

Here’s the code snippet from page.server.ts:

export const load: PageServerLoad = async ({ params, fetch }) => {

const client = createClient({ fetch });

let uid: string | undefined = params.slug || 'index';


const document = await client.getByUID('page', uid, langParam);

if (!document) {
	error(404, {
		message: 'Not found'
	});
} else {
	return {
		document
	};
}

}

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.

Thank you.

Hey @piyush.bansal.design,

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:

import { error } from '@sveltejs/kit';
import { asText } from '@prismicio/client';

import { createClient } from '$lib/prismicio';

export async function load({ fetch, cookies }) {
	const client = createClient({ fetch, cookies });

	try {
		const page = await client.getByUID('page', 'home');

		return {
			page,
			title: asText(page.data.title),
			meta_description: page.data.meta_description,
			meta_title: page.data.meta_title,
			meta_image: page.data.meta_image.url
		};
	} catch (err) {
		error(404, String(err));
	}
}

Let me know if that helps :slight_smile:

Sam

Hello Sam,

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.

The website can be accessed here.

I am relatively new to development so I may have overlooked something simple. Any further assistance would be greatly appreciated.

Kind regards,
Piyush

Hi Piyush,

Nice website! It looks fantastic.

I see that you have many queries outside of your try/catch block (linked below). You will need all of these lines to be inside of your try/catch.

Sam

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.

Link to code

Piyush

Hi Piyush,

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.

Sam

Hi Sam,

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.

Hi Piyush,

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:

What error are you seeing?

Sam

Hi Sam,

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.

Piyush

Hi @piyush.bansal.design,

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.

Sam

1 Like

Hi Sam,

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.

Best, Piyush

That's great to hear! Thanks for the update @piyush.bansal.design :)