PrismicError: No documents were returned

Hi,

I have a question, trying to upgrade from an old version of prismic-javascript.

When I'm doing a call, for example,
const menu = await client.getSingle("menu", { lang: locale }); (getSingle, or getByUID)
when 'menu' doesn't exists, shouldn't return an empty object, undefined or null, anything other than throwing an error?
This can be also replicated in GitHub - prismicio-community/nextjs-starter-prismic-multi-language: Multi-language project with Next.js & Prismic' and giving a bad/missing 'documentType' name.

In the old version of "prismic-javascript": "^3.0.2" I would get an 'undefined' and move on, but in the latest version everything is breaking because of the error.

Am I doing something wrong or is this the expected behaviour?

Thanks for the help.
Ovidiu

Hello @m_ovidiu01

Welcome to the Prismic community, and thanks for reaching out to us.

I have experienced this issue, too, and I agree that it should return an empty object or undefined. I'll bring this up with my dedicated team. For the moment, you have to do error handling by applying try.. catch block.

Let me know if you have any further questions.

Thanks,
Priyanka

Hello @Priyanka,

Any estimate on when a fix would be released?

Thanks,
Jiro

Hello @jiro.farah

I don't have an ETA at the moment and I believe it's not in their roadmap because:

  • Querying by UID using a non-existing UID yields a PrismicError, this is intended;
  • Querying by UID using a non-existing document type yields a ParsingError, this is intended as the document type doesn't exist in the queried Prismic repository.

You need to put a try-catch block to catch errors like this:

import {
	createClient,
	PrismicError,
	ParsingError,
} from "https://cdn.skypack.dev/@prismicio/client@latest"

const client = createClient("your-repo-name")

// Valid call, yields document
console.log(await client.getByUID("page", "home"))

// Unknown UID, yields PrismicError
try {
	await client.getByUID("page", "unknown")
} catch (error) {
	console.error(error instanceof PrismicError)
	console.error(error)
}

// Unknown custom type, yields ParsingError
try {
	await client.getByUID("unknown", "home")
} catch (error) {
	console.error(error instanceof ParsingError)
	console.error(error.message)
}

Thanks,
Priyanka

1 Like

I am just starting development with prismic client and this behaviour is really weird.

I guess the fact that you are returning an error is not a massive problem. But the fact that you are returning PrismicError instead of NotFoundError is at least problematic.

For example, in your example provided ParsingError is actually PrismicError too. So if we try to catch PrismicErrors we will catch a bunch of stuff that we shouldn't catch.

The only solution then is actually doing something like that if I don't want to have my logs completely overrun:

export async function getBySlug(postType: string, slug: string) {
    try {
        return client.getByUID(postType, slug)
    } catch (e) {
        if (!(e instanceof PrismicError) || e.message !== `No documents were returned`) {
            console.error(e)
        }
        return null;
    }
}

which just feels very wrong :slight_smile:

Hello @maciekpaprocki

Welcome to the Prismic community, and thanks for reaching out to us.

That was a workaround. And as I mentioned before, I have already created an issue in Prismic issue tracker. But I don't have any ETA for the moment to get fix.

Thanks for jumping here and giving a vote.

Best,
Priyanka

1 Like