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

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.`),

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

{#if document}

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.


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

Solved in Personel chat:

The 0 error looks like [0] is being called on what should be an array but is undefined.
that could happen in a few places. Here's the out put from grep.

grep -irn "\[0\]" src/
src//lib/utils/simplybook-widget.js:419:  document.getElementsByTagName("head")[0].appendChild(link);
src//lib/components/LeadGenForm.svelte:111:      errorFields[0].focus();
src//lib/components/WaitlistForm.svelte:116:      errorFields[0].focus();
src//lib/components/LangSwitcher.svelte:25:      if ($activeLanguage_Store && lang[0] === pageLanguage && $altLanguages_Store.length > 0) {
src//lib/components/LangSwitcher.svelte:34:  // const getPageObject = (lang) => $altLanguages_Store.find(page => page.lang === lang[0]);
src//lib/components/LangSwitcher.svelte:36:    return $altLanguages_Store.find(page => page.lang === lang[0]);
src//lib/components/LangSwitcher.svelte:64:          {#if lang[0] === pageLanguage}
src//routes/contact.svelte:140:      errorFields[0].focus();
src//routes/__layout.svelte:42:  $: headerNavLinks = getHeaderNavLinks[0] ? getHeaderNavLinks[0].data.links : null;
src//routes/stripe/webhook/checkout-completed.js:48:    const productId =[0].price.product;
src//routes/[lang]/contact.svelte:146:      errorFields[0].focus();```

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.