Vercel & SvelteKit 'Unhandled Promise Rejection' Application Error

I'm running SvelteKit on Vercel using the Prismic and Javascript API. Here is the error on Vercel that is seemingly random:

2021-06-10T22:17:12.466Z	50b8d94d-bfe5-46d8-a60c-d273dad7cab3	ERROR	Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read property 'data' of undefined","reason":{"errorType":"TypeError","errorMessage":"Cannot read property 'data' of undefined","stack":["TypeError: Cannot read property 'data' of undefined","    at /var/task/index.js:8400:38","    at processTicksAndRejections (internal/process/task_queues.js:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read property 'data' of undefined","    at process.<anonymous> (/var/runtime/index.js:35:15)","    at process.emit (events.js:388:22)","    at processPromiseRejections (internal/process/promises.js:245:33)","    at processTicksAndRejections (internal/process/task_queues.js:96:32)"]}
[ERROR] [1623363432467] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 400.
RequestId: c9d2c624-e72b-480f-8334-c7c02bdcde0c Error: Runtime exited with error: exit status 128
Runtime.ExitError

Here's a screenshot:


Does this look like a Prismic related error?

Here's an example of the home page code querying the Prismic API:

<script context="module">
import { Client } from "$lib/utils/client";

export async function load({ page }) {
  const { lang } = page.params;
  const document = await Client.getSingle("home", { lang: lang });

  if (document) {
    return {
      props: { document },
    };
  }

  return {
    status: 404,
    error: new Error(`Could not load Prismic Home page.`),
  };
}
</script>

Then, in the HTML, I'm also checking if the document object is available:

{#if document}
  ...
  <h1>{PrismicDOM.RichText.asText(document.data.hero_title)}</h1>
  ...etc.
{/if}

Help! This error only happens once in a while to various pages, all of which is querying the Prismic API.

Hello @chris.davis

Thanks for reaching out to us.

I need to investigate your code. Could you please share your Github repo and Prismic repo with me? You can send me a private message too.

Thanks,
Priyanka

Hey @Priyanka, I sent you a private message on Friday. Thanks for helping!