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